cat.nyaa.nyaacore.utils.ReflectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rpgitems Show documentation
Show all versions of rpgitems Show documentation
The legacy RPGItems continues. (fork)
/*
* Copyright (C) SainttX
* Copyright (C) contributors
*
* This file is part of Auctions.
*
* Auctions is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Auctions is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Auctions. If not, see .
*/
package cat.nyaa.nyaacore.utils;
import org.bukkit.Bukkit;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.*;
public final class ReflectionUtils {
/*
* Cache of NMS classes that we've searched for
*/
private static final Map> loadedNMSClasses = new HashMap<>();
/*
* Cache of OBS classes that we've searched for
*/
private static final Map> loadedOBCClasses = new HashMap<>();
/*
* Cache of methods that we've found in particular classes
*/
private static final Map, Map> loadedMethods = new HashMap<>();
/*
* The server version string to location NMS & OBC classes
*/
private static String versionString;
/**
* Gets the version string for NMS & OBC class paths
*
* @return The version string of OBC and NMS packages
*/
public static String getVersion() {
if (versionString == null) {
String name = Bukkit.getServer().getClass().getPackage().getName();
versionString = name.substring(name.lastIndexOf('.') + 1) + ".";
}
return versionString;
}
/**
* Get a class from the org.bukkit.craftbukkit package
*
* @param obcClassName the path to the class
* @return the found class at the specified path
*/
public static Class getOBCClass(String obcClassName) {
if (loadedOBCClasses.containsKey(obcClassName)) {
return loadedOBCClasses.get(obcClassName);
}
String clazzName = "org.bukkit.craftbukkit." + getVersion() + obcClassName;
Class clazz;
try {
clazz = Class.forName(clazzName);
} catch (Throwable t) {
t.printStackTrace();
loadedOBCClasses.put(obcClassName, null);
return null;
}
loadedOBCClasses.put(obcClassName, clazz);
return clazz;
}
/**
* Get a method from a class that has the specific parameters
*
* @param clazz The class we are searching
* @param methodName The name of the method
* @param params Any parameters that the method has
* @return The method with appropriate parameters
*/
public static Method getMethod(Class clazz, String methodName, Class... params) {
if (!loadedMethods.containsKey(clazz)) {
loadedMethods.put(clazz, new HashMap<>());
}
Map methods = loadedMethods.get(clazz);
if (methods.containsKey(methodName)) {
return methods.get(methodName);
}
try {
Method method = clazz.getDeclaredMethod(methodName, params);
if (method != null) method.setAccessible(true);
methods.put(methodName, method);
loadedMethods.put(clazz, methods);
return method;
} catch (Exception e) {
e.printStackTrace();
methods.put(methodName, null);
loadedMethods.put(clazz, methods);
return null;
}
}
/**
* get all declared fields in a class and its' super class.
*
* @param clz target class
* @return a List of Field objects declared by clz.
* @since 7.2
*/
public static List getAllFields(Class clz) {
List fields = new ArrayList<>();
return getAllFields(clz, fields);
}
private static List getAllFields(Class clz, List list) {
Collections.addAll(list, clz.getDeclaredFields());
Class supClz = clz.getSuperclass();
if (supClz == null) {
return list;
} else {
return getAllFields(supClz, list);
}
}
}