Reputation: 13801
I have a code like this:
tracker.setValue(false);
for which I need to generate the bytecode via ASM. So using the tool, I found the bytecode instructions for the above line is:
ALOAD 0
GETFIELD example/MethodAdapter.tracker : Lexample/Tracker;
ICONST_0
INVOKEVIRTUAL example/Tracker.setValue (Z)V
so when I convert this to into an ASM code, which looks like this:
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "example/MethodAdapter", "tracker", "Lexample/Tracker;");
mv.visitInsn(ICONST_0);
mv.visitMethodInsn(INVOKEVIRTUAL, "example/Tracker", "setValue", "(Z)V", false);
(this code snippet is written into visitMethodInsn
) however running the above ASM with -javaagent
for a test class , I get an exception saying:
Exception Details:
Location:
TestClass.main([Ljava/lang/String;)V @20: getfield
Reason:
Type '[Ljava/lang/String;' (current frame, stack[0]) is not assignable to 'example/MethodAdapter'
Current Frame:
bci: @20
flags: { }
locals: { '[Ljava/lang/String;', 'TestClass' }
stack: { '[Ljava/lang/String;' }
Bytecode:
0000000: b800 02bb 0003 59b7 0004 4c2b b600 052b
0000010: b600 062a b400 2a03 b600 30b8 0007 b1
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2688)
at java.lang.Class.getMethod0(Class.java:2937)
at java.lang.Class.getMethod(Class.java:1771)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
I found the issue is happening with this line:
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, "example/MethodAdapter", "tracker", "Lexample/Tracker;");
looks like ALOAD
of 0
is not pulling out the proper this
or am I missing something here?
Upvotes: 0
Views: 236
Reputation: 39441
You're inserting the code into your main
function, which is static, and hence has no this
argument. The first argument is the string array.
Upvotes: 1