ASM是一个十分灵活高效的java字节码生成工具,他可以帮助我们动态的生成所需的字节码文件,也可以修改已存在的字节码使其满足我们新的需要,是一个功能强大的AOP工具。
ASM官网地址http://asm.ow2.org/,附件提供了官方guide很详细,以及ASM的api。
ASM本身功能强大灵活,为此付出的代价就是其功能函数粒度很细,写ASM代码有一种写汇编代码的感觉,不过开发者为此已经想好了解决办法,ASMifierClassVisitor类提供了:生成一个给定class文件的ASM代码的功能。
下面的代码就是利用ASM3.2中的ASMifierClassVisitor类生成 能够产生test类class文件的ASM代码 的代码。
package nihaokid.asm;
import java.io.InputStream;
import java.io.PrintWriter;
import org.objectweb.asm.*;package nihaokid.asm;
import org.objectweb.asm.util.ASMifierClassVisitor;
public class targetCode{
public static void main(String[] args) throws ClassNotFoundException, IOException {
ClassLoader cl = targetCode.class.getClassLoader();
java.lang.Class<?> target = hhhhtest.class;
InputStream in = cl.getResourceAsStream(target.getName().replace(".", "/")+".class");
ClassReader cr = new ClassReader(in);
ASMifierClassVisitor visitor = new ASMifierClassVisitor(new PrintWriter(System.out));
cr.accept(visitor,0);
}
}
以下贴出tset类的代码
package nihaokid.asm;
public class hhhhtest {
/**
* @param args
*/
public static void main(String[] args) {
hhhhtest t = new hhhhtest();
t.print();
}
public void print()
{
System.out.println("hello world!");
}
}
执行targetCode,输出如下
import java.util.*;
import org.objectweb.asm.*;
import org.objectweb.asm.attrs.*;
public class hhhhtestDump implements Opcodes {
public static byte[] dump () throws Exception {
ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;
cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "nihaokid/asm/hhhhtest", null, "java/lang/Object", null);
cw.visitSource("hhhhtest.java", null);
{
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLineNumber(3, l0);
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
mv.visitInsn(RETURN);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLocalVariable("this", "Lnihaokid/asm/hhhhtest;", null, l0, l1, 0);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "main", "([Ljava/lang/String;)V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLineNumber(9, l0);
mv.visitTypeInsn(NEW, "nihaokid/asm/hhhhtest");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "nihaokid/asm/hhhhtest", "<init>", "()V");
mv.visitVarInsn(ASTORE, 1);
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLineNumber(10, l1);
mv.visitVarInsn(ALOAD, 1);
mv.visitMethodInsn(INVOKEVIRTUAL, "nihaokid/asm/hhhhtest", "print", "()V");
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitLineNumber(12, l2);
mv.visitInsn(RETURN);
Label l3 = new Label();
mv.visitLabel(l3);
mv.visitLocalVariable("args", "[Ljava/lang/String;", null, l0, l3, 0);
mv.visitLocalVariable("t", "Lnihaokid/asm/hhhhtest;", null, l1, l3, 1);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "print", "()V", null, null);
mv.visitCode();
Label l0 = new Label();
mv.visitLabel(l0);
mv.visitLineNumber(16, l0);
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitLdcInsn("hello world!");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
Label l1 = new Label();
mv.visitLabel(l1);
mv.visitLineNumber(17, l1);
mv.visitInsn(RETURN);
Label l2 = new Label();
mv.visitLabel(l2);
mv.visitLocalVariable("this", "Lnihaokid/asm/hhhhtest;", null, l0, l2, 0);
mv.visitMaxs(2, 1);
mv.visitEnd();
}
cw.visitEnd();
return cw.toByteArray();
}
}