Is it possible to load a class in Java and ‘fake’ the package name/canonical name of a class? I tried doing this, the obvious way, but I get a “class name doesn’t match” message in a ClassDefNotFoundException.
The reason I’m doing this is I’m trying to load an API that was written in the default package so that I can use it directly without using reflection. The code will compile against the class in a folder structure representing the package and a package name import. ie:
./com/DefaultPackageClass.class
// ...
import com.DefaultPackageClass;
import java.util.Vector;
// ...
My current code is as follows:
public Class loadClass(String name) throws ClassNotFoundException {
if(!CLASS_NAME.equals(name))
return super.loadClass(name);
try {
URL myUrl = new URL(fileUrl);
URLConnection connection = myUrl.openConnection();
InputStream input = connection.getInputStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int data = input.read();
while(data != -1){
buffer.write(data);
data = input.read();
}
input.close();
byte[] classData = buffer.toByteArray();
return defineClass(CLASS_NAME,
classData, 0, classData.length);
} catch (MalformedURLException e) {
throw new UndeclaredThrowableException(e);
} catch (IOException e) {
throw new UndeclaredThrowableException(e);
}
}
As Pete mentioned, this can be done using the ASM bytecode library. In fact, that library actually ships with a class specifically for handling these class name re-mappings (
RemappingClassAdapter). Here is an example of a class loader using this class:To illustrate, I created two classes, both of which belong to the default package:
and
This is the listing of
Orderbefore any re-mapping:> 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 }This is the listing of
Orderafter remapping (usingcom.mycompanyas the default package):> 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 }As you can see, the remapping has changed all
Orderreferences tocom.mycompany.Orderand allCustomerreferences tocom.mycompany.Customer.This class loader would have to load all classes that either: