正如 Pete 所提到的,这可以使用 ASM 字节码库来完成。实际上,该库实际上附带了一个专门用于处理这些类名重新映射的类(RemappingClassAdapter
)。下面是使用此类的类装入器的示例:
public class MagicClassLoader extends ClassLoader {
private final String defaultPackageName;
public MagicClassLoader(String defaultPackageName) {
super();
this.defaultPackageName = defaultPackageName;
}
public MagicClassLoader(String defaultPackageName, ClassLoader parent) {
super(parent);
this.defaultPackageName = defaultPackageName;
}
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
byte[] bytecode = ...; // I will leave this part up to you
byte[] remappedBytecode;
try {
remappedBytecode = rewriteDefaultPackageClassNames(bytecode);
} catch (IOException e) {
throw new RuntimeException("Could not rewrite class " + name);
}
return defineClass(name, remappedBytecode, 0, remappedBytecode.length);
}
public byte[] rewriteDefaultPackageClassNames(byte[] bytecode) throws IOException {
ClassReader classReader = new ClassReader(bytecode);
ClassWriter classWriter = new ClassWriter(classReader, 0);
Remapper remapper = new DefaultPackageClassNameRemapper();
classReader.accept(
new RemappingClassAdapter(classWriter, remapper),
0
);
return classWriter.toByteArray();
}
class DefaultPackageClassNameRemapper extends Remapper {
@Override
public String map(String typeName) {
boolean hasPackageName = typeName.indexOf('.') != -1;
if (hasPackageName) {
return typeName;
} else {
return defaultPackageName + "." + typeName;
}
}
}
}
为了说明这一点,我创建了两个类,它们都属于默认包:
public class Customer {
}
和
public class Order {
private Customer customer;
public Order(Customer customer) {
this.customer = customer;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
}
这是任何重新映射之前的列表:Order
> javap -private -c Order
Compiled from "Order.java"
public class Order extends java.lang.Object{
private Customer customer;
public Order(Customer);
Code:
0: aload_0
1: invokespecial #10; //Method java/lang/Object."":()V
4: aload_0
5: aload_1
6: putfield #13; //Field customer:LCustomer;
9: return
public Customer getCustomer();
Code:
0: aload_0
1: getfield #13; //Field customer:LCustomer;
4: areturn
public void setCustomer(Customer);
Code:
0: aload_0
1: aload_1
2: putfield #13; //Field customer:LCustomer;
5: return
}
这是重新映射后的列表(用作默认包):Order
com.mycompany
> javap -private -c Order
Compiled from "Order.java"
public class com.mycompany.Order extends com.mycompany.java.lang.Object{
private com.mycompany.Customer customer;
public com.mycompany.Order(com.mycompany.Customer);
Code:
0: aload_0
1: invokespecial #30; //Method "com.mycompany.java/lang/Object"."":()V
4: aload_0
5: aload_1
6: putfield #32; //Field customer:Lcom.mycompany.Customer;
9: return
public com.mycompany.Customer getCustomer();
Code:
0: aload_0
1: getfield #32; //Field customer:Lcom.mycompany.Customer;
4: areturn
public void setCustomer(com.mycompany.Customer);
Code:
0: aload_0
1: aload_1
2: putfield #32; //Field customer:Lcom.mycompany.Customer;
5: return
}
如您所见,重新映射已更改了对 的所有引用和对 的所有引用。Order
com.mycompany.Order
Customer
com.mycompany.Customer
此类装入器必须装入以下所有类: