Add method to get PluginClassLoader's classes map

This commit is contained in:
Mark Vainomaa 2017-01-18 03:46:07 +02:00
parent 700119edbb
commit 8b4a334873

View File

@ -4,6 +4,7 @@ import eu.mikroskeem.utils.reflect.Reflect;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.plugin.java.JavaPluginLoader;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@ -43,6 +44,7 @@ public class ServerUtils {
* @return Plugin ClassLoader
*/
@SuppressWarnings("unchecked")
@Nullable
public static ClassLoader getPluginClassLoader(JavaPlugin plugin){
/*
* Note: CB/Spigot has PluginClassLoader package-private, PaperSpigot has public
@ -83,6 +85,34 @@ public class ServerUtils {
return null;
}
/**
* Get PluginClassLoader classes map
* @param classLoader PluginClassLoader instance
* @return Classes map
* @throws RuntimeException if ClassLoader doesn't extend PluginClassLoader
*/
@SuppressWarnings("unchecked")
@Nullable
public static Map<String, Class<?>> getClassesMap(ClassLoader classLoader){
Class<?> clClass = classLoader.getClass();
try {
Class<?> pluginClassLoader = checkNotNull(Reflect.getClass(
"org.bukkit.plugin.java.PluginClassLoader"));
if(pluginClassLoader.isAssignableFrom(clClass)){
Field classesField = checkNotNull(Reflect.getField(clClass, "classes"));
return (Map<String, Class<?>>) checkNotNull(Reflect.readField(classesField, classLoader));
} else {
throw new RuntimeException(
clClass.getName()
+ " does not extend "
+ pluginClassLoader.getName());
}
} catch(NullPointerException e){
e.printStackTrace();
}
return null;
}
/**
* Get NMS version of current running server
* @return NMS version or 0 length string