java ASM 实现新生子类
java ASM 实现新生子类
yan5845hao 发表于9个月前
java ASM 实现新生子类
  • 发表于 9个月前
  • 阅读 16
  • 收藏 0
  • 点赞 0
  • 评论 0

新睿云服务器60天免费使用,快来体验!>>>   

需要实现效果

package com.uwo.resources.asm.test;

/**
 * Created by yanhao on 2017/5/13.
 */
public class Uwo$Test{

    public Uwo$Test(String name){
        super(name);
    }

    public String hello(String say){
        System.out.println("START");
        System.out.println("hello source!!");
        System.out.println("END");
        return name  + " == " + say;
    }
    
}

原类

package com.uwo.resources.asm.test;

/**
 * Created by yanhao on 2017/5/13.
 */
public class Uwo{

    protected String name;

    public Uwo(String name){
        this.name = name;
    }

    public String hello(String say){

        System.out.println("hello source!!");

        return name  + " == " + say;
    }

    protected Integer sex;

}

实现MethodAdapter

package com.uwo.resources.asm.test;

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

/**
 * Created by yanhao on 2017/5/15.
 */
public class UwoMethodAdapter extends MethodAdapter{

    public UwoMethodAdapter(MethodVisitor mv){
        super(mv);
    }


    @Override
    public void visitCode() {
        mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
        mv.visitLdcInsn("START");
        mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
    }

    @Override
    public void visitInsn(int opcode) {
        if ((opcode >= Opcodes.IRETURN && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW) {
            mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
            mv.visitLdcInsn("END");
            mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V");
        }
        mv.visitInsn(opcode);
    }

    @Override
    public void visitEnd() {
        super.visitEnd();
    }
}

实现ClassAdapter

package com.uwo.resources.asm.test;

import org.objectweb.asm.*;

/**
 * Created by yanhao on 2017/5/15.
 */
public class UwoClassAdapter extends ClassAdapter{

    private String superName;

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

    @Override
    public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
        System.out.println("visit start");
        this.superName = name;
        super.visit(version, access, name + "$Test", signature, name, interfaces);
        System.out.println("visit end");
    }

    @Override
    public void visitOuterClass(String owner, String name, String desc) {
        System.out.println("visitOuterClass start");
        super.visitOuterClass(owner, name, desc);
        System.out.println("visitOuterClass end");
    }

    @Override
    public void visitInnerClass(String name, String outerName, String innerName, int access) {
        System.out.println("visitInnerClass start");
        super.visitInnerClass(name, outerName, innerName, access);
        System.out.println("visitInnerClass end");
    }

    @Override
    public void visitAttribute(Attribute attr) {
        System.out.println("visitAttribute start");
        super.visitAttribute(attr);
        System.out.println("visitAttribute end");
    }

    @Override
    public void visitSource(String source, String debug) {
        System.out.println("visitSource start");
        super.visitSource(source, debug);
        System.out.println("visitSource end");
    }

    @Override
    public void visitEnd() {
        System.out.println("visitEnd start");
        super.visitEnd();
        System.out.println("visitEnd end");
    }

    @Override
    public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
        System.out.println("FieldVisitor");
        return super.visitField(access, name, desc, signature, value);
    }

    @Override
    public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
        System.out.println("MethodVisitor");
        MethodVisitor mv = cv.visitMethod(access, name, desc, signature, exceptions);
        if(!name.equalsIgnoreCase("<init>")){
            return new UwoMethodAdapter(mv);
        }else{
            mv.visitVarInsn(Opcodes.ALOAD, 0);
            mv.visitVarInsn(Opcodes.ALOAD, 1);
            mv.visitMethodInsn(Opcodes.INVOKESPECIAL, superName, "<init>", "(Ljava/lang/String;)V");
            mv.visitInsn(Opcodes.RETURN);
            return mv;
        }

    }

    @Override
    public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
        System.out.println("AnnotationVisitor");
        return super.visitAnnotation(desc, visible);
    }

}

执行方法

try {
            ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
            ClassReader cr = new ClassReader(Uwo.class.getName());
            UwoClassAdapter adapter = new UwoClassAdapter(cw);
            cr.accept(adapter, 0);
            byte[] bytes = cw.toByteArray();
            TestLoader loader = new TestLoader();
            Class<?> clazz = loader.findClass("com.uwo.resources.asm.test.Uwo$Test", bytes);
            Constructor constructor = clazz.getConstructor(new Class[]{String.class});
            Object o = constructor.newInstance("YANHAO123456");
            Method main = clazz.getMethods()[0];
            Object res = main.invoke(o, new Object[]{"hello"});
            if(res != null){
                System.out.println(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

执行结果

START
hello source!!
END
YANHAO123456 == hello
  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 6
博文 102
码字总数 20659
×
yan5845hao
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: