有一个简单的方法来测试这个......在每个 Transformer 类的 transform 方法中添加一个 System.out 指令,该指令将输出各个类的唯一消息。然后查看控制台上获取输出的顺序。如果您在 Transformer2 之前收到 Transformer1 的唯一消息,那么是的,转换方法是按顺序调用的。
这就是我所做的...
package Test;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.IllegalClassFormatException;
import java.lang.instrument.Instrumentation;
import java.security.ProtectionDomain;
public class TransFormerTest {
public TransFormerTest() {
super();
}
public static void premain(String agentArguments, Instrumentation instrumentation) {
instrumentation.addTransformer(new Transformer1());
instrumentation.addTransformer(new Transformer2());
}
}
class Transformer1 implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class redefiningClass, ProtectionDomain domain, byte[] bytes) throws IllegalClassFormatException {
System.out.println(className + "one"); // one for Transformer
return bytes;
}
}
class Transformer2 implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class redefiningClass, ProtectionDomain domain, byte[] bytes) throws IllegalClassFormatException {
System.out.println(className + "two"); // two for Transformer 2
return bytes;
}
}
因此,每次加载一个类并调用转换器时,您应该会看到该类的名称被打印两次,第一次是一个作为后缀,然后是两个作为后缀。
我用一个简单的 Hello World 程序测试了这个理论......这是我的输出::
Test/Transformer2 - one
sun/launcher/LauncherHelper - one
sun/launcher/LauncherHelper - two
java/lang/Enum - one
java/lang/Enum - two
HelloWorld - one
HelloWorld - two
java/lang/Void - one
java/lang/Void - two
Hello World
java/lang/Shutdown - one
java/lang/Shutdown - two
java/lang/Shutdown$Lock - one
java/lang/Shutdown$Lock - two
所以,它似乎确实维持了秩序。
话虽如此,您是否考虑过链接转换方法?例如...
public class TransFormerTest {
public TransFormerTest() {
super();
}
public static void premain(String agentArguments, Instrumentation instrumentation) {
instrumentation.addTransformer(new Transformer());
}
}
class Transformer implements ClassFileTransformer {
public byte[] transform(ClassLoader loader, String className, Class redefiningClass, ProtectionDomain domain, byte[] bytes) throws IllegalClassFormatException {
byte[] bytes2 = privateTransformer(className, bytes);
return bytes2;
}
private byte[] privateTransformer(String className, byte[] bytes) {
System.out.println(className + " - one");
// TODO add code for First Transformation.
byte[] bytes2 = privateTransformer2(className, bytes);
return bytes2;
}
private byte[] privateTransformer2(String className, byte[] bytes) {
System.out.println(className + " - two");
// TODO add code for Second Transformation.
return bytes;
}
}
这仍然会达到类似的结果,这肯定会保持转换可能发生的顺序。