Class ReflectUtils
-
- All Implemented Interfaces:
public final class ReflectUtils
Utility class to ease Reflection
-
-
Constructor Summary
Constructors Constructor Description ReflectUtils()
-
Method Summary
Modifier and Type Method Description static <T> T
allocateInstance(@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> T
invokeConstructorWithArgs(@NonNull() Class<T> clazz, Array<Object> args)
Attempts to find and invoke the constructor of class T matching the specified arguments 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 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 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 static Object
getField(@NonNull() Object instance, @NonNull() String fieldName)
Gets a field declared in the class. static Object
getField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName)
Gets a field declared in the class. static void
setField(@NonNull() Object instance, @NonNull() String fieldName, @Nullable() Object v)
Override a field of a class. static void
setField(@NonNull() Class<out Object> clazz, @Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)
Override a field of a class. static void
setFinalField(@Nullable() Object instance, @NonNull() String fieldName, @Nullable() Object v)
Override a final field of a class. static void
setFinalField(@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
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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 theclazz
or null to get static fieldfieldName
- Name of 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 theclazz
or 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 theclazz
or 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 theclazz
or null to set static field.fieldName
- Name of the field.v
- Value to store.
-
-
-
-