带有ASM的javaagent FileTransformer无效

时间:2018-03-04 10:51:10

标签: java classloader bytecode java-bytecode-asm javaagents

我的FileTransformer运行不正常。我做得很简单。

这是premain

public class App {
    public static void premain(String agentArgs, Instrumentation inst) {
        inst.addTransformer(new MyFirstFileTransformer());
        System.out.println("first-agent premain");
    }
}

FileTransformer 公共类MyFirstFileTransformer实现ClassFileTransformer {

    public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined,
            ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
    System.out.println("entering trsnform");
    ClassReader cr = new ClassReader(classfileBuffer);
    System.out.println("entering ClassReader");
    ClassWriter cw = new ClassWriter(cr, 0);
    System.out.println("ClassWriter");
    System.out.println("??? what?");
    return cw.toByteArray();
    }
}

简单就是这样。我已经通过maven制作了一个jar文件。

我的test课程处于不同的项目中。

public class ComplicatedObject {

    public static void main(String[] args) throws InterruptedException {
        Thread.sleep(1000);
        System.out.println("main");
    }
}

输出很有趣。

first-agent premain
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
entering trsnform
main
entering trsnform
entering trsnform

我在同一个项目中测试了我的代码。它工作正常但分成两部分后。它不起作用。请帮忙!!

0 个答案:

没有答案