Child-first mod class loading

This commit is contained in:
Anuken
2021-06-09 10:04:23 -04:00
parent a8a163ebe2
commit adf6465029
3 changed files with 59 additions and 21 deletions

View File

@@ -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);
}
}