What is the main purpose of the Transformer in the java instrumentation. I know that they are to be added with the instance of instrumentation. Also in the premain method, what parameters are assigned to agentArgs
in public static void premain(String agentArgs, Instrumentation inst);
Transformers (java.lang.instrument.ClassFileTransformer) are used to transform class files on byte code level before they are loaded into the Java Virtual Machine. That feature can be used to modify the methods on byte level (really, you get a byte array) or by using a third-party library like Javassist or ASM where Javassist is on a higher modification layer (you can provide source code that gets compiled during the runtime). ASM works with the bytecode keywords (e.g., invokevirtual
).
Modifying the classes and its entries during the load time is used in popular Frameworks like AspectJ to provide the ability to load aspects (that is what is called "loadtime-weaving").
The agentArgs
parameter in the premain(...)
method is a String
containing the parameters that where given to the agent on startup. It is a single String
that shall be parsed by the agent itself.
You can provide one for the agent like this:
$ java -javaagent:${jarfile}=${agentArgs}
The value you enter as ${agentArgs}
will be the value of the agentArgs
parameter.
Why it is called args
if it only contains one argument? Only god knows.