ASM的helloworld例子(mark).

mark贴.

package asm;

import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * User: zhenghui
 * Date: 13-11-4
 * Time: 下午8:33
 */
public class AsmHelloWorld extends ClassLoader{

    public static void main(String[] args) throws InvocationTargetException, IllegalAccessException {
        ClassWriter classWriter = new ClassWriter(0);
        classWriter.visit(Opcodes.V1_6,Opcodes.ACC_PUBLIC,"Example",null,"java/lang/Object",null);
        //创建默认构造器
        MethodVisitor methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC,"<init>","()V",null,null);
        methodVisitor.visitVarInsn(Opcodes.ALOAD,0);
        methodVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL,"java/lang/Object","<init>","()V");
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(1,1);
        methodVisitor.visitEnd();
        methodVisitor = classWriter.visitMethod(Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC,"main","([Ljava/lang/String;)V",null,null);
        // 调用System类的PrintStream类的out
        methodVisitor.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        methodVisitor.visitLdcInsn("Hello World!");
        methodVisitor.visitMethodInsn(Opcodes.INVOKEVIRTUAL,"java/io/PrintStream","println","(Ljava/lang/String;)V");
        methodVisitor.visitInsn(Opcodes.RETURN);
        methodVisitor.visitMaxs(2,2);
        byte[] code = classWriter.toByteArray();
        AsmHelloWorld classLoader = new AsmHelloWorld();
        Class clazz = classLoader.defineClass("Example",code,0,code.length);
        Method method = clazz.getMethods()[0];
        method.invoke(null,new Object[]{null});
    }
}

 

你可能感兴趣的:(helloworld)