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