updated fileutil to load *vomit* all classes, closes #381

This commit is contained in:
Sxtanna 2020-07-27 11:36:25 -04:00
parent 1993b2cd82
commit 134086f6a9

View File

@ -27,6 +27,8 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URL; import java.net.URL;
import java.net.URLClassLoader; import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarEntry; import java.util.jar.JarEntry;
import java.util.jar.JarInputStream; import java.util.jar.JarInputStream;
@ -43,7 +45,11 @@ public class FileUtil
final URL jar = file.toURI().toURL(); final URL jar = file.toURI().toURL();
try (final URLClassLoader loader = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader()); final JarInputStream stream = new JarInputStream(jar.openStream())) final List<String> matches = new ArrayList<>();
final List<Class<? extends T>> classes = new ArrayList<>();
try (final JarInputStream stream = new JarInputStream(jar.openStream()); final URLClassLoader loader = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader()))
{ {
JarEntry entry; JarEntry entry;
while ((entry = stream.getNextJarEntry()) != null) while ((entry = stream.getNextJarEntry()) != null)
@ -54,21 +60,25 @@ public class FileUtil
continue; continue;
} }
matches.add(name.substring(0, name.lastIndexOf('.')).replace('/', '.'));
}
for (final String match : matches)
{
try try
{ {
final Class<?> loaded = loader.loadClass(name.substring(0, name.lastIndexOf('.')).replace('/', '.')); final Class<?> loaded = loader.loadClass(match);
if (clazz.isAssignableFrom(loaded)) if (clazz.isAssignableFrom(loaded))
{ {
return loaded.asSubclass(clazz); classes.add(loaded.asSubclass(clazz));
} }
} }
catch (final NoClassDefFoundError ignored) catch (final NoClassDefFoundError ignored)
{ { }
}
} }
} }
return null; return classes.isEmpty() ? null : classes.get(0);
} }
} }