SystemYou are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.
You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.
User
How do I inject a new method into an existing class using ASM? Show me a complete example.
Assistant
To inject a new method, intercept the ClassVisitor and add the method in visitEnd():
import org.objectweb.asm.*;
public class MethodInjectionExample { public static void main(String[] args) throws Exception { ClassReader cr = new ClassReader("com.example.MyClass"); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES);