Bastian Helfert wrote:
Hello everybody,
I re-read the API documentation for
/java.lang.instrument.Instrumentation/ over and over again, but I
could not figure out what the (main) difference is between the methods
/redefineClasses/ and /retransformClasses/.
Imagine I have multiple agents that (re)transform the bytecode of the
same class. By using /retransformClasses/ the current definition of
the class is passed to the transform method.
But could not I also read the current class file bytes and instrument
them, e.g. by using a bytecode manipulation library, /before /I pass
these bytes to /redefineClasses/?
Hence, what can I do with /retransformClasses/ that I cannot also do
with /redefineClasses/? Or is /retransformClasses/ actually "API
sugar"? When would I use what method?
The main recommendation is to use retransformClasses if you are doing
instrumentation. The redefineClasses method is more intended for
fix-and-continue and other cases where the class definition needs to be
replaced.
The main motivation and benefit of retransformClasses is that it allows
multiple instrumentation agents to co-exist. Each agent's transformer
gets to instrument the class in turn. Removing instrumentation becomes
significantly easier as the retransformClasses starts with the initial
bytes.
Does does help?
-Alan.