I need to know the name of the object which called invokevirtual operation(in the following format - Objectname@object_id). Is it possible given only MethodInsnNode object? I know that it is stored with the index 0 in local variables but I don't know how to get it.
Java asm get "this" object from method variables
1k views Asked by Sergey AtThere are 2 answers
On
Here is how I solved this puzzle in my fork of the JavaFlow library. For the exact implementation please take a look at a CallSiteFinder.java in my Github repository.
The idea is the following. When you have method instruction, you may get the necessary stack size required for correct method invocation
Type.getArgumentsAndReturnSizes(methodInsnNode.desc) >> 2
Now move by instructions list starting from the given MethodInsnNode in the reverse direction and reducing the obtained stack size according to the OPCode of the every instruction encountered. Continue until your size is zero. At this point you have an instruction node that is used for "this" -- first argument for instance method call. It could be ALOAD (incl. this of the method you are visiting -- "ALOD 0"), AALOAD, GETSTATIC, GETFIELD or a result from the previous method call when several method calls are chained like
StringBuilder.append(...).append(..)
You need the MethodNode to get access to the local variables.
If you have the associated MethodNode in the variable
mnand your MethodInsnNode stored ininsnthen: