ASM is not loading object in class retransform but working fine with usual transform

StackOverflow https://stackoverflow.com/questions/21915053

  •  14-10-2022
  •  | 
  •  

Question

I am trying to load some object through bytecode modification using asm bytecode instrumentation library.

I am retransforming the classes using retransformClasses() method.

I am loading the objects in this way :

super.visitVarInsn(Opcodes.ALOAD, 0);
super.visitFieldInsn(Opcodes.GETFIELD, owner, name, desc);
super.visitMethodInsn(org.objectweb.asm.Opcodes.INVOKESTATIC, 
                                    "com/coolcoder/MyClass", 
                                    "objectCheckTest",
                                    "(Ljava/lang/Object;)V");`

The problem is that I the objects are getting loaded using usual tranform() of ClassTransformer , but when I am using Attach API's retranformClasses() , these objects are not getting loaded . Strange thing is that , I am not getting any bytecode error either.

Am I doing something wrong ? or am I missing some intricate part regarding retransform ?

Was it helpful?

Solution

I was be able to solve the issue , though I do not know why it happened in the first place.

(1) I was loading the object only on PUTFIELD or PUTSTATIC opcodes, i.e , when the object value is being set.

(2) Just after bytecode modification the class is getting redefined , as a result of which the code snippet did not work.

(3) Next time when the class loaded due to redefinition , the object is already being set , so it will not be called again.

(4) Now , when I checked for GETFIELD or GETSTATIC opcodes , i.e when the object's value is being retrieved and I loaded it , I was able to view its details.

The same approach which I mentioned above is working fine in usual transformation (i.e running with premain).

Any idea on this erratic behaviour of redefinition ?

OTHER TIPS

First, in manifest.mf file,Can-Retransform-Classes attribute should be true.

And your ClassFileTransformer instance should be add with the true value for parameter canRetransform in method addTransformer .

Here is another additional tips:

If the transformer throws an exception (which it doesn't catch), subsequent transformers will still be called and the load, redefine or retransform will still be attempted. Thus, throwing an exception has the same effect as returning null. To prevent unexpected behavior when unchecked exceptions are generated in transformer code, a transformer can catch Throwable. If the transformer believes the classFileBuffer does not represent a validly formatted class file, it should throw an IllegalClassFormatException; while this has the same effect as returning null. it facilitates the logging or debugging of format corruptions.

from java api docs

So, there maybe some uncatched exception which will be just ignored by the jvm arised. You can put a try..catch

Please forgive my poor English ...

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