From aa4f9093241e64d6dd19d9f7cdbdeb6617443797 Mon Sep 17 00:00:00 2001 From: Wesley Wolfe Date: Tue, 24 Dec 2013 22:20:20 -0600 Subject: Add method to get plugin by its class. Adds BUKKIT-5240 Currently, the only way to get a plugin is by name or using a static variable. This adds two methods to get a plugin based on its classes, utilizing the plugin classloader. --- .../java/org/bukkit/plugin/java/JavaPlugin.java | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'src/main') diff --git a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java index bdecbea2..8fd726bd 100644 --- a/src/main/java/org/bukkit/plugin/java/JavaPlugin.java +++ b/src/main/java/org/bukkit/plugin/java/JavaPlugin.java @@ -12,6 +12,7 @@ import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import org.apache.commons.lang.Validate; import org.bukkit.Server; import org.bukkit.Warning.WarningState; import org.bukkit.command.Command; @@ -405,4 +406,59 @@ public abstract class JavaPlugin extends PluginBase { public String toString() { return description.getFullName(); } + + /** + * This method provides fast access to the plugin that has {@link + * #getProvidingPlugin(Class) provided} the given plugin class, which is + * usually the plugin that implemented it. + *

+ * An exception to this would be if plugin's jar that contained the class + * does not extend the class, where the intended plugin would have + * resided in a different jar / classloader. + * + * @param clazz the class desired + * @return the plugin that provides and implements said class, or null + * if called from an inconsistent static initialization block + * @throws IllegalArgumentException if clazz is null + * @throws IllegalArgumentException if clazz does not extend {@link + * JavaPlugin} + * @throws IllegalArgumentException if clazz was not provided by a + * plugin, for example, if called with + * JavaPlugin.getPlugin(JavaPlugin.class) + * @throws ClassCastException if plugin that provided the class does not + * extend the class + */ + public static T getPlugin(Class clazz) { + Validate.notNull(clazz, "Null class cannot have a plugin"); + if (!JavaPlugin.class.isAssignableFrom(clazz)) { + throw new IllegalArgumentException(clazz + " does not extend " + JavaPlugin.class); + } + final ClassLoader cl = clazz.getClassLoader(); + if (!(cl instanceof PluginClassLoader)) { + throw new IllegalArgumentException(clazz + " is not initialized by " + PluginClassLoader.class); + } + JavaPlugin plugin = ((PluginClassLoader) cl).plugin; + if (plugin == null) { + throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer"); + } + return clazz.cast(plugin); + } + + /** + * This method provides fast access to the plugin that has {@link + * #getProvidingPlugin(Class) provided} the given plugin class, which is + * usually the plugin that implemented it. + */ + public static JavaPlugin getProvidingPlugin(Class clazz) { + Validate.notNull(clazz, "Null class cannot have a plugin"); + final ClassLoader cl = clazz.getClassLoader(); + if (!(cl instanceof PluginClassLoader)) { + throw new IllegalArgumentException(clazz + " is not provided by " + PluginClassLoader.class); + } + JavaPlugin plugin = ((PluginClassLoader) cl).plugin; + if (plugin == null) { + throw new IllegalStateException("Cannot get plugin for " + clazz + " from a static initializer"); + } + return plugin; + } } -- cgit v1.2.3