From 89e4f83cd41117cc8a0264e70049f2f6e490740a Mon Sep 17 00:00:00 2001 From: md_5 Date: Fri, 26 Aug 2016 15:37:26 +1000 Subject: SPIGOT-2629: Better thread safety for plugin class loading --- .../org/bukkit/plugin/java/JavaPluginLoader.java | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java index 21187717..3bacd09c 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPluginLoader.java @@ -7,6 +7,7 @@ import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; @@ -48,7 +49,7 @@ public final class JavaPluginLoader implements PluginLoader { final Server server; private final Pattern[] fileFilters = new Pattern[] { Pattern.compile("\\.jar$"), }; private final Map> classes = new HashMap>(); - private final Map loaders = new LinkedHashMap(); + private final Map loaders = Collections.synchronizedMap(new LinkedHashMap()); /** * This class was not meant to be constructed explicitly @@ -114,9 +115,6 @@ public final class JavaPluginLoader implements PluginLoader { } for (final String pluginName : description.getDepend()) { - if (loaders == null) { - throw new UnknownDependencyException(pluginName); - } PluginClassLoader current = loaders.get(pluginName); if (current == null) { @@ -186,14 +184,14 @@ public final class JavaPluginLoader implements PluginLoader { if (cachedClass != null) { return cachedClass; } else { - for (String current : loaders.keySet()) { - PluginClassLoader loader = loaders.get(current); - - try { - cachedClass = loader.findClass(name, false); - } catch (ClassNotFoundException cnfe) {} - if (cachedClass != null) { - return cachedClass; + synchronized (loaders) { + for (PluginClassLoader loader : loaders.values()) { + try { + cachedClass = loader.findClass(name, false); + } catch (ClassNotFoundException cnfe) {} + if (cachedClass != null) { + return cachedClass; + } } } } -- cgit v1.2.3