如果您的类加载器正确实现,它将首先向其父类加载器询问应加载的任何类.
您的加载器的父类加载器可能是通常的应用程序类加载器.这意味着您的类加载器加载的每个类将首先在应用程序类加载器上搜索,并且只有在您找不到的时候才会搜索.
您的类加载器定义的所有类也将在类加载器上搜索所需的类.如果他们不这样做,你的ClassA并不是由你的装载机加载的.
如果这没有帮助,您需要显示一些有关如何获得结果的代码.
关于做什么的想法:
class ModifyingClassLoader extends URLClassLoader {
// TODO: add constructors
private boolean needsModifying(String name) {
// TODO
}
private byte[] modifyClass(InputStream original) throws IOException {
// TODO
}
public Class> findClass(String name) throws {
if(needsModifying(name)) {
try {
InputStream classData = getResourceAsStream(name.replace('.','/') + ".class");
if(classData == null) {
throw new ClassNotFoundException("class " + name + " is not findable");
}
byte[] array = modifyClass(classData);
return defineClass(name,array,array.length);
}
catch(IOException io) {
throw new ClassNotFoundException(io);
}
}
else {
return super.findClass(name);
}
}
}
对你的问题:
Is there a way to tell the parent class loader to find and define the class,
BUT set the Class’s context class loader to your custom one?
不,类的ClassLoader总是一个被调用来创建类的defineClass方法的类. (context class loader是别的 – 它是线程特定的,只有明确想要使用它的类才使用,而不是通过解析自己的直接依赖关系的类来使用).