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 use MethodVisitor to add instructions to a method? Show me examples of visiting different instruction types and manipulating the operand stack.
Assistant
MethodVisitor allows you to visit and modify individual instructions within a method:
import org.objectweb.asm.*;
public class MethodVisitorExample { public static void main(String[] args) { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cw.visit(Opcodes.V11, Opcodes.ACC_PUBLIC, "Example", null, "java/lang/Object", null); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "doWork", "()V", null, null); mv.visitCode();