Object ReflectionUtils
-
- All Implemented Interfaces:
public class ReflectionUtilsUtility methods for calling methods and accessing fields reflectively.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public final classReflectionUtils.ClassParameterTyped parameter used with reflective method calls.
-
Field Summary
Fields Modifier and Type Field Description public final static ReflectionUtilsINSTANCE
-
Method Summary
Modifier and Type Method Description final static <R extends Any> RgetField(Object instance, String fieldName)Reflectively get the value of a field. final static UnitsetField(Object instance, String fieldName, Object fieldNewValue)Reflectively set the value of a field. final static UnitsetField(Class<?> type, Object instance, String fieldName, Object fieldNewValue)Reflectively set the value of a field. final static <R extends Any> RgetStaticField(Field field)Reflectively get the value of a static field. final static <R extends Any> RgetStaticField(Class<?> clazz, String fieldName)Reflectively get the value of a static field. final static UnitsetStaticField(Field field, Object fieldNewValue)Reflectively set the value of a static field. final static UnitsetStaticField(Class<?> clazz, String fieldName, Object fieldNewValue)Reflectively set the value of a static field. final static <R extends Any> RcallInstanceMethod(Object instance, String methodName, ReflectionUtils.ClassParameter<?> classParameters)Reflectively call an instance method on an object. final static <R extends Any> RcallInstanceMethod(Class<?> clazz, Object instance, String methodName, ReflectionUtils.ClassParameter<?> classParameters)Reflectively call an instance method on an instance on a specific class. final static <R extends Any> RcallStaticMethod(String fullyQualifiedClassName, String methodName, ReflectionUtils.ClassParameter<?> classParameters)Helper method for calling a static method using a class. final static <R extends Any> RcallStaticMethod(Class<?> clazz, String methodName, ReflectionUtils.ClassParameter<?> classParameters)Reflectively call a static method on a class. final static Class<?>loadClass(String fullyQualifiedClassName)Try to load a class via reflection. final static <T extends Any> Class<out T>loadClass(String fullyQualifiedClassName, Class<T> superClazz)Try to load a class via reflection. final static BooleanisClassAvailable(String fullyQualifiedClassName)Determines if a named class can be loaded or not. final static <T extends Any> BooleanisClassAvailable(String fullyQualifiedClassName, Class<T> superClazz)Determines if a named Class can be loaded or not. -
-
Method Detail
-
getField
final static <R extends Any> R getField(Object instance, String fieldName)
Reflectively get the value of a field.
- Parameters:
instance- the target instancefieldName- the field name- Returns:
the value of the field on the instance
-
setField
final static Unit setField(Object instance, String fieldName, Object fieldNewValue)
Reflectively set the value of a field.
- Parameters:
instance- the target instance.fieldName- the field name.fieldNewValue- the new value of field.
-
setField
final static Unit setField(Class<?> type, Object instance, String fieldName, Object fieldNewValue)
Reflectively set the value of a field.
- Parameters:
type- the target type.instance- the target instance.fieldName- the field name.fieldNewValue- the new value of field.
-
getStaticField
final static <R extends Any> R getStaticField(Field field)
Reflectively get the value of a static field.
- Parameters:
field- the field instance.- Returns:
the value of the field.
-
getStaticField
final static <R extends Any> R getStaticField(Class<?> clazz, String fieldName)
Reflectively get the value of a static field.
- Parameters:
clazz- the target class.fieldName- the field name.- Returns:
the value of the field.
-
setStaticField
final static Unit setStaticField(Field field, Object fieldNewValue)
Reflectively set the value of a static field.
- Parameters:
field- the field instance.fieldNewValue- the new value.
-
setStaticField
final static Unit setStaticField(Class<?> clazz, String fieldName, Object fieldNewValue)
Reflectively set the value of a static field.
- Parameters:
clazz- the target class.fieldName- the field name.fieldNewValue- the new value of field.
-
callInstanceMethod
final static <R extends Any> R callInstanceMethod(Object instance, String methodName, ReflectionUtils.ClassParameter<?> classParameters)
Reflectively call an instance method on an object.
- Parameters:
instance- the target instance.methodName- the method name to call.classParameters- the array of parameter types and values.- Returns:
the return value of the method
-
callInstanceMethod
final static <R extends Any> R callInstanceMethod(Class<?> clazz, Object instance, String methodName, ReflectionUtils.ClassParameter<?> classParameters)
Reflectively call an instance method on an instance on a specific class.
- Parameters:
clazz- the class.instance- the target instance.methodName- the method name to call.classParameters- the array of parameter types and values.- Returns:
the return value of the method.
-
callStaticMethod
final static <R extends Any> R callStaticMethod(String fullyQualifiedClassName, String methodName, ReflectionUtils.ClassParameter<?> classParameters)
Helper method for calling a static method using a class.
- Parameters:
fullyQualifiedClassName- the fully qualified class name.methodName- the method name to call.classParameters- the array of parameter types and values.- Returns:
the return value of the method.
-
callStaticMethod
final static <R extends Any> R callStaticMethod(Class<?> clazz, String methodName, ReflectionUtils.ClassParameter<?> classParameters)
Reflectively call a static method on a class.
- Parameters:
clazz- the target class.methodName- the method name to call.classParameters- the array of parameter types and values.- Returns:
the return value of the method.
-
loadClass
final static Class<?> loadClass(String fullyQualifiedClassName)
Try to load a class via reflection.
- Parameters:
fullyQualifiedClassName- the full class name.- Returns:
a Class if it's available, or throw Exception
-
loadClass
final static <T extends Any> Class<out T> loadClass(String fullyQualifiedClassName, Class<T> superClazz)
Try to load a class via reflection.
- Parameters:
fullyQualifiedClassName- the full class name.superClazz- the class of the type to cast this class object to.- Returns:
this Class object, cast to represent a subclass of the specified class object.
-
isClassAvailable
final static Boolean isClassAvailable(String fullyQualifiedClassName)
Determines if a named class can be loaded or not.
- Parameters:
fullyQualifiedClassName- the fully qualified name of the class.- Returns:
true if class is available, false otherwise.
-
isClassAvailable
final static <T extends Any> Boolean isClassAvailable(String fullyQualifiedClassName, Class<T> superClazz)
Determines if a named Class can be loaded or not.
- Parameters:
fullyQualifiedClassName- the fully qualified name of the class.superClazz- the class of the type to cast this class object to.- Returns:
true if class is available, false otherwise.
-
-
-
-