public final class Reflection
extends java.lang.Object
Modified Reflection utility from LagMonitor plugin. https://github.com/games647/LagMonitor/blob/master/src/main/java/com/github/games647/lagmonitor/traffic/Reflection.java
Modifier and Type | Class and Description |
---|---|
static interface |
Reflection.ConstructorInvoker
An interface for invoking a specific constructor.
|
static interface |
Reflection.FieldAccessor<T>
An interface for retrieving the field content.
|
static interface |
Reflection.MethodInvoker
An interface for invoking a specific method.
|
Modifier and Type | Method and Description |
---|---|
static java.lang.Class<?> |
getClass(java.lang.String lookupName)
Retrieve a class from its full name.
|
static Reflection.ConstructorInvoker |
getConstructor(java.lang.Class<?> clazz,
java.lang.Class<?>... params)
Search for the first publicly and privately defined constructor of the given name and parameter count.
|
static Reflection.ConstructorInvoker |
getConstructor(java.lang.String className,
java.lang.Class<?>... params)
Search for the first publicly and privately defined constructor of the given name and parameter count.
|
static java.lang.Class<?> |
getCraftBukkitClass(java.lang.String name)
Retrieve a class in the org.bukkit.craftbukkit.VERSION.* package.
|
static <T> Reflection.FieldAccessor<T> |
getField(java.lang.Class<?> target,
java.lang.Class<T> fieldType,
int index)
Retrieve a field accessor for a specific field type and name.
|
static <T> Reflection.FieldAccessor<T> |
getField(java.lang.Class<?> target,
java.lang.String name,
java.lang.Class<T> fieldType)
Retrieve a field accessor for a specific field type and name.
|
static <T> Reflection.FieldAccessor<T> |
getField(java.lang.String className,
java.lang.Class<T> fieldType,
int index)
Retrieve a field accessor for a specific field type and name.
|
static <T> Reflection.FieldAccessor<T> |
getField(java.lang.String className,
java.lang.String name,
java.lang.Class<T> fieldType)
Retrieve a field accessor for a specific field type and name.
|
static Reflection.MethodInvoker |
getMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?>... params)
Search for the first publicly and privately defined method of the given name and parameter count.
|
static Reflection.MethodInvoker |
getMethod(java.lang.String className,
java.lang.String methodName,
java.lang.Class<?>... params)
Search for the first publicly and privately defined method of the given name and parameter count.
|
static java.lang.Class<?> |
getMinecraftClass(java.lang.String name)
Retrieve a class in the net.minecraft.server.VERSION.* package.
|
static Reflection.MethodInvoker |
getTypedMethod(java.lang.Class<?> clazz,
java.lang.String methodName,
java.lang.Class<?> returnType,
java.lang.Class<?>... params)
Search for the first publicly and privately defined method of the given name and parameter count.
|
static java.lang.Class<java.lang.Object> |
getUntypedClass(java.lang.String lookupName)
Retrieve a class from its full name, without knowing its type on compile time.
|
public static <T> Reflection.FieldAccessor<T> getField(java.lang.Class<?> target, java.lang.String name, java.lang.Class<T> fieldType)
target
- - the target type.name
- - the name of the field, or NULL to ignore.fieldType
- - a compatible field type.public static <T> Reflection.FieldAccessor<T> getField(java.lang.String className, java.lang.String name, java.lang.Class<T> fieldType)
className
- - lookup name of the class, see getClass(String)
.name
- - the name of the field, or NULL to ignore.fieldType
- - a compatible field type.public static <T> Reflection.FieldAccessor<T> getField(java.lang.Class<?> target, java.lang.Class<T> fieldType, int index)
target
- - the target type.fieldType
- - a compatible field type.index
- - the number of compatible fields to skip.public static <T> Reflection.FieldAccessor<T> getField(java.lang.String className, java.lang.Class<T> fieldType, int index)
className
- - lookup name of the class, see getClass(String)
.fieldType
- - a compatible field type.index
- - the number of compatible fields to skip.public static Reflection.MethodInvoker getMethod(java.lang.String className, java.lang.String methodName, java.lang.Class<?>... params)
className
- - lookup name of the class, see getClass(String)
.methodName
- - the method name, or NULL to skip.params
- - the expected parameters.java.lang.IllegalStateException
- If we cannot find this method.public static Reflection.MethodInvoker getMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?>... params)
clazz
- - a class to start with.methodName
- - the method name, or NULL to skip.params
- - the expected parameters.java.lang.IllegalStateException
- If we cannot find this method.public static Reflection.MethodInvoker getTypedMethod(java.lang.Class<?> clazz, java.lang.String methodName, java.lang.Class<?> returnType, java.lang.Class<?>... params)
clazz
- - a class to start with.methodName
- - the method name, or NULL to skip.returnType
- - the expected return type, or NULL to ignore.params
- - the expected parameters.java.lang.IllegalStateException
- If we cannot find this method.public static Reflection.ConstructorInvoker getConstructor(java.lang.String className, java.lang.Class<?>... params)
className
- - lookup name of the class, see getClass(String)
.params
- - the expected parameters.java.lang.IllegalStateException
- If we cannot find this method.public static Reflection.ConstructorInvoker getConstructor(java.lang.Class<?> clazz, java.lang.Class<?>... params)
clazz
- - a class to start with.params
- - the expected parameters.java.lang.IllegalStateException
- If we cannot find this method.public static java.lang.Class<java.lang.Object> getUntypedClass(java.lang.String lookupName)
This is useful when looking up fields by a NMS or OBC type.
lookupName
- - the class name with variables.Object.getClass()
public static java.lang.Class<?> getClass(java.lang.String lookupName)
Strings enclosed with curly brackets - such as {TEXT} - will be replaced according to the following table:
Variable | Content |
---|---|
{nms} | Actual package name of net.minecraft.server.VERSION |
{obc} | Actual package name of org.bukkit.craftbukkit.VERSION |
{version} | The current Minecraft package VERSION, if any. |
lookupName
- - the class name with variables.java.lang.IllegalArgumentException
- If a variable or class could not be found.public static java.lang.Class<?> getMinecraftClass(java.lang.String name)
name
- - the name of the class, excluding the package.java.lang.IllegalArgumentException
- If the class doesn't exist.public static java.lang.Class<?> getCraftBukkitClass(java.lang.String name)
name
- - the name of the class, excluding the package.java.lang.IllegalArgumentException
- If the class doesn't exist.