How can I rewrite a class (preferably using asm) such that a setupSpecial() invocation is introduced before every single call to special(int)?
public class Application { public void f() { System.out.println("A"); // setupSpecial(); ← to be added special(1); System.out.println("B"); // setupSpecial(); ← to be added special(2); System.out.println("C"); // setupSpecial(); ← to be added special(3); } } Note that both special() and setupSpecial() are not members of Application.
I can pass a ClassVisitor+MethodVisitor combination and intercept calls to special(int) via overriding visitMethodInsn(Opcodes.INVOKEVIRTUAL, "com/acme/Magic", "special", "()V", false), though this will be too late since the method argument of type integer is already passed via visitIntInsn(Opcodes.BIPUSH, int). I need a way to say "Now I see the call to special()! Roll this call back, turn magic on, and proceed again."