public final class ReflectionUtils extends Object
| Constructor and Description |
|---|
ReflectionUtils() |
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
cast(Object instance,
Class<T> castTo) |
static <T> T |
castedInvoke(Object instance,
Method method,
Object... arguments)
Invoke the given method of the given instance with the given arguments and cast the value.
|
static <T> T |
castedInvoke(Object instance,
String method)
Invoke the given method of the given instance and cast the value.
|
static boolean |
castedStaticBooleanValue(Field field) |
static <T> T |
castedStaticValue(Field field) |
static boolean |
getBooleanValue(Object instance,
Field field)
Get the value of a boolean field.
|
static <T> Class<T> |
getCastedClass(String className) |
static <T> T |
getCastedValue(Object instance,
Field field)
Get the value of a field and cast it to T.
|
static <T> T |
getCastedValue(Object instance,
String fieldName)
Get the value of a field and cast it to T.
|
static Class<?> |
getClass(String className)
Get the class from a class name.
|
static Class<?> |
getClassOrFallback(String className,
String fallbackClassName) |
static Class<?> |
getClassOrFallbackPrefixed(String className,
String fallbackClassName) |
static Class<?> |
getClassOrThrow(String className) |
static Class<?> |
getClassSilently(String className) |
static <T> Constructor<T> |
getConstructor(Class<T> clazz,
boolean declared,
Class<?>... parameters) |
static Field |
getField(Class<?> clazz,
String fieldName)
Get a field from a class, it doesn't matter if the field is public or not.
|
static Field |
getField(Class<?> clazz,
String fieldName,
boolean declared)
Get a field of a class.
|
static Field |
getFieldOfType(Class<?> clazz,
Class<?> fieldType)
Get a declared field from a class without having to provide a field name.
Calling this method is equal to calling getFieldOfType(Class, Class, boolean) with declared =
true. |
static Field |
getFieldOfType(Class<?> clazz,
Class<?> fieldType,
boolean declared)
Get a field from a class without having to provide a field name.
|
static Method |
getMethod(Class<?> clazz,
String method,
boolean declared,
Class<?>... arguments)
Get a method from a class, it doesn't matter if the field is public or not.
|
static Method |
getMethod(Class<?> clazz,
String methodName,
Class<?>... arguments)
Get a method from a class, it doesn't matter if the method is public or not.
|
static Method |
getMethod(Object instance,
String methodName,
Class<?>... arguments)
Get a method from a class, it doesn't matter if the method is public or not.
|
static Method |
getMethodByName(Class<?> clazz,
String methodName,
boolean declared)
Get a method from a class by using the name of the method.
|
static Method |
getMethodFromParam(Class<?> clazz,
Class<?> paramType,
boolean declared)
Get a method from a class without having to provide a method name.
|
static Method |
getMethodThatReturns(Class<?> clazz,
Class<?> returnType,
boolean declared,
Class<?>... parameterTypes) |
static String |
getPrefix()
The package name that is shared between all the
getPrefixedClass(String) calls so
that the className will be a lot shorter. |
static Class<?> |
getPrefixedClass(String className)
Get a class that is prefixed with the prefix provided in
setPrefix(String). |
static Class<?> |
getPrefixedClassSilently(String className) |
static Object |
getValue(Object instance,
Field field)
Get the value of a field.
|
static Object |
getValue(Object instance,
String fieldName)
Get the value of the given field by finding the field and then get the value of it.
|
static Object |
invoke(Object instance,
Method method,
Object... arguments)
Invoke the given method of the given instance with the given arguments.
|
static Object |
invokeStatic(Class<?> clazz,
String method)
Invoke the given static method.
|
static <T extends AccessibleObject> |
makeAccessible(T accessibleObject)
Make the object accessible if it isn't accessible yet
|
static <T> T |
newInstance(Constructor<T> constructor,
Object... parameters) |
static <T> T |
newInstanceOrThrow(Constructor<T> constructor,
Object... parameters) |
static void |
setPrefix(String prefix)
The package name that is shared between all the
getPrefixedClass(String) calls so
that the className will be a lot shorter. |
static void |
setValue(Object instance,
Field field,
Object value)
Set the value of a field.
|
static boolean |
setValue(Object instance,
String fieldName,
Object value)
Set the value of a field.
|
@Nullable public static Class<?> getPrefixedClass(String className)
setPrefix(String). Calling
this method is equal to calling getClass(String) with prefix.classname
as class name.className - the prefix class to find@Nullable public static Class<?> getPrefixedClassSilently(String className)
@Nullable public static Class<?> getClass(String className)
Class.forName(String) where String is the class name.className - the name of the class to findpublic static Class<?> getClassOrFallbackPrefixed(String className, String fallbackClassName)
public static Class<?> getClassOrFallback(String className, String fallbackClassName)
@Nullable public static <T> Constructor<T> getConstructor(Class<T> clazz, boolean declared, Class<?>... parameters)
@Nullable public static <T> T newInstanceOrThrow(Constructor<T> constructor, Object... parameters)
@Nullable public static <T> T newInstance(Constructor<T> constructor, Object... parameters)
@Nullable public static Field getField(Class<?> clazz, String fieldName, boolean declared)
Class.getField(String) where String is the fieldName when isPublic is true and calling this
method is equal to calling Class.getDeclaredField(String) where String is the
fieldName when isPublic is false.clazz - the class name to get the field fromfieldName - the name of the fielddeclared - if the field is declared.@Nullable public static Field getField(Class<?> clazz, String fieldName)
clazz - the class to get the field fromfieldName - the name of the field@Nullable public static Field getFieldOfType(Class<?> clazz, Class<?> fieldType, boolean declared)
clazz - the class to search the field fromfieldType - the type of the fielddeclared - if the field is declared@Nullable public static Field getFieldOfType(Class<?> clazz, Class<?> fieldType)
getFieldOfType(Class, Class, boolean) with declared =
true.clazz - the class to search the field fromfieldType - the type of the declared field@Nullable public static Object getValue(Object instance, Field field)
instance - the instance to get the value fromfield - the field to get the value frompublic static boolean getBooleanValue(Object instance, Field field)
instance - the instance to get the value fromfield - the field to get the value from@Nullable public static Object getValue(Object instance, String fieldName)
instance - the instance of the objectfieldName - the name of the field to get the value from@Nullable public static <T> T getCastedValue(Object instance, Field field)
T - the type to cast the value toinstance - the instance to get the value fromfield - the field to get the value from@Nullable public static <T> T getCastedValue(Object instance, String fieldName)
T - the type to cast the value toinstance - the instance to get the value fromfieldName - the field to get the value frompublic static boolean castedStaticBooleanValue(Field field)
public static void setValue(Object instance, Field field, Object value)
instance - the instance to set the value tofield - the field to set the value tovalue - the value to setpublic static boolean setValue(Object instance, String fieldName, Object value)
setValue(Object, Field, Object).instance - the instance to set the value tofieldName - the field to set the value tovalue - the value to set@Nullable public static Method getMethod(Class<?> clazz, String method, boolean declared, Class<?>... arguments)
clazz - the class to get the method frommethod - the name of the method to finddeclared - if the the method is declaredarguments - the classes of the method arguments@Nullable public static Method getMethod(Class<?> clazz, String methodName, Class<?>... arguments)
clazz - the class to get the method frommethodName - the name of the method to findarguments - the classes of the method arguments@Nullable public static Method getMethod(Object instance, String methodName, Class<?>... arguments)
instance - the class to get the method frommethodName - the name of the method to findarguments - the classes of the method arguments@Nullable public static Method getMethodThatReturns(Class<?> clazz, Class<?> returnType, boolean declared, Class<?>... parameterTypes)
@Nullable public static Method getMethodByName(Class<?> clazz, String methodName, boolean declared)
clazz - the class to search the method inmethodName - the name of the methoddeclared - if the method is declared@Nullable public static Method getMethodFromParam(Class<?> clazz, Class<?> paramType, boolean declared)
clazz - the class to search the method inparamType - the type of one of the method parametersdeclared - if the method is declared@Nullable public static Object invoke(Object instance, Method method, Object... arguments)
instance - the instance to get the value frommethod - the method to invokearguments - the arguments of the method@Nullable public static <T> T castedInvoke(Object instance, Method method, Object... arguments)
instance - the instance to get the value frommethod - the method to invokearguments - the arguments of the method@Nullable public static <T> T castedInvoke(Object instance, String method)
instance - the instance to get the value frommethod - the method to invoke@Nullable public static Object invokeStatic(Class<?> clazz, String method)
clazz - the class to get the method frommethod - the name of the method to invokepublic static <T extends AccessibleObject> T makeAccessible(T accessibleObject)
T - accessible object typeaccessibleObject - the object to make accessiblepublic static String getPrefix()
getPrefixedClass(String) calls so
that the className will be a lot shorter. Example net.minecraft.server.v1_8R3.PacketHandshakingInSetProtocol
will become PacketHandshakingInSetProtocol if the prefix is set to
net.minecraft.server.v1_8R3public static void setPrefix(String prefix)
getPrefixedClass(String) calls so
that the className will be a lot shorter. Example net.minecraft.server.v1_8R3.PacketHandshakingInSetProtocol
will become PacketHandshakingInSetProtocol if the prefix is set to
net.minecraft.server.v1_8R3