Package org.geysermc.floodgate.core.util
Class ReflectionUtils
java.lang.Object
org.geysermc.floodgate.core.util.ReflectionUtils
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tstatic <T> TcastedInvoke(Object instance, Method method, Object... arguments) Invoke the given method of the given instance with the given arguments and cast the value.static <T> TcastedInvoke(Object instance, String method) Invoke the given method of the given instance and cast the value.static booleancastedStaticBooleanValue(Field field) static <T> TcastedStaticValue(Field field) static booleangetBooleanValue(Object instance, Field field) Get the value of a boolean field.static <T> Class<T>getCastedClass(String className) static <T> TgetCastedValue(Object instance, Field field) Get the value of a field and cast it to T.static <T> TgetCastedValue(Object instance, String fieldName) Get the value of a field and cast it to T.static Class<?>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 FieldGet a field from a class, it doesn't matter if the field is public or not.static FieldGet a field of a class.static FieldgetFieldOfType(Class<?> clazz, Class<?> fieldType) Get a declared field from a class without having to provide a field name.
Calling this method is equal to callinggetFieldOfType(Class, Class, boolean)with declared = true.static FieldgetFieldOfType(Class<?> clazz, Class<?> fieldType, boolean declared) Get a field from a class without having to provide a field name.static MethodGet a method from a class, it doesn't matter if the field is public or not.static MethodGet a method from a class, it doesn't matter if the method is public or not.static MethodGet a method from a class, it doesn't matter if the method is public or not.static MethodgetMethodByName(Class<?> clazz, String methodName, boolean declared) Get a method from a class by using the name of the method.static MethodgetMethodFromParam(Class<?> clazz, Class<?> paramType, boolean declared) Get a method from a class without having to provide a method name.static MethodgetMethodThatReturns(Class<?> clazz, Class<?> returnType, boolean declared, Class<?>... parameterTypes) static StringThe package name that is shared between all thegetPrefixedClass(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 insetPrefix(String).static Class<?>getPrefixedClassSilently(String className) static ObjectGet the value of a field.static ObjectGet the value of the given field by finding the field and then get the value of it.static ObjectInvoke the given method of the given instance with the given arguments.static ObjectinvokeStatic(Class<?> clazz, String method) Invoke the given static method.static <T extends AccessibleObject>
TmakeAccessible(T accessibleObject) Make the object accessible if it isn't accessible yetstatic <T> TnewInstance(Constructor<T> constructor, Object... parameters) static voidThe package name that is shared between all thegetPrefixedClass(String)calls so that the className will be a lot shorter.static voidSet the value of a field.static booleanSet the value of a field.
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
getPrefixedClass
Get a class that is prefixed with the prefix provided insetPrefix(String). Calling this method is equal to callinggetClass(String)with prefix.classname as class name.- Parameters:
className- the prefix class to find- Returns:
- the class if found, otherwise null
-
getPrefixedClassSilently
-
getClass
Get the class from a class name. Calling this method is equal to callingClass.forName(String)where String is the class name.
This method will return null when the class isn't found instead of throwing the exception, but the exception will be printed to the console.- Parameters:
className- the name of the class to find- Returns:
- the class or null if the class wasn't found.
-
getCastedClass
-
getClassSilently
-
getClassOrThrow
-
getClassOrFallbackPrefixed
-
getClassOrFallback
-
getConstructor
@Nullable public static <T> Constructor<T> getConstructor(Class<T> clazz, boolean declared, Class<?>... parameters) -
newInstance
-
getField
Get a field of a class. Calling this method is equal to callingClass.getField(String)where String is the fieldName when isPublic is true and calling this method is equal to callingClass.getDeclaredField(String)where String is the fieldName when isPublic is false.
Please note that this method will return null instead of throwing the exception.- Parameters:
clazz- the class name to get the field fromfieldName- the name of the fielddeclared- if the field is declared.- Returns:
- the field if found, otherwise null
-
getField
Get a field from a class, it doesn't matter if the field is public or not. This method will first try to get a declared field and if that failed it'll try to get a public field.- Parameters:
clazz- the class to get the field fromfieldName- the name of the field- Returns:
- the field if found, otherwise null
-
getFieldOfType
Get a field from a class without having to provide a field name.- Parameters:
clazz- the class to search the field fromfieldType- the type of the fielddeclared- if the field is declared- Returns:
- the field if it has been found, otherwise null
-
getFieldOfType
Get a declared field from a class without having to provide a field name.
Calling this method is equal to callinggetFieldOfType(Class, Class, boolean)with declared = true.- Parameters:
clazz- the class to search the field fromfieldType- the type of the declared field- Returns:
- the field if it has been found, otherwise null
-
getValue
Get the value of a field. This method first makes the field accessible and then gets the value.
This method will return null instead of throwing an exception, but it'll log the stacktrace to the console.- Parameters:
instance- the instance to get the value fromfield- the field to get the value from- Returns:
- the value when succeeded, otherwise null
-
getBooleanValue
Get the value of a boolean field. This method first makes the field accessible and then gets the value.
This method will return false instead of throwing an exception, but it'll log the stacktrace to the console.- Parameters:
instance- the instance to get the value fromfield- the field to get the value from- Returns:
- the value when succeeded, otherwise null
-
getValue
Get the value of the given field by finding the field and then get the value of it.- Parameters:
instance- the instance of the objectfieldName- the name of the field to get the value from- Returns:
- the value of the field when succeeded, otheriwse null
-
getCastedValue
Get the value of a field and cast it to T.- Type Parameters:
T- the type to cast the value to- Parameters:
instance- the instance to get the value fromfield- the field to get the value from- Returns:
- the casted value when succeeded, otherwise null
-
getCastedValue
Get the value of a field and cast it to T.- Type Parameters:
T- the type to cast the value to- Parameters:
instance- the instance to get the value fromfieldName- the field to get the value from- Returns:
- the casted value when succeeded, otherwise null
-
castedStaticValue
-
castedStaticBooleanValue
-
setValue
Set the value of a field. This method make the field accessible and then sets the value.
This method doesn't throw an exception when failed, but it'll log the error to the console.- Parameters:
instance- the instance to set the value tofield- the field to set the value tovalue- the value to set
-
setValue
Set the value of a field. This method finds the field, and then callssetValue(Object, Field, Object).- Parameters:
instance- the instance to set the value tofieldName- the field to set the value tovalue- the value to set- Returns:
- true if the field was found
-
getMethod
@Nullable public 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. This method will first try to get a declared field and if that failed it'll try to get a public field.
Instead of throwing an exception when the method wasn't found, it will return null, but the exception will be printed in the console.- Parameters:
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- Returns:
- the requested method if it has been found, otherwise null
-
getMethod
Get a method from a class, it doesn't matter if the method is public or not. This method will first try to get a declared method and if that fails it'll try to get a public method.- Parameters:
clazz- the class to get the method frommethodName- the name of the method to findarguments- the classes of the method arguments- Returns:
- the requested method if it has been found, otherwise null
-
getMethod
Get a method from a class, it doesn't matter if the method is public or not. This method will first try to get a declared method and if that fails it'll try to get a public method.- Parameters:
instance- the class to get the method frommethodName- the name of the method to findarguments- the classes of the method arguments- Returns:
- the requested method if it has been found, otherwise null
-
getMethodThatReturns
-
getMethodByName
Get a method from a class by using the name of the method.- Parameters:
clazz- the class to search the method inmethodName- the name of the methoddeclared- if the method is declared- Returns:
- the method if it has been found, otherwise null
-
getMethodFromParam
@Nullable public static Method getMethodFromParam(Class<?> clazz, Class<?> paramType, boolean declared) Get a method from a class without having to provide a method name.- Parameters:
clazz- the class to search the method inparamType- the type of one of the method parametersdeclared- if the method is declared- Returns:
- the method if it has been found, otherwise null
-
invoke
Invoke the given method of the given instance with the given arguments.- Parameters:
instance- the instance to get the value frommethod- the method to invokearguments- the arguments of the method- Returns:
- the value got from invoking the method, or null when failed to invoke
-
cast
-
castedInvoke
Invoke the given method of the given instance with the given arguments and cast the value.- Parameters:
instance- the instance to get the value frommethod- the method to invokearguments- the arguments of the method- Returns:
- the casted value got from invoking the method, or null when failed to invoke
-
castedInvoke
Invoke the given method of the given instance and cast the value.- Parameters:
instance- the instance to get the value frommethod- the method to invoke- Returns:
- the casted value got from invoking the method, or null when failed to invoke
-
invokeStatic
Invoke the given static method.- Parameters:
clazz- the class to get the method frommethod- the name of the method to invoke- Returns:
- the value got from invoking the status method, or null when failed to invoke
-
makeAccessible
Make the object accessible if it isn't accessible yet- Type Parameters:
T- accessible object type- Parameters:
accessibleObject- the object to make accessible- Returns:
- the accessibleObject
-
getPrefix
The package name that is shared between all thegetPrefixedClass(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 -
setPrefix
The package name that is shared between all thegetPrefixedClass(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
-