javajava-bytecode-asm

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.)


Solution

  • 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) {
    …
    }