From a3d78f4d0e03ed4824ca55b7ca0e0e0a02595458 Mon Sep 17 00:00:00 2001 From: Luck Date: Sun, 17 Sep 2017 19:30:43 +0100 Subject: [PATCH] Always load dependencies into the plugin classloader, and not it's parent - fixes #479 --- .../main/java/me/lucko/luckperms/api/Log.java | 2 +- .../dependencies/DependencyManager.java | 33 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/api/src/main/java/me/lucko/luckperms/api/Log.java b/api/src/main/java/me/lucko/luckperms/api/Log.java index f1298204..1bb0ce07 100644 --- a/api/src/main/java/me/lucko/luckperms/api/Log.java +++ b/api/src/main/java/me/lucko/luckperms/api/Log.java @@ -39,7 +39,7 @@ import javax.annotation.Nonnull; *

Any changes made to log entries will only apply to this instance of the log. * You can add to the log using the {@link Storage}, and then request an updated copy.

* - *

All methods are thread safe, and return immutable & thread safe collections.

+ *

All methods are thread safe, and return immutable and thread safe collections.

*/ public interface Log { diff --git a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java index b981178b..a841612e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java +++ b/common/src/main/java/me/lucko/luckperms/common/dependencies/DependencyManager.java @@ -31,14 +31,15 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; -import me.lucko.luckperms.api.PlatformType; import me.lucko.luckperms.common.config.ConfigKeys; import me.lucko.luckperms.common.plugin.LuckPermsPlugin; import me.lucko.luckperms.common.storage.StorageType; import java.io.File; import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; import java.nio.file.Files; @@ -49,14 +50,17 @@ import java.util.Set; @UtilityClass public class DependencyManager { - private static Method ADD_URL_METHOD; + private static final Method ADD_URL_METHOD; + static { + Method addUrlMethod = null; try { - ADD_URL_METHOD = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); - ADD_URL_METHOD.setAccessible(true); + addUrlMethod = URLClassLoader.class.getDeclaredMethod("addURL", URL.class); + addUrlMethod.setAccessible(true); } catch (NoSuchMethodException e) { e.printStackTrace(); } + ADD_URL_METHOD = addUrlMethod; } public static final Map> STORAGE_DEPENDENCIES = ImmutableMap.>builder() @@ -104,9 +108,9 @@ public class DependencyManager { for (Map.Entry e : toLoad) { try { loadJar(plugin, e.getValue()); - } catch (Throwable e1) { + } catch (Throwable t) { plugin.getLog().severe("Failed to load jar for dependency " + e.getKey().name()); - e1.printStackTrace(); + t.printStackTrace(); } } } @@ -134,14 +138,19 @@ public class DependencyManager { } } - private static void loadJar(LuckPermsPlugin plugin, File file) throws Exception { - URLClassLoader classLoader = (URLClassLoader) plugin.getClass().getClassLoader(); + private static void loadJar(LuckPermsPlugin plugin, File file) { + // get the classloader to load into + ClassLoader classLoader = plugin.getClass().getClassLoader(); - if (plugin.getServerType() != PlatformType.SPONGE && !plugin.getServerName().equals("KCauldron") && !plugin.getServerName().equals("Uranium")) { - classLoader = (URLClassLoader) classLoader.getParent(); + if (classLoader instanceof URLClassLoader) { + try { + ADD_URL_METHOD.invoke(classLoader, file.toURI().toURL()); + } catch (IllegalAccessException | InvocationTargetException | MalformedURLException e) { + throw new RuntimeException("Unable to invoke URLClassLoader#addURL", e); + } + } else { + throw new RuntimeException("Unknown classloader: " + classLoader.getClass()); } - - ADD_URL_METHOD.invoke(classLoader, file.toURI().toURL()); } }