PlotSquared/Bukkit/src/main/java/com/plotsquared/bukkit/chat/Reflection.java

206 lines
9.2 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.chat;
2016-03-19 05:39:42 +01:00
import com.intellectualcrafters.plot.PS;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* A class containing static utility methods and caches which are intended as reflective conveniences.
* Unless otherwise noted, upon failure methods will return {@code null}.
*/
2015-09-13 06:04:31 +02:00
public final class Reflection {
/**
* Stores loaded classes from the {@code net.minecraft.server} package.
*/
private static final Map<String, Class<?>> _loadedNMSClasses = new HashMap<>();
/**
* Stores loaded classes from the {@code org.bukkit.craftbukkit} package (and subpackages).
*/
private static final Map<String, Class<?>> _loadedOBCClasses = new HashMap<>();
private static final Map<Class<?>, Map<String, Field>> _loadedFields = new HashMap<>();
/**
* Contains loaded methods in a cache.
* The map maps [types to maps of [method names to maps of [parameter types to method instances]]].
*/
private static final Map<Class<?>, Map<String, Map<ArrayWrapper<Class<?>>, Method>>> _loadedMethods = new HashMap<>();
2015-09-13 06:04:31 +02:00
private Reflection() {
2015-09-11 12:09:22 +02:00
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
/**
* Gets the version string from the package name of the CraftBukkit server implementation.
* This is needed to bypass the JAR package name changing on each update.
* @return The version string of the OBC and NMS packages, <em>including the trailing dot</em>.
*/
2015-09-13 06:04:31 +02:00
public synchronized static String getVersion() {
2016-03-19 05:39:42 +01:00
return PS.get().IMP.getNMSPackage();
2015-09-11 12:09:22 +02:00
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
/**
* Gets a {@link Class} object representing a type contained within the {@code net.minecraft.server} versioned package.
* The class instances returned by this method are cached, such that no lookup will be done twice (unless multiple threads are accessing this method simultaneously).
* @param className The name of the class, excluding the package, within NMS.
* @return The class instance representing the specified NMS class, or {@code null} if it could not be loaded.
*/
2015-09-13 06:04:31 +02:00
public synchronized static Class<?> getNMSClass(final String className) {
if (_loadedNMSClasses.containsKey(className)) {
return _loadedNMSClasses.get(className);
}
final String fullName = "net.minecraft.server." + getVersion() + "." + className;
Class<?> clazz;
2015-09-13 06:04:31 +02:00
try {
2015-09-11 12:09:22 +02:00
clazz = Class.forName(fullName);
} catch (ClassNotFoundException e) {
2015-09-11 12:09:22 +02:00
e.printStackTrace();
_loadedNMSClasses.put(className, null);
return null;
}
_loadedNMSClasses.put(className, clazz);
return clazz;
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
/**
* Gets a {@link Class} object representing a type contained within the {@code org.bukkit.craftbukkit} versioned package.
* The class instances returned by this method are cached, such that no lookup will be done twice (unless multiple threads are accessing this
* method simultaneously).
* @param className The name of the class, excluding the package, within OBC. This name may contain a subpackage name, such as {@code inventory
* .CraftItemStack}.
2015-09-11 12:09:22 +02:00
* @return The class instance representing the specified OBC class, or {@code null} if it could not be loaded.
*/
2015-09-13 06:04:31 +02:00
public synchronized static Class<?> getOBCClass(final String className) {
if (_loadedOBCClasses.containsKey(className)) {
return _loadedOBCClasses.get(className);
}
final String fullName = "org.bukkit.craftbukkit." + getVersion() + "." + className;
Class<?> clazz;
2015-09-13 06:04:31 +02:00
try {
2015-09-11 12:09:22 +02:00
clazz = Class.forName(fullName);
} catch (ClassNotFoundException e) {
2015-09-11 12:09:22 +02:00
e.printStackTrace();
_loadedOBCClasses.put(className, null);
return null;
}
_loadedOBCClasses.put(className, clazz);
return clazz;
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
/**
* Attempts to get the NMS handle of a CraftBukkit object.
* <p>
* The only match currently attempted by this method is a retrieval by using a parameterless {@code getHandle()} method implemented by the
* runtime type of the specified object.
2015-09-11 12:09:22 +02:00
* </p>
* @param obj The object for which to retrieve an NMS handle.
* @return The NMS handle of the specified object, or {@code null} if it could not be retrieved using {@code getHandle()}.
*/
2015-09-13 06:04:31 +02:00
public synchronized static Object getHandle(final Object obj) {
try {
2015-09-11 12:09:22 +02:00
return getMethod(obj.getClass(), "getHandle").invoke(obj);
2016-03-19 19:07:55 +01:00
} catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e) {
2015-09-11 12:09:22 +02:00
e.printStackTrace();
return null;
}
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
/**
* Retrieves a {@link Field} instance declared by the specified class with the specified name.
* Java access modifiers are ignored during this retrieval. No guarantee is made as to whether the field
* returned will be an instance or static field.
* <p>
* A global caching mechanism within this class is used to store fields. Combined with synchronization, this guarantees that
* no field will be reflectively looked up twice.
* </p>
* <p>
* If a field is deemed suitable for return,
* {@link Field#setAccessible(boolean) setAccessible} will be invoked with an argument of {@code true} before it is returned.
2015-09-11 12:09:22 +02:00
* This ensures that callers do not have to check or worry about Java access modifiers when dealing with the returned instance.
* </p>
* @param clazz The class which contains the field to retrieve.
* @param name The declared name of the field in the class.
* @return A field object with the specified name declared by the specified class.
* @see Class#getDeclaredField(String)
*/
2015-09-13 06:04:31 +02:00
public synchronized static Field getField(final Class<?> clazz, final String name) {
2015-09-11 12:09:22 +02:00
Map<String, Field> loaded;
2015-09-13 06:04:31 +02:00
if (!_loadedFields.containsKey(clazz)) {
loaded = new HashMap<>();
2015-09-11 12:09:22 +02:00
_loadedFields.put(clazz, loaded);
2015-09-13 06:04:31 +02:00
} else {
2015-09-11 12:09:22 +02:00
loaded = _loadedFields.get(clazz);
}
2015-09-13 06:04:31 +02:00
if (loaded.containsKey(name)) {
2015-09-11 12:09:22 +02:00
// If the field is loaded (or cached as not existing), return the relevant value, which might be null
return loaded.get(name);
}
2015-09-13 06:04:31 +02:00
try {
2015-09-11 12:09:22 +02:00
final Field field = clazz.getDeclaredField(name);
field.setAccessible(true);
loaded.put(name, field);
return field;
2016-03-19 05:39:42 +01:00
} catch (NoSuchFieldException | SecurityException e) {
2015-09-11 12:09:22 +02:00
// Error loading
e.printStackTrace();
// Cache field as not existing
loaded.put(name, null);
return null;
}
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
/**
* Retrieves a {@link Method} instance declared by the specified class with the specified name and argument types.
* Java access modifiers are ignored during this retrieval. No guarantee is made as to whether the field
* returned will be an instance or static field.
* <p>
* A global caching mechanism within this class is used to store method. Combined with synchronization, this guarantees that
* no method will be reflectively looked up twice.
* </p>
* <p>
* If a method is deemed suitable for return, {@link Method#setAccessible(boolean) setAccessible} will be invoked with an argument of {@code true} before it is returned.
* This ensures that callers do not have to check or worry about Java access modifiers when dealing with the returned instance.
* </p>
* <p>
* This method does <em>not</em> search superclasses of the specified type for methods with the specified signature.
* Callers wishing this behavior should use {@link Class#getDeclaredMethod(String, Class...)}.
* @param clazz The class which contains the method to retrieve.
* @param name The declared name of the method in the class.
* @param args The formal argument types of the method.
* @return A method object with the specified name declared by the specified class.
*/
2015-09-13 06:04:31 +02:00
public synchronized static Method getMethod(final Class<?> clazz, final String name, final Class<?>... args) {
if (!_loadedMethods.containsKey(clazz)) {
2015-09-11 12:09:22 +02:00
_loadedMethods.put(clazz, new HashMap<String, Map<ArrayWrapper<Class<?>>, Method>>());
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
final Map<String, Map<ArrayWrapper<Class<?>>, Method>> loadedMethodNames = _loadedMethods.get(clazz);
2015-09-13 06:04:31 +02:00
if (!loadedMethodNames.containsKey(name)) {
2015-09-11 12:09:22 +02:00
loadedMethodNames.put(name, new HashMap<ArrayWrapper<Class<?>>, Method>());
}
2015-09-13 06:04:31 +02:00
2015-09-11 12:09:22 +02:00
final Map<ArrayWrapper<Class<?>>, Method> loadedSignatures = loadedMethodNames.get(name);
final ArrayWrapper<Class<?>> wrappedArg = new ArrayWrapper<>(args);
2015-09-13 06:04:31 +02:00
if (loadedSignatures.containsKey(wrappedArg)) {
return loadedSignatures.get(wrappedArg);
}
for (final Method m : clazz.getMethods()) {
if (m.getName().equals(name) && Arrays.equals(args, m.getParameterTypes())) {
2015-09-11 12:09:22 +02:00
m.setAccessible(true);
loadedSignatures.put(wrappedArg, m);
return m;
}
}
loadedSignatures.put(wrappedArg, null);
return null;
}
2015-09-13 06:04:31 +02:00
}