Better FileUtil#findClass

Issue #727 's CME roots come from FileUtil. This can be due to the previous
implementation having 2 lists linking each other. This is removing 1 of the
lists and handling on-the-go while also cleaning up the code a bit.

Oh, and for the CloudExpansionManager change: I put it on the wrong
spot the first time :P so yea
This commit is contained in:
Ivan Pekov 2021-10-28 19:55:52 +03:00
parent e0b18faf1d
commit f1de7d058a
No known key found for this signature in database
GPG Key ID: E44CE4557A5E12E0
2 changed files with 24 additions and 25 deletions

View File

@ -104,6 +104,10 @@ public final class CloudExpansionManager {
public void kill() {
clean();
ASYNC_EXECUTOR.shutdown();
try {
ASYNC_EXECUTOR.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException ignored) {}
}
@NotNull
@ -167,10 +171,6 @@ public final class CloudExpansionManager {
await.values().forEach(future -> future.cancel(true));
await.clear();
ASYNC_EXECUTOR.shutdown();
try {
ASYNC_EXECUTOR.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException ignored) {}
}
public void fetch(final boolean allowUnverified) {

View File

@ -28,9 +28,11 @@ import java.io.IOException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarInputStream;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
public class FileUtil {
@ -41,37 +43,34 @@ public class FileUtil {
return null;
}
final URL jar = file.toURI().toURL();
final URLClassLoader loader = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader());
final List<String> matches = new ArrayList<>();
final List<Class<? extends T>> classes = new ArrayList<>();
try (final JarInputStream stream = new JarInputStream(jar.openStream())) {
JarEntry entry;
while ((entry = stream.getNextJarEntry()) != null) {
final String name = entry.getName();
if (name.isEmpty() || !name.endsWith(".class")) {
JarFile jar = new JarFile(file);
Enumeration<? extends ZipEntry> entries = jar.entries();
List<Class<? extends T>> classes = new ArrayList<>();
try (URLClassLoader loader =
new URLClassLoader(new URL[] {file.toURI().toURL()}, clazz.getClassLoader())) {
while (entries.hasMoreElements()) {
ZipEntry zip = entries.nextElement();
JarEntry entry = jar.getJarEntry(zip.getName());
if (entry == null) {
continue;
}
matches.add(name.substring(0, name.lastIndexOf('.')).replace('/', '.'));
String name = entry.getName();
if (!name.endsWith(".class")) {
continue;
}
for (final String match : matches) {
name = name.substring(0, name.indexOf('.')).replace('/', '.');
try {
final Class<?> loaded = loader.loadClass(match);
Class<?> loaded = loader.loadClass(name);
if (clazz.isAssignableFrom(loaded)) {
classes.add(loaded.asSubclass(clazz));
}
} catch (final NoClassDefFoundError ignored) {
} catch (NoClassDefFoundError ignored) {
}
}
}
if (classes.isEmpty()) {
loader.close();
return null;
}
return classes.get(0);
}
}