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,7 +5,7 @@ plugins {
|
||||||
|
|
||||||
String packagePath = 'net.frankheijden.serverutils'
|
String packagePath = 'net.frankheijden.serverutils'
|
||||||
group = packagePath
|
group = packagePath
|
||||||
version '1.4.1'
|
version '1.4.2'
|
||||||
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
|
|
|
||||||
|
|
@ -5,17 +5,13 @@ import net.frankheijden.serverutils.reflection.*;
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.*;
|
import org.bukkit.command.*;
|
||||||
import org.bukkit.plugin.*;
|
import org.bukkit.plugin.*;
|
||||||
import org.bukkit.plugin.java.PluginClassLoader;
|
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.set;
|
|
||||||
|
|
||||||
public class PluginManager {
|
public class PluginManager {
|
||||||
|
|
||||||
public static LoadResult loadPlugin(String jarFile) {
|
public static LoadResult loadPlugin(String jarFile) {
|
||||||
|
|
@ -78,7 +74,7 @@ public class PluginManager {
|
||||||
try {
|
try {
|
||||||
RSimplePluginManager.getPlugins(Bukkit.getPluginManager()).remove(plugin);
|
RSimplePluginManager.getPlugins(Bukkit.getPluginManager()).remove(plugin);
|
||||||
RSimplePluginManager.removeLookupName(Bukkit.getPluginManager(), plugin.getName());
|
RSimplePluginManager.removeLookupName(Bukkit.getPluginManager(), plugin.getName());
|
||||||
clearClassLoader(RJavaPlugin.getClassLoader(plugin));
|
RPluginClassLoader.clearClassLoader(RJavaPlugin.getClassLoader(plugin));
|
||||||
} catch (Exception ex) {
|
} catch (Exception ex) {
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
return Result.ERROR;
|
return Result.ERROR;
|
||||||
|
|
@ -86,20 +82,6 @@ public class PluginManager {
|
||||||
return Result.SUCCESS;
|
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) {
|
public static Result enablePlugin(String pluginName) {
|
||||||
return enablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
return enablePlugin(Bukkit.getPluginManager().getPlugin(pluginName));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,14 @@
|
||||||
package net.frankheijden.serverutils.reflection;
|
package net.frankheijden.serverutils.reflection;
|
||||||
|
|
||||||
import org.bukkit.plugin.java.PluginClassLoader;
|
import java.io.IOException;
|
||||||
|
|
||||||
import java.lang.reflect.*;
|
import java.lang.reflect.*;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.FieldParam.fieldOf;
|
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.VersionParam.ALL_VERSIONS;
|
||||||
import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields;
|
import static net.frankheijden.serverutils.reflection.ReflectionUtils.getAllFields;
|
||||||
|
import static net.frankheijden.serverutils.reflection.ReflectionUtils.set;
|
||||||
|
|
||||||
public class RPluginClassLoader {
|
public class RPluginClassLoader {
|
||||||
|
|
||||||
|
|
@ -16,7 +17,7 @@ public class RPluginClassLoader {
|
||||||
|
|
||||||
static {
|
static {
|
||||||
try {
|
try {
|
||||||
pluginClassLoaderClass = PluginClassLoader.class;
|
pluginClassLoaderClass = Class.forName("org.bukkit.plugin.java.PluginClassLoader");
|
||||||
fields = getAllFields(pluginClassLoaderClass,
|
fields = getAllFields(pluginClassLoaderClass,
|
||||||
fieldOf("plugin", ALL_VERSIONS),
|
fieldOf("plugin", ALL_VERSIONS),
|
||||||
fieldOf("pluginInit", ALL_VERSIONS));
|
fieldOf("pluginInit", ALL_VERSIONS));
|
||||||
|
|
@ -25,7 +26,21 @@ public class RPluginClassLoader {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Map<String, Field> getFields() {
|
public static boolean isInstance(Object obj) {
|
||||||
return fields;
|
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