This commit is contained in:
Anuken
2021-06-12 08:44:16 -04:00
parent cb0382a15b
commit c5261cf2e5
2 changed files with 11 additions and 6 deletions

View File

@@ -4,7 +4,12 @@ import arc.struct.*;
public class ModClassLoader extends ClassLoader{
private Seq<ClassLoader> children = new Seq<>();
private volatile boolean inChild = false;
private ThreadLocal<Boolean> inChild = new ThreadLocal<>(){
@Override
protected Boolean initialValue(){
return Boolean.FALSE;
}
};
public void addChild(ClassLoader child){
children.add(child);
@@ -13,15 +18,15 @@ public class ModClassLoader extends ClassLoader{
@Override
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){
inChild = false;
if(inChild.get()){
inChild.set(false);
throw new ClassNotFoundException(name);
}
ClassNotFoundException last = null;
int size = children.size;
inChild = true;
inChild.set(true);
//if it doesn't exist in the main class loader, try all the children
for(int i = 0; i < size; i++){
try{
@@ -30,7 +35,7 @@ public class ModClassLoader extends ClassLoader{
last = e;
}
}
inChild = false;
inChild.set(false);
throw (last == null ? new ClassNotFoundException(name) : last);
}