Merge pull request #18 from FrankHeijden/bug/fix-javapluginloader

Fix NoSuchFieldException on JavaPluginLoader#classes
This commit is contained in:
Frank van der Heijden 2021-05-19 23:55:45 +02:00 committed by GitHub
commit f844a962a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,10 +3,14 @@ package net.frankheijden.serverutils.bukkit.reflection;
import dev.frankheijden.minecraftreflection.MinecraftReflection; import dev.frankheijden.minecraftreflection.MinecraftReflection;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import dev.frankheijden.minecraftreflection.exceptions.MinecraftReflectionException;
import org.bukkit.plugin.java.JavaPluginLoader; import org.bukkit.plugin.java.JavaPluginLoader;
public class RJavaPluginLoader { public class RJavaPluginLoader {
private RJavaPluginLoader() {}
private static final MinecraftReflection reflection = MinecraftReflection.of(JavaPluginLoader.class); private static final MinecraftReflection reflection = MinecraftReflection.of(JavaPluginLoader.class);
public static MinecraftReflection getReflection() { public static MinecraftReflection getReflection() {
@ -19,11 +23,37 @@ public class RJavaPluginLoader {
* @param list The list of classpaths. * @param list The list of classpaths.
*/ */
public static void removeClasses(Object instance, Collection<? extends String> list) { public static void removeClasses(Object instance, Collection<? extends String> list) {
Map<String, Class<?>> classes = reflection.get(instance, "classes"); Map<String, Class<?>> classes = getFieldIfExists(instance, "classes");
if (classes == null) return; if (classes != null) {
for (String key : list) {
classes.remove(key);
}
}
for (String key : list) { Map<String, Integer> classLoadLockCount = getFieldIfExists(instance, "classLoadLockCount");
classes.remove(key); 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;
}
} }
} }
} }