summaryrefslogtreecommitdiffstats
path: root/src/main/java/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org')
-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;
+ }
}