如何在Java上修改和应用已定义的类

时间:2017-02-01 17:10:07

标签: java java-bytecode-asm

我想修改并应用已定义的类。

我更改了"目标"下面的课程使用ASM,但我不知道如何在运行时应用它。

如何制作" newTarget.a()"和" newTarget.b()"调用已更改类的方法?

package asm;

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

import static org.objectweb.asm.Opcodes.ASM5;

public class Main {
    public static void main(String[] args) throws Exception {
        Target target = new Target();
        target.a();
        target.b();

        ClassReader reader = new ClassReader("asm.Main$Target");
        ClassWriter writer = new ClassWriter(0);
        ClassVisitor visitor = new TestClassVisitor(ASM5, writer);

        reader.accept(visitor, 0);

        byte[] transformed = writer.toByteArray();

        // Apply byte[] transformed

        Target newTarget = new Target();
        newTarget.a();
        newTarget.b();
    }

    static class Target {
        private void a() {
            System.out.println("first method");
        }

        private void b() {
            System.out.println("second method");
        }
    }

    static class TestClassVisitor extends ClassVisitor {
        public TestClassVisitor(int i, ClassVisitor classVisitor) {
            super(i, classVisitor);
        }

        @Override
        public MethodVisitor visitMethod(int i, String s, String s1, String s2, String[] strings) {
            MethodVisitor visitor = super.visitMethod(i, s, s1, s2, strings);
            if (!s.equals("<init>")) {
                return visitor;
            }
            return new TestMethodVisitor(api, visitor);
        }
    }

    static class TestMethodVisitor extends MethodVisitor {
        public TestMethodVisitor(int i, MethodVisitor methodVisitor) {
            super(i, methodVisitor);
        }

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

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

0 个答案:

没有答案