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
Order
before any re-mapping:This is the listing of
Order
after remapping (usingcom.mycompany
as the default package):As you can see, the remapping has changed all
Order
references tocom.mycompany.Order
and allCustomer
references tocom.mycompany.Customer
.This class loader would have to load all classes that either:
You should be able to knock something up with ASM, though it would be easier to do the package rename once at build time rather than at load time.
Maybe it would be easier to move the API from the default package into a more reasonable spot? It sounds like you don't have access to the source code. I am not sure if the package is encoded into class files, so simply moving the API class might be worth a try. Otherwise, Java decompilers like JAD usually do a good job, so you could change the package name in the decompiled source and compile it again.