user5549139
user5549139

Reputation: 187

How to duplicate the parameters of a method in JVM stack by ASM?

I am using ASM to insert a method after a special method. For example, a method as follows:

a.doSomeThing(p1, p2, p3, p4, p5, p6)

I want to insert a method follow it, just like this:

a.doSomeThing(p1, p2, p3, p4, p5, p6)
MyClass.myMethod(a, p1, p2, p3, p4, p5, p6) //insert a static method

The insert method has the same parameters as the previous one.

I know that when invoking virtual doSomeThing, the values are at the top of the stack. How can I duplicate them, and use them for myMethod?

Upvotes: 1

Views: 355

Answers (2)

user5549139
user5549139

Reputation: 187

Finally, i solve it. When call a.doSomeThing(p1, p2, p3, p4, p5, p6), the operand stack frame order in the stack is a p1 p2 p3 p4 p5 p6, so i just need to store them to local variables as p6 p5 p4 p3 p2 p1 a, now i load them from local variables to stack again, and call a.doSomeThing(p1, p2, p3, p4, p5, p6), next, load local variables again and call MyClass.myMethod(a, p1, p2, p3, p4, p5, p6).
By this way, i insert my codes which have same parameters with the previous method.

Upvotes: 1

Antimony
Antimony

Reputation: 39451

If there is only one parameter, or only two parameters that are not longs or doubles, you can use the DUP or DUP2 instructions respectively. If there are more than two parameters, there is no way to duplicate them directly using bytecode. Instead, you need to save them to local variables and then read them back.

Upvotes: 1

Related Questions