From 134086f6a952ed15b7fe682a4048033d14360142 Mon Sep 17 00:00:00 2001 From: Sxtanna Date: Mon, 27 Jul 2020 11:36:25 -0400 Subject: [PATCH] updated fileutil to load *vomit* all classes, closes #381 --- .../me/clip/placeholderapi/util/FileUtil.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/me/clip/placeholderapi/util/FileUtil.java b/src/main/java/me/clip/placeholderapi/util/FileUtil.java index ab5f3b5..81fd690 100644 --- a/src/main/java/me/clip/placeholderapi/util/FileUtil.java +++ b/src/main/java/me/clip/placeholderapi/util/FileUtil.java @@ -27,6 +27,8 @@ import java.io.File; import java.io.IOException; import java.net.URL; import java.net.URLClassLoader; +import java.util.ArrayList; +import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarInputStream; @@ -43,7 +45,11 @@ public class FileUtil 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 matches = new ArrayList<>(); + final List> classes = new ArrayList<>(); + + + try (final JarInputStream stream = new JarInputStream(jar.openStream()); final URLClassLoader loader = new URLClassLoader(new URL[]{jar}, clazz.getClassLoader())) { JarEntry entry; while ((entry = stream.getNextJarEntry()) != null) @@ -54,21 +60,25 @@ public class FileUtil continue; } + matches.add(name.substring(0, name.lastIndexOf('.')).replace('/', '.')); + } + + for (final String match : matches) + { try { - final Class loaded = loader.loadClass(name.substring(0, name.lastIndexOf('.')).replace('/', '.')); + final Class loaded = loader.loadClass(match); if (clazz.isAssignableFrom(loaded)) { - return loaded.asSubclass(clazz); + classes.add(loaded.asSubclass(clazz)); } } catch (final NoClassDefFoundError ignored) - { - } + { } } } - return null; + return classes.isEmpty() ? null : classes.get(0); } }