Mailing List webobjects-dev@wocommunity.org Message #229
From: OCsite <webobjects-dev@wocommunity.org>
Subject: Re: [WO-DEV] change toString for some classes?
Date: Sat, 3 Jul 2021 23:05:42 +0200
To: WebObjects & WOnder Development <webobjects-dev@wocommunity.org>
Dennis,

thanks a lot, but it does not work for me — I keep getting

javassist.CannotCompileException: by java.lang.LinkageError: loader 'app' attempted duplicate class definition for com.webobjects.foundation.NSTimestamp. (com.webobjects.foundation.NSTimestamp is in unnamed module of loader 'app')

Looks like some darned pre-main magic of Groovy or my own code in some static block or something like that touches (indirectly, for I can't find the culprit in my sources) the class and loads it before it can be patched. Pity.

Thanks and all the best,
OC

On 30 Jun 2021, at 11:56, Dennis Bliefernicht <webobjects-dev@wocommunity.org> wrote:

Hi,
 
is there some Wonder (or NS- or Java-level) trick to “override” (for the original class; in ObjC I would simply swizzle the method) toString for some classes? Namely, in my current application, I would need that
 
there are some ways in Java to do this kind of thing, Javassist is something we still use in an older codebase, there is also ByteBuddy which tries to wrap everything in a  neat API. One important thing is that you need to do this _very_ early in your application, before the relevant classes are loaded (because this hooks into the class loading mechanism, once the class is loaded, you can’t modify it that easily anymore). In fact we do this basically first thing in the main method. I don’t have any snippets at hand for ByteBuddy, but below is an outline, how we replaced some methods with Javassist. Basically we prepend the method with “return ApplicationUtility.myReplacementMethod(this)” which makes it easy enough to just code up the replacement method as usual.
 
Performancewise this seems to be fine, in our case we had to replace a .hashCode() deep in WO, because performance monitoring revealed that our data modelling resulted in wildly bad behaviour of EO objects in HashMaps and after the fix we got good improvements. So I assume generally the JIT compiler will smooth out any extra redirections.
 
Hope that helps && Greetings
Dennis
 
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtConstructor;
import javassist.CtMethod;
import javassist.LoaderClassPath;
 
public class ApplicationUtility {
     public static int myReplacementMethod(final NSTimestamp object) {
           // do your magic
           return "TODO";
     }
 
     public static void patchClasses() {
           try {
                final ClassPool classPool = ClassPool.getDefault();
                classPool.appendClassPath(new LoaderClassPath(Thread.currentThread().getContextClassLoader()));
 
                final CtClass classToPatch = classPool.get("com.webobjects.foundation.NSTimestamp");
                final CtMethod toStringMethod = classToPatch.getDeclaredMethod("toString");
                toStringMethod.insertBefore("{ return " + ApplicationUtility.class.getName() + ".myReplacementMethod(this); }");
                classToPatch.toClass();
           } catch (Exception e) {
                // TODO: error handling, probably logging and exit
           }
     }
}
 
 
 
-- 
<image001.png>
-----------------------------------------------------
Dennis Bliefernicht • Head of Backend Development
T +49 40 357 3001 62
 
XYRALITY GmbH • Friedensallee 290 • 22763 Hamburg
Registergericht: Hamburg HRB 115332
Geschäftsführer: Sven Ossenbrüggen
-----------------------------------------------------
 
 
#############################################################
This message is sent to you because you are subscribed to
 the mailing list <webobjects-dev@wocommunity.org>.
To unsubscribe, E-mail to: <webobjects-dev-off@wocommunity.org>
To switch to the DIGEST mode, E-mail to <webobjects-dev-digest@wocommunity.org>
To switch to the INDEX mode, E-mail to <webobjects-dev-index@wocommunity.org>
Send administrative queries to  <webobjects-dev-request@wocommunity.org>

Subscribe (FEED) Subscribe (DIGEST) Subscribe (INDEX) Unsubscribe Mail to Listmaster