Search code examples
java-bytecode-asm

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


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 ?


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 ?