Hey friends! I'm wondering why some of my code isn't working quite well. I'm using ASM and playing around with bytecode injection. Currently I have it injecting into the bytecode correctly, however I'm having some trouble getting it copy the original code as well. Beforehand I was wrapping the instructions in a try-catch statement, but it was copying the original code into the catch statement, which is obviously not what I want. I'm at the end of my rope here trying to figure this out, not even ChatGPT can provide any insight. If anyone could point me in the right direction that'd be amazing. Forgot to note, the MethodNode is the methodnode of InjectMethod#injectableMethod(String className)
Original Code:
public ConstructorTest(QuickTest test) {
this.test = test;
System.out.println("ConstructorTest");
}
@InjectLocation
public ConstructorTest(QuickTest test1,String test) {
this.test = test1;
System.out.println("ConstructorTest " + test);
}```
Injected Code (Removing the annotation is intentional):
```public ConstructorTest(QuickTest var1) {
String var2 = "Lme/quicktest/QuickTest;";
System.out.println("Injecting into " + var2);
}
public ConstructorTest(QuickTest var1, String var2) {
String var3 = "Lme/quicktest/QuickTest;";
System.out.println("Injecting into " + var3);
}```
Method in which I am stealing instructions from:
```public class InjectMethod {
public void injectableMethod(String className) {
System.out.println("Injecting into " + className);
}
}```
Code Visitor:
``` @Override
public void visitCode() {
super.visitCode();
if (isAnnotated) {
isAnnotated = false;
System.out.println("Injecting into " + name);
int varIndex = methodNode.maxLocals;
methodNode.visitLocalVariable(
"className",
"Ljava/lang/String;",
null,
new Label(),
new Label(),
varIndex);
methodNode.maxLocals++;
super.visitLdcInsn(className);
super.visitVarInsn(Opcodes.ASTORE, 1);
super.visitVarInsn(Opcodes.ALOAD, 1);
// Insert the instructions of the methodNodeToInsert at the beginning
// of the visited method's instruction list
methodNode.instructions.accept(this);
super.visitLabel(new Label());
System.out.println("Finished injecting into " + name);
}
}```