Commits

Thinkofname authored e80c39fa690
Allow for easier plugin debugging from an IDE
No tags

src/main/java/org/bukkit/plugin/java/PluginClassLoader.java

Modified
1 1 package org.bukkit.plugin.java;
2 2
3 3 import java.io.File;
4 +import java.lang.reflect.InvocationTargetException;
4 5 import java.net.MalformedURLException;
5 6 import java.net.URL;
6 7 import java.net.URLClassLoader;
7 8 import java.util.HashMap;
8 9 import java.util.Map;
9 10 import java.util.Set;
11 +import java.util.logging.Level;
10 12
13 +import com.google.common.base.Throwables;
11 14 import org.apache.commons.lang.Validate;
15 +import org.bukkit.Bukkit;
12 16 import org.bukkit.plugin.InvalidPluginException;
13 17 import org.bukkit.plugin.PluginDescriptionFile;
14 18
15 19 /**
16 20 * A ClassLoader for plugins, to allow shared classes across multiple plugins
17 21 */
18 22 final class PluginClassLoader extends URLClassLoader {
19 23 private final JavaPluginLoader loader;
20 24 private final Map<String, Class<?>> classes = new HashMap<String, Class<?>>();
21 25 private final PluginDescriptionFile description;
42 46 throw new InvalidPluginException("Cannot find main class `" + description.getMain() + "'", ex);
43 47 }
44 48
45 49 Class<? extends JavaPlugin> pluginClass;
46 50 try {
47 51 pluginClass = jarClass.asSubclass(JavaPlugin.class);
48 52 } catch (ClassCastException ex) {
49 53 throw new InvalidPluginException("main class `" + description.getMain() + "' does not extend JavaPlugin", ex);
50 54 }
51 55
52 - plugin = pluginClass.newInstance();
56 + if (pluginClass.getClassLoader() == this) {
57 + plugin = pluginClass.newInstance();
58 + } else {
59 + loader.server.getLogger().log(Level.WARNING, "Trying to load {0} for debugging", description.getName());
60 + plugin = pluginClass.getConstructor(
61 + JavaPluginLoader.class, PluginDescriptionFile.class, File.class, File.class
62 + ).newInstance(loader, description, dataFolder, file);
63 + }
53 64 } catch (IllegalAccessException ex) {
54 65 throw new InvalidPluginException("No public constructor", ex);
55 66 } catch (InstantiationException ex) {
56 67 throw new InvalidPluginException("Abnormal plugin type", ex);
68 + } catch (NoSuchMethodException ex) {
69 + throw new InvalidPluginException("Missing constructor", ex);
70 + } catch (InvocationTargetException ex) {
71 + throw new InvalidPluginException(ex);
57 72 }
58 73 }
59 74
60 75 @Override
61 76 protected Class<?> findClass(String name) throws ClassNotFoundException {
62 77 return findClass(name, true);
63 78 }
64 79
65 80 Class<?> findClass(String name, boolean checkGlobal) throws ClassNotFoundException {
66 81 if (name.startsWith("org.bukkit.") || name.startsWith("net.minecraft.")) {

Everything looks good. We'll let you know here if there's anything you should know about.

Add shortcut