Merge pull request #18 from FrankHeijden/bug/fix-javapluginloader
Fix NoSuchFieldException on JavaPluginLoader#classes
This commit is contained in:
commit
f844a962a1
1 changed files with 34 additions and 4 deletions
|
|
@ -3,10 +3,14 @@ package net.frankheijden.serverutils.bukkit.reflection;
|
|||
import dev.frankheijden.minecraftreflection.MinecraftReflection;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import dev.frankheijden.minecraftreflection.exceptions.MinecraftReflectionException;
|
||||
import org.bukkit.plugin.java.JavaPluginLoader;
|
||||
|
||||
public class RJavaPluginLoader {
|
||||
|
||||
private RJavaPluginLoader() {}
|
||||
|
||||
private static final MinecraftReflection reflection = MinecraftReflection.of(JavaPluginLoader.class);
|
||||
|
||||
public static MinecraftReflection getReflection() {
|
||||
|
|
@ -19,11 +23,37 @@ public class RJavaPluginLoader {
|
|||
* @param list The list of classpaths.
|
||||
*/
|
||||
public static void removeClasses(Object instance, Collection<? extends String> list) {
|
||||
Map<String, Class<?>> classes = reflection.get(instance, "classes");
|
||||
if (classes == null) return;
|
||||
Map<String, Class<?>> classes = getFieldIfExists(instance, "classes");
|
||||
if (classes != null) {
|
||||
for (String key : list) {
|
||||
classes.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
for (String key : list) {
|
||||
classes.remove(key);
|
||||
Map<String, Integer> classLoadLockCount = getFieldIfExists(instance, "classLoadLockCount");
|
||||
if (classLoadLockCount != null) {
|
||||
for (String key : list) {
|
||||
classLoadLockCount.remove(key);
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, ReentrantReadWriteLock> classLoadLock = getFieldIfExists(instance, "classLoadLock");
|
||||
if (classLoadLock != null) {
|
||||
for (String key : list) {
|
||||
classLoadLock.remove(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> T getFieldIfExists(Object instance, String field) {
|
||||
try {
|
||||
return reflection.get(instance, field);
|
||||
} catch (MinecraftReflectionException ex) {
|
||||
if (ex.getCause() instanceof NoSuchFieldException) {
|
||||
return null;
|
||||
} else {
|
||||
throw ex;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue