When using javassist to instrument a com.sun.net.*
class I could successfully use .insertAfter method.
However, when trying to instrument a third-party class from an app running in Glassfish I get an error:
Failed instrumenting com.app.class
agent.internal.javassist.CannotCompileException: cannot find org.springframework.class
So it looks like the javaagent is able to fetch the bytecode of the class, but can't compile it, because some dependencies of the instrumented method (from org.springframework
) can't be found.
It is unusual behaviour, as one would expect javassist to simply append bytecode to the instrumented method without having to refer to any classes referenced in that same method.
I could debug the problem through inspecting the ClassPool that the agent uses:
ClassPool pool = new ClassPool();
pool.appendSystemPath();
CustomLogger.logText("Class pool: " + pool.toString());
resulting in:
Class pool: [class path: my.java.agent.Transformer.class:java.lang.Object.class:]
We can then insert exact path (on the machine where agent will run!) to the missing library *.jar into the classPool:
pool.insertClassPath("/path/to/glassfish/modules/missing-module.jar");
Which results in:
Class pool: [class path: file:/path/to/glassfish/modules/missing-module.jar:my.java.agent.Transformer.class:java.lang.Object.class:]
javassist can now compile the class