summaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorWesley Wolfe <weswolf@aol.com>2013-12-24 22:20:20 -0600
committerWesley Wolfe <weswolf@aol.com>2013-12-24 22:20:20 -0600
commitaa4f9093241e64d6dd19d9f7cdbdeb6617443797 (patch)
tree3b6a7e0e61c914b4bd3233a5f71f06bb367506c9 /src/main
parentf8984c4486582ee5794f8dda8003d7aca6bf5be5 (diff)
downloadbukkit-aa4f9093241e64d6dd19d9f7cdbdeb6617443797.tar
bukkit-aa4f9093241e64d6dd19d9f7cdbdeb6617443797.tar.gz
bukkit-aa4f9093241e64d6dd19d9f7cdbdeb6617443797.tar.lz
bukkit-aa4f9093241e64d6dd19d9f7cdbdeb6617443797.tar.xz
bukkit-aa4f9093241e64d6dd19d9f7cdbdeb6617443797.zip
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.
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/bukkit/plugin/java/JavaPlugin.java56
1 files changed, 56 insertions, 0 deletions
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.
+ * <p>
+ * 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
+ * <code>JavaPlugin.getPlugin(JavaPlugin.class)</code>
+ * @throws ClassCastException if plugin that provided the class does not
+ * extend the class
+ */
+ public static <T extends JavaPlugin> T getPlugin(Class<T> 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;
+ }
}