v1.4.2 - Call PluginClassLoader through reflection
This commit is contained in:
parent
ac6de28b68
commit
74f5f4c4f1
3 changed files with 22 additions and 25 deletions
|
|
@ -5,17 +5,13 @@ import net.frankheijden.serverutils.reflection.*;
|
|||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.*;
|
||||
import org.bukkit.plugin.*;
|
||||
import org.bukkit.plugin.java.PluginClassLoader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.set;
|
||||
|
||||
public class PluginManager {
|
||||
|
||||
public static LoadResult loadPlugin(String jarFile) {
|
||||
|
|
@ -78,7 +74,7 @@ public class PluginManager {
|
|||
try {
|
||||
RSimplePluginManager.getPlugins(Bukkit.getPluginManager()).remove(plugin);
|
||||
RSimplePluginManager.removeLookupName(Bukkit.getPluginManager(), plugin.getName());
|
||||
clearClassLoader(RJavaPlugin.getClassLoader(plugin));
|
||||
RPluginClassLoader.clearClassLoader(RJavaPlugin.getClassLoader(plugin));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return Result.ERROR;
|
||||
|
|
@ -86,20 +82,6 @@ public class PluginManager {
|
|||
return Result.SUCCESS;
|
||||
}
|
||||
|
||||
public static void clearClassLoader(ClassLoader loader) throws IllegalAccessException, IOException {
|
||||
if (loader == null) return;
|
||||
if (loader instanceof PluginClassLoader) {
|
||||
clearClassLoader((PluginClassLoader) loader);
|
||||
}
|
||||
}
|
||||
|
||||
public static void clearClassLoader(PluginClassLoader loader) throws IllegalAccessException, IOException {
|
||||
if (loader == null) return;
|
||||
set(RPluginClassLoader.getFields(), loader, "plugin", null);
|
||||
set(RPluginClassLoader.getFields(), loader, "pluginInit", null);
|
||||
loader.close();
|
||||
}
|
||||
|
||||
public static Result enablePlugin(String pluginName) {
|
||||
return enablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
package net.frankheijden.serverutils.reflection;
|
||||
|
||||
import org.bukkit.plugin.java.PluginClassLoader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.*;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf;
|
||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.VersionParam.ALL_VERSIONS;
|
||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields;
|
||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.set;
|
||||
|
||||
public class RPluginClassLoader {
|
||||
|
||||
|
|
@ -16,7 +17,7 @@ public class RPluginClassLoader {
|
|||
|
||||
static {
|
||||
try {
|
||||
pluginClassLoaderClass = PluginClassLoader.class;
|
||||
pluginClassLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
|
||||
fields = getAllFields(pluginClassLoaderClass,
|
||||
fieldOf("plugin", ALL_VERSIONS),
|
||||
fieldOf("pluginInit", ALL_VERSIONS));
|
||||
|
|
@ -25,7 +26,21 @@ public class RPluginClassLoader {
|
|||
}
|
||||
}
|
||||
|
||||
public static Map<String, Field> getFields() {
|
||||
return fields;
|
||||
public static boolean isInstance(Object obj) {
|
||||
return pluginClassLoaderClass.isInstance(obj);
|
||||
}
|
||||
|
||||
public static void clearClassLoader(ClassLoader loader) throws IOException, IllegalAccessException {
|
||||
if (loader == null) return;
|
||||
if (isInstance(loader)) {
|
||||
clearURLClassLoader((URLClassLoader) loader);
|
||||
}
|
||||
}
|
||||
|
||||
public static void clearURLClassLoader(URLClassLoader loader) throws IllegalAccessException, IOException {
|
||||
if (loader == null) return;
|
||||
set(fields, loader, "plugin", null);
|
||||
set(fields, loader, "pluginInit", null);
|
||||
loader.close();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue