Question

I'm having fun doing some Java bytecode modification tutorials. All of them state that I need to have the following manifest attirbutes set: Can-Redefine-Classes: true Can-Retransform-Classes: true Can-Set-Native-Method-Prefix: true It's completely fine, but none of the tutorials explain what do the attributes exactly do. The oracle documentation is pretty laconic, except for the Can-Set-Native-Method-Prefix: true which I assume allows to instrument native methods as well, since they can't by simply altered as Java methods...

Could anyone please explain what is the difference between redefinition and retransformation of classes?

Was it helpful?

Solution

First, these attributes are not necessary for bytecode manipulations in general. They are there to request special features for java agents using the instrumentation feature of the JVM. E.g. creating new classes by assembling bytecode works without them.

Redefinition means that at an arbitrary point of time an agent will invoke Instrumentation. redefineClasses to change the actual definition of existing (and already loaded) classes. The agent will provide the bytecode for the new definition.

Retransformation refers to the process of class file transformation which is normally applied at class loading time. Agents can register ClassFileTransformers which are called one after another to apply transformations to the byte code before the class will be initialized. So Retransformation refers to the capability of the JVM to repeat this process for already loaded classes. In this case an agent may invoke Instrumentation.retransformClasses specifying which classes to retransform but no bytecode. Instead the JVM will call all registered retransforming capable ClassFileTransformers providing the actual bytecode (or the result of the previous transformer for a chained transformer).

Setting a native method prefix is even more special; it might be usefull when instrumenting native methods.

So you see, it’s unlikely that you really need always all of these feature when playing around with bytecode.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top