I have been trying to use ASM framework to inject bytecode at my interested location and I have been successful till now.Currently I am trying to inject code which basically creates a new instance/object of a class and after reading a bit I found that this can be achieved using INVOKESPECIAL (I hope my understanding was right of INVOKESPECIAL "INVOKESPECIAL for private methods and constructors").
Below is the code snippet I used to create instance
visitor.visitLdcInsn(System.currentTimeMillis());
visitor.visitLdcInsn(System.currentTimeMillis());
visitor.visitLdcInsn(_type);
visitor.visitVarInsn(ALOAD, metanamevarindex);
eventObject = newLocal(Type.getType("com/vish/RequestTrackerEvent"));
visitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "com/vish/RequestTrackerEvent", "<init>",
"(JJLjava/lang/String;Ljava/lang/String;)V");
visitor.visitVarInsn(ASTORE, eventObject);
The constructor of the class takes in 4 arguments (long,long,String,String) But whenever I do this I get an exception like below
java.lang.VerifyError: JVMVRFY036 stack underflow;
at java.lang.J9VMInternals.verifyImpl(Native Method)
at java.lang.J9VMInternals.verify(J9VMInternals.java:72)
at java.lang.J9VMInternals.verify(J9VMInternals.java:70)
at java.lang.J9VMInternals.initialize(J9VMInternals.java:134)
Can anyone help me in understanding whether my usage/understanding of INVOKESPECIAL is right, if right then where am I doing wrong?
Thanks
Question like "how do I generate {some Java code} with ASM" has been answered in ASM FAQ:
You can go even further, by comparing output of ASMifier before and after transformation as described in this article.