File size: 2,969 Bytes
b27bfb3
 
 
 
1
2
3
4
5
{"harness": "opencode", "name": "MethodVisitor Instructions and Stack", "id": "lang-0028", "type": "session"}
{"message": {"content": "You are an expert Java bytecode engineer specializing in ASM, bytecode manipulation, and class transformation.", "role": "system"}, "type": "message"}
{"message": {"content": "How do I use MethodVisitor to add instructions to a method? Show me examples of visiting different instruction types and manipulating the operand stack.", "role": "user"}, "type": "message"}
{"message": {"content": "MethodVisitor allows you to visit and modify individual instructions within a method:\n\nimport org.objectweb.asm.*;\n\npublic class MethodVisitorExample {\n    public static void main(String[] args) {\n        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);\n        cw.visit(Opcodes.V11, Opcodes.ACC_PUBLIC, \"Example\", null, \"java/lang/Object\", null);\n        MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, \"doWork\", \"()V\", null, null);\n        mv.visitCode();\n\n        mv.visitFieldInsn(Opcodes.GETSTATIC, \"java/lang/System\", \"out\", \"Ljava/io/PrintStream;\");\n        mv.visitLdcInsn(\"Hello, ASM!\");\n        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, \"java/io/PrintStream\", \"println\", \"(Ljava/lang/String;)V\", false);\n\n        mv.visitInsn(Opcodes.ICONST_2);\n        mv.visitInsn(Opcodes.ICONST_3);\n        mv.visitInsn(Opcodes.IADD);\n        mv.visitVarInsn(Opcodes.ISTORE, 1);\n\n        Label label = new Label();\n        mv.visitVarInsn(Opcodes.ILOAD, 1);\n        mv.visitJumpInsn(Opcodes.IF_ICMPGE, label);\n        mv.visitFieldInsn(Opcodes.GETSTATIC, \"java/lang/System\", \"out\", \"Ljava/io/PrintStream;\");\n        mv.visitLdcInsn(\"Less than 5\");\n        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, \"java/io/PrintStream\", \"println\", \"(Ljava/lang/String;)V\", false);\n        mv.visitLabel(label);\n\n        mv.visitInsn(Opcodes.RETURN);\n        mv.visitMaxs(2, 2);\n        mv.visitEnd();\n        cw.visitEnd();\n    }\n}\n\nKey instruction categories:\n- visitCode() starts the method bytecode\n- visitInsn(opcode) executes zero-operand instructions (IADD, ICONST_0..5, RETURN, POP, DUP, etc.)\n- visitIntInsn(opcode, operand) for BIPUSH and SIPUSH\n- visitVarInsn(opcode, var) for local variable load/store (ILOAD, ISTORE, ALOAD, ASTORE, etc.)\n- visitLdcInsn(cst) loads constants (strings, int/long/float/double, class references, method handles)\n- visitFieldInsn(opcode, owner, name, desc) for field access (GETFIELD, PUTFIELD, GETSTATIC, PUTSTATIC)\n- visitMethodInsn(opcode, owner, name, desc, isInterface) for method calls (INVOKEVIRTUAL, INVOKESTATIC, INVOKEINTERFACE, INVOKESPECIAL)\n- visitJumpInsn(opcode, label) for conditional and unconditional jumps\n- visitLabel(label) marks a bytecode position as a jump target\n- visitMaxs(maxStack, maxLocals) must be called even with COMPUTE_FRAMES (placeholder)", "role": "assistant"}, "type": "message"}