ASM java 字节码框架常用代码片断


输出hello world

mv.visitFieldInsn(GETSTATIC, Type.getInternalName(System.class),
    "err", Type.getDescriptor(PrintStream.class));
mv.visitLdcInsn("hello world!");
mv.visitMethodInsn(INVOKEVIRTUAL,
    Type.getInternalName(PrintStream.class), "println", "("
        + Type.getDescriptor(String.class) + ")V");

生成默认构造方法

MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "", "()V", null,
    new String[] { Type.getInternalName(Exception.class) });
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V");
mv.visitInsn(RETURN);
mv.visitMaxs(0, 0);
mv.visitEnd();