user10608712
user10608712

Reputation:

Creating MethodNode fails with IllegalStateException

I want to get the MethodNode of main() function

public class TestMethodNode {      
    public void main() {            
    }
}

so I tried this

import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.MethodNode;   
import static org.objectweb.asm.Opcodes.ASM7; 
import java.io.IOException;    
    
public class Instrumentation {
    public byte[] editFunction(String className) throws IOException {    
        byte[] modifiedClass = null;    
        try {    
            ClassReader classReader = new ClassReader(className);
            ClassWriter classWriter = new ClassWriter(classReader, ClassWriter.COMPUTE_FRAMES);
            ClassVisitor classAdapter = new ClassVisitor(ASM7, classWriter) {    
                public MethodVisitor visitMethod(
                        int access,
                        String name,
                        String desc,
                        String signature,
                        String[] exceptions) {    
                    if (name.equals("main")) {
                        final MethodVisitor methodVisitor = super.visitMethod(access, name, desc, signature, exceptions);
                        MethodNode methodNode = new MethodNode(access, name, desc, signature, exceptions) {
                            public void visitEnd() {
                                // transform / analyze method here
                                accept(methodVisitor);
                            }
                        };    
                        return methodNode;
                    }  else {  
                        return super.visitMethod(access, name, desc, signature, exceptions);
                    }
                }
            };    
            classReader.accept(classAdapter, 0);
            modifiedClass = classWriter.toByteArray();  
        } catch (IOException ex) {    
            throw ex;
        }    
        return modifiedClass;
    }
}

and got IllegalStateException from .../asm/tree/MethodNode.java while creating MethodNode

  public MethodNode(
      final int access,
      final String name,
      final String descriptor,
      final String signature,
      final String[] exceptions) {
    this(Opcodes.ASM7, access, name, descriptor, signature, exceptions);
    if (getClass() != MethodNode.class) {
      throw new IllegalStateException();
    }
  }

What am I doing wrong? (I don't want to hack Minecraft, I am researching and trying to manipulate callstacks which involve lambda expressions and nested / inner classes for a workflow engine that uses bytecode manipulation at runtime.)

Upvotes: 1

Views: 360

Answers (2)

user10608712
user10608712

Reputation:

Just to provide a full running sample to instrument function main() of a given class

public class Instrumentation {
    public byte[] instrument(String className) {

        byte[] modifiedClass = null;

        try {

            ClassReader classReader = new ClassReader(className);
            ClassWriter classWriter = new ClassWriter(classReader, 4);
            ClassVisitor classVisitor = new ClassVisitor(ASM7) {
                public MethodVisitor visitMethod(
                        int access,
                        String name,
                        String desc,
                        String signature,
                        String[] exceptions) {
                    if (name.equals("main")) {
                        MethodNode methodNode = new MethodNode(ASM7,access, name, desc, signature, exceptions) {
                            public void visitEnd() {
                                // do some stuff here; remove exceptions, insnnode etc. -- smaple iterates through instructions
                                for (int i = 0; i < this.instructions.size();i++) {
                                    AbstractInsnNode node = this.instructions.get(i);
                                }
                            }
                        };
                        return methodNode;
                    } else {
                        return super.visitMethod(access, name, desc, signature, exceptions);
                    }
                }
            };

            classReader.accept(classVisitor,0);
            classReader.accept(classWriter, 0);
            modifiedClass = classWriter.toByteArray();

        } catch (IOException ex) {
           // handle IOException here
        }

        return modifiedClass;
    }
}

Upvotes: 1

Holger
Holger

Reputation: 298143

From the documentation of the constructor MethodNode(int access, String name, String descriptor, String signature, String[] exceptions):

Subclasses must not use this constructor. Instead, they must use the MethodNode(int, int, String, String, String, String[]) version.

Since you are creating a subclass, you have to change the call

new MethodNode(access, name, desc, signature, exceptions) {
…
}

to

new MethodNode(ASM7, access, name, desc, signature, exceptions) {
…
}

Upvotes: 2

Related Questions