Child-first mod class loading
This commit is contained in:
@@ -11,25 +11,23 @@ public class ModClassLoader extends ClassLoader{
|
||||
}
|
||||
|
||||
@Override
|
||||
protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException{
|
||||
//always try the superclass first
|
||||
try{
|
||||
return super.loadClass(name, resolve);
|
||||
}catch(ClassNotFoundException error){
|
||||
//a child may try to delegate class loading to its parent, which is *this class loader* - do not let that happen
|
||||
if(inChild) throw error;
|
||||
int size = children.size;
|
||||
//if it doesn't exist in the main class loader, try all the children
|
||||
for(int i = 0; i < size; i++){
|
||||
try{
|
||||
inChild = true;
|
||||
var out = children.get(i).loadClass(name);
|
||||
inChild = false;
|
||||
return out;
|
||||
}catch(ClassNotFoundException ignored){
|
||||
}
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException{
|
||||
//a child may try to delegate class loading to its parent, which is *this class loader* - do not let that happen
|
||||
if(inChild) throw new ClassNotFoundException(name);
|
||||
|
||||
ClassNotFoundException last = null;
|
||||
int size = children.size;
|
||||
//if it doesn't exist in the main class loader, try all the children
|
||||
for(int i = 0; i < size; i++){
|
||||
try{
|
||||
inChild = true;
|
||||
var out = children.get(i).loadClass(name);
|
||||
inChild = false;
|
||||
return out;
|
||||
}catch(ClassNotFoundException e){
|
||||
last = e;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
throw (last == null ? new ClassNotFoundException(name) : last);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user