import org.objectweb.asm.*;
import org.objectweb.asm.tree.*;
public class ASMTreeExample {
public static void main(String[] args) {
ClassReader cr = new ClassReader("ExampleClass");
ClassNode cn = new ClassNode();
cr.accept(cn, 0);
for (MethodNode mn : cn.methods) {
InsnList instructions = mn.instructions;
instructions.clear();
instructions.add(new FieldInsnNode(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"));
instructions.add(new LdcInsnNode("Hello, ASM Tree!"));
instructions.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V"));
instructions.add(new InsnNode(Opcodes.RETURN));
}
cn.accept(cw);
byte[] modifiedBytes = cw.toByteArray();
// ...
// ...
}
}