13
0
geforkt von Mirrors/Paper

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.

By: Wesley Wolfe <weswolf@aol.com>
Dieser Commit ist enthalten in:
Bukkit/Spigot 2013-12-24 22:20:20 -06:00
Ursprung ea7c13e03d
Commit 216df9a3d5

Datei anzeigen

@ -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;
}
}