Class ReflectUtils
-
- All Implemented Interfaces:
public final class ReflectUtilsUtility class to ease Reflection
-
-
Constructor Summary
Constructors Constructor Description ReflectUtils()
-
Method Summary
Modifier and Type Method Description static <T> TallocateInstance(@NonNull() Class<T> clazz)Creates new class instance without using a constructor static <T> Constructor<T>getConstructorByArgs(@NonNull() Class<T> clazz, Array<Object> args)Gets the constructor for class T matching the specified arguments static <T> TinvokeConstructorWithArgs(@NonNull() Class<T> clazz, Array<Object> args)Attempts to find and invoke the constructor of class T matching the specified arguments static MethodgetMethodByArgs(@NonNull() Class<out Object> clazz, @NonNull() String methodName, Array<Object> args)Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically static ObjectinvokeMethod(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String methodName, Array<Object> args)Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically static ObjectinvokeMethod(@NonNull() Object instance, @NonNull() String methodName, Array<Object> args)Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically static ObjectgetField(@NonNull() Object instance, @NonNull() String fieldName)Gets a field declared in the class. static ObjectgetField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName)Gets a field declared in the class. static voidsetField(@NonNull() Object instance, @NonNull() String fieldName, @Nullable() Object v)Override a field of a class. static voidsetField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)Override a field of a class. static voidsetFinalField(@Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)Override a final field of a class. static voidsetFinalField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)Override a final field of a class. -
-
Method Detail
-
allocateInstance
static <T> T allocateInstance(@NonNull() Class<T> clazz)
Creates new class instance without using a constructor
- Parameters:
clazz- Class- Returns:
Created instance
-
getConstructorByArgs
static <T> Constructor<T> getConstructorByArgs(@NonNull() Class<T> clazz, Array<Object> args)
Gets the constructor for class T matching the specified arguments
- Parameters:
clazz- T.args- The arguments that should be passed to the constructor.- Returns:
The found constructor
-
invokeConstructorWithArgs
static <T> T invokeConstructorWithArgs(@NonNull() Class<T> clazz, Array<Object> args)
Attempts to find and invoke the constructor of class T matching the specified arguments
- Parameters:
clazz- T.args- The arguments to invoke the constructor with.- Returns:
The constructed Object
-
getMethodByArgs
@NonNull() static Method getMethodByArgs(@NonNull() Class<out Object> clazz, @NonNull() String methodName, Array<Object> args)
Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically
- Parameters:
clazz- The classmethodName- The name of the methodargs- The arguments to invoke the method with.- Returns:
The found method
-
invokeMethod
@Nullable() static Object invokeMethod(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String methodName, Array<Object> args)
Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically
- Parameters:
clazz- The class holding the methodinstance- The instance of the class to invoke the method on or null to invoke static methodmethodName- The name of the methodargs- The arguments to invoke the method with.- Returns:
The result of invoking the method
-
invokeMethod
@Nullable() static Object invokeMethod(@NonNull() Object instance, @NonNull() String methodName, Array<Object> args)
Attempts to find and invoke the method matching the specified arguments Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Method to improve performance drastically
- Parameters:
instance- The instance of the class to invoke the method onmethodName- The name of the methodargs- The arguments to invoke the method with.- Returns:
The result of invoking the method
-
getField
@Nullable() static Object getField(@NonNull() Object instance, @NonNull() String fieldName)
Gets a field declared in the class. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically
- Parameters:
instance- Instance of the class where the field is located.fieldName- Name of the field.- Returns:
Data stored in the field.
-
getField
@Nullable() static Object getField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName)
Gets a field declared in the class. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically
- Parameters:
clazz- Class where the field is located.instance- Instance of theclazzor null to get static fieldfieldName- Name of the field.- Returns:
Data stored in the field.
-
setField
static void setField(@NonNull() Object instance, @NonNull() String fieldName, @Nullable() Object v)
Override a field of a class. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically
- Parameters:
instance- Instance of the class where the field is located.fieldName- Name of the field.v- Value to store.
-
setField
static void setField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)
Override a field of a class. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically
- Parameters:
clazz- Class where the field is located.instance- Instance of theclazzor null to set static field.fieldName- Name of the field.v- Value to store.
-
setFinalField
static void setFinalField(@Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)
Override a final field of a class. WARNING: If this field is of a primitive type, setting it may have no effect as the compiler will inline final primitives. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically
- Parameters:
instance- Instance of theclazzor null to set static field.fieldName- Name of the field.v- Value to store.
-
setFinalField
static void setFinalField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)
Override a final field of a class. WARNING: If this field is of a primitive type, setting it may have no effect as the compiler will inline final primitives. Please note that this does not cache the lookup result, so if you need to call this many times you should do it manually and cache the Field to improve performance drastically
- Parameters:
clazz- Class where the field is located.instance- Instance of theclazzor null to set static field.fieldName- Name of the field.v- Value to store.
-
-
-
-