I have written a Class with premain method and inside that method I have added a ClassFileTransformer to Instrumentation (Instrumentation.addTransformer()).
I have invoked a application using
java -javaagent:<path_to_agnet.jar> <application>
However ClassFileTransformer.transform() method is not being invoked.
I have observed that premain is being invoked.
I have also observed that if I call Instrumentation.retransformClasses(), then ClassFileTransformer.transform() is being invoked.
On first definition (Classloader.defineClass()), transform() method is not being invoked.
Any clue what could be wrong?
Note: I can post the source code if that is of any help.
Regards, Rejeev.
Possible causes
.
Incorrect MANIFEST
Did you follow all the steps required to define an Instrumentation class ?
Especially the ‘packaging’ step, which involve specifing a slightly different set of attributes in the JAR’s manifest:
.
Silent Exception
(Rejeev Divakaran got that one).
I was using
classBeingRedefined.getName()to print the class name.classBeingRedefinedisnullwhen it is loaded first time.The bottom line is if there is uncaught exception in transform method.
It will be silently eaten up.