I have a method which returns a value that is generated in another method similar to this:
public static FileChannel open()
{
return provider.newObject();
}
So the bytecode of the method roughly looks like this:
INVOKEVIRTUAL org/test/Helper.process ()Lorg/test/MyObject;
ARETURN
I have a Java Agent which uses ASM to do bytecode-transformation when the JVM starts up.
Now I would like to inject code which access the returned MyObject without doing too much changes to the invoke itself, i.e. ideally I would just add some bytecode instructions before the ARETURN.
Which ASM/bytecode construct allows me to access the object that is returned here?
For something simple, you can just put a DUP
instruction in there followed by the desired use. If you need to inject more complex code, you should store it in a register (it doesn't really matter which since it won't be used after your code except in the exceedingly unlikely event that areturn
throws an exception and there's an exception handler for it in the method).
So if you're using register 0 it would go like
astore_0
(your code) aload_0
areturn
.