Monday, January 23, 2012

Lesson learnt during writing Java Agent

"Never Use Class Object reference which you want to transform in the 'agent classes' itself".

To understand above sentence, lets take an example if I want to instrument class "com.my.target.Klass1" and some where in agent jar file itself we made a reference to Klass1 object by saying
"Klass1.class"
then this class will not be called for transformation by registered ClassFileTransformer in the agent.

In my case I wanted "Klass1" name so I added code in ClassFileTransformer implementation as below:

String klassObject = Klass1.class.getName();

and when I made agent jar and wanted to use it for instrumentation, this class (Klass1.class) was no where to be seen. I struggle for hours and noticed that all other classes were called in by ClassFileTransformer implementation and only "Klass1" class was not. I had to change above code not to refer to class object directly. So I removed "Klass1.class" statement and replaced it with complete class name as below
String klassObject = "com.my.target.Klass1";

Now it worked like a charm...

No comments:

Post a Comment