ASM 改写方法

Generator.java


import java.io.File;
import java.io.FileOutputStream;

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;

public class Generator  extends ClassLoader implements Opcodes {

 /**
  * @param args
  */
 public static void  main(String[] args) throws Exception {
     ClassReader cr = new ClassReader("MyBeanHello");
     ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
     ClassAdapter classAdapter =  new MyClassAdapter(cw);
     cr.accept(classAdapter, ClassReader.SKIP_DEBUG);
     byte[] code = cw.toByteArray();
     Generator loader = new Generator();
     Class<?> exampleClass = loader.defineClass("MyBeanHello", code, 0, code.length);
     exampleClass.getMethods()[3].invoke(exampleClass.newInstance(), new   Object[]{ "Jack "} );   
     byte[] data = cw.toByteArray();
     File file = new File("/devTool/MyBeanHello.class");
     FileOutputStream fout = new FileOutputStream(file);
     fout.write(data);
     fout.close();

 }

}


MyBeanHello.java


public class MyBeanHello {

    private String name;
    private int age;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        System.out.println(" run "+name);
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
      
    public String toString(){
        return "name:"+name+";age:"+age;
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        System.out.println("run main ");
        System.out.println( new MyBeanHello().toString());
}

}

MyClassAdapter.java


import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;

public class MyClassAdapter extends ClassAdapter {

    public MyClassAdapter(ClassVisitor cv) {
        super(cv);
    }

    public MethodVisitor visitMethod(int access, String name, String desc,String signature, String[] exceptions) {
        
        MethodVisitor mv = cv.visitMethod(access, name, desc, signature,exceptions);
        MethodVisitor wrappedMv = mv;
        if (mv != null) {
      
            if (name.equals("setName")) {
                // 使用自定义 MethodVisitor,实际改写方法内容
                wrappedMv = new MyMethodAdapter(mv);
            } else {
            }
        }
        return wrappedMv;

    }
 
}


MyMethodAdapter.java

import org.objectweb.asm.Label;
import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class MyMethodAdapter extends MethodAdapter {

    public MyMethodAdapter(MethodVisitor mv) {
        super(mv);
        mv.visitCode();
        Label l0 = new Label();
        mv.visitLabel(l0);
        mv.visitLineNumber(4, l0);
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn(" MyMethodAdapter  Code add By ASM Eclipse ");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
        Label l1 = new Label();
        mv.visitLabel(l1);
        mv.visitLineNumber(5, l1);
//        mv.visitInsn(Opcodes.RETURN);
    }
}

用jad反编译后的MyBeanHello.java


// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3)

import java.io.PrintStream;

public class MyBeanHello
{

    public MyBeanHello()
    {
    }

    public String getName()
    {
        return name;
    }

    public void setName(String s)
    {
        System.out.println(" MyMethodAdapter  Code add By ASM Eclipse ");
        System.out.println((new StringBuilder(" run ")).append(s).toString());

    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int i)
    {
        age = i;
    }

    public String toString()
    {
        return (new StringBuilder("name:")).append(name).append(";age:").append(age).toString();
    }

    public static void main(String args[])
    {
        System.out.println("run main ");
        System.out.println((new MyBeanHello()).toString());
    }

    private String name;
    private int age;
}

你可能感兴趣的:(eclipse,ClassLoader,String,File,Class,generator)