|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.unitils.util.ReflectionUtils
public class ReflectionUtils
Utility methods that use reflection for instance creation or class inspection.
Constructor Summary | |
---|---|
ReflectionUtils()
|
Method Summary | ||
---|---|---|
static void |
copyFields(Object fromObject,
Object toObject)
|
|
static
|
createInstanceOfType(Class<T> type,
boolean bypassAccessibility)
Creates an instance of the given type |
|
static
|
createInstanceOfType(Class<T> type,
boolean bypassAccessibility,
Class[] argumentTypes,
Object[] arguments)
Creates an instance of the given type |
|
static
|
createInstanceOfType(String className,
boolean bypassAccessibility)
Creates an instance of the class with the given name. |
|
static Set<Field> |
getAllFields(Class<?> clazz)
Gets all fields of the given class and all its super-classes. |
|
static Set<Method> |
getAllMethods(Class<?> clazz)
Gets all methods of the given class and all its super-classes. |
|
static
|
getClassForType(Type type)
Gets the class instance for the given type instance. |
|
static
|
getClassWithName(String className)
Gets the class for the given name. |
|
static
|
getEnumValue(Class<T> enumClass,
String enumValueName)
Gets the enum value that has the given name. |
|
static Set<Field> |
getFieldsAssignableFrom(Class<?> clazz,
Type type,
boolean isStatic)
Returns all declared fields of the given class that are assignable from the given type. |
|
static Set<Field> |
getFieldsOfType(Class<?> clazz,
Type type,
boolean isStatic)
Returns the fields in the given class that have the exact given type. |
|
static
|
getFieldValue(Object object,
Field field)
Returns the value of the given field (may be private) in the given object |
|
static Field |
getFieldWithName(Class<?> clazz,
String fieldName,
boolean isStatic)
From the given class, returns the field with the given name. isStatic indicates if it should be a static field or not. |
|
static Type |
getGenericType(Field field)
Gets the T from a Class |
|
static Method |
getGetter(Class<?> clazz,
String propertyName,
boolean isStatic)
From the given class, returns the getter for the given property name. |
|
static Method |
getGetter(Method setter,
boolean isStatic)
From the given class, returns the getter for the given setter method. |
|
static Method |
getMethod(Class<?> clazz,
String methodName,
boolean isStatic,
Class<?>... parameterTypes)
Gets the method with the given name from the given class or one of its super-classes. |
|
static String |
getPropertyName(Method setterMethod)
Gets the name of the field for the given setter method. |
|
static Method |
getSetter(Class<?> clazz,
String propertyName,
boolean isStatic)
From the given class, returns the setter for the property with the given name and 1 argument. |
|
static Set<Method> |
getSettersAssignableFrom(Class<?> clazz,
Type type,
boolean isStatic)
Returns all declared setter methods of fields of the given class that are assignable from the given type. |
|
static Set<Method> |
getSettersOfType(Class<?> clazz,
Type type,
boolean isStatic)
Returns the setter methods in the given class that have an argument with the exact given type. |
|
static String |
getSimpleMethodName(Method method)
Gets the string representation of the method as follows: 'class name'.' |
|
static
|
invokeMethod(Object target,
Method method,
Object... arguments)
Invokes the given method with the given parameters on the given target object |
|
static boolean |
isAssignable(Type fromType,
Type toType)
Checks whether the given fromType is assignable to the given toType, also taking into account possible auto-boxing. |
|
static boolean |
isSetter(Method method)
For each method, check if it can be a setter for an object of the given type. |
|
static void |
setFieldAndSetterValue(Object object,
Set<Field> fields,
Set<Method> setterMethods,
Object value)
Sets the given value to the given field and setters on the given object. |
|
static void |
setFieldValue(Object object,
Field field,
Object value)
Sets the given value to the given field on the given object |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public ReflectionUtils()
Method Detail |
---|
public static <T> T createInstanceOfType(String className, boolean bypassAccessibility)
className
- The name of the class, not nullbypassAccessibility
- If true, no exception is thrown if the parameterless
constructor is not public
UnitilsException
- if the class could not be found or no instance could be
createdpublic static <T> T createInstanceOfType(Class<T> type, boolean bypassAccessibility)
T
- The type of the instancetype
- The type of the instancebypassAccessibility
- If true, no exception is thrown if the parameterless
constructor is not public
UnitilsException
- If an instance could not be createdpublic static <T> T createInstanceOfType(Class<T> type, boolean bypassAccessibility, Class[] argumentTypes, Object[] arguments)
T
- The type of the instancetype
- The type of the instancebypassAccessibility
- If true, no exception is thrown if the parameterless
constructor is not publicargumentTypes
- The constructor arg types, not nullarguments
- The constructor args, not null
UnitilsException
- If an instance could not be createdpublic static <T> T getFieldValue(Object object, Field field)
object
- The object containing the field, null for static fieldsfield
- The field, not null
UnitilsException
- if the field could not be accessedpublic static void setFieldValue(Object object, Field field, Object value)
object
- The object containing the field, not nullfield
- The field, not nullvalue
- The value for the given field in the given object
UnitilsException
- if the field could not be accessedpublic static void setFieldAndSetterValue(Object object, Set<Field> fields, Set<Method> setterMethods, Object value)
object
- The object containing the field and setters, not nullfields
- The fields, not nullsetterMethods
- The setter methods, not nullvalue
- The value for the given field and setters in the given objectpublic static <T> T invokeMethod(Object target, Method method, Object... arguments) throws InvocationTargetException
target
- The object containing the method, not nullmethod
- The method, not nullarguments
- The method arguments
UnitilsException
- if the method could not be invoked
InvocationTargetException
- If the called method throwed an exceptionpublic static Set<Field> getFieldsAssignableFrom(Class<?> clazz, Type type, boolean isStatic)
clazz
- The class to get fields from, not nulltype
- The type, not nullisStatic
- True if static fields are to be returned, false for non-static
public static Set<Field> getFieldsOfType(Class<?> clazz, Type type, boolean isStatic)
clazz
- The class to get the field from, not nulltype
- The type, not nullisStatic
- True if static fields are to be returned, false for non-static
public static Set<Method> getSettersAssignableFrom(Class<?> clazz, Type type, boolean isStatic)
clazz
- The class to get setters from, not nulltype
- The type, not nullisStatic
- True if static setters are to be returned, false for
non-static
public static Set<Method> getSettersOfType(Class<?> clazz, Type type, boolean isStatic)
clazz
- The class to get the setter from, not nulltype
- The type, not nullisStatic
- True if static setters are to be returned, false for
non-static
public static Method getSetter(Class<?> clazz, String propertyName, boolean isStatic)
clazz
- The class to get the setter from, not nullpropertyName
- The name of the property, not nullisStatic
- True if a static setter is to be returned, false for
non-static
public static Method getGetter(Class<?> clazz, String propertyName, boolean isStatic)
clazz
- The class to get the setter from, not nullpropertyName
- The name of the property, not nullisStatic
- True if a static getter is to be returned, false for
non-static
public static Method getGetter(Method setter, boolean isStatic)
setter
- The setter method, not nullisStatic
- True if a static getter is to be returned, false for
non-static
public static Field getFieldWithName(Class<?> clazz, String fieldName, boolean isStatic)
clazz
- The class to get the field from, not nullfieldName
- The name, not nullisStatic
- True if a static field is to be returned, false for non-static
public static <T extends Enum<?>> T getEnumValue(Class<T> enumClass, String enumValueName)
enumClass
- The enum class, not nullenumValueName
- The name of the enum value, not null
UnitilsException
- if no value could be found with the given namepublic static boolean isSetter(Method method)
method
- The method to check, not null
public static String getPropertyName(Method setterMethod)
setterMethod
- The method, not null
public static <T> Class<T> getClassWithName(String className)
className
- The name of the class, not null
public static Method getMethod(Class<?> clazz, String methodName, boolean isStatic, Class<?>... parameterTypes)
clazz
- The class containing the methodmethodName
- The name of the method, not nullisStatic
- True for a static method, false for non-staticparameterTypes
- The parameter types
public static Set<Method> getAllMethods(Class<?> clazz)
clazz
- The class
public static Set<Field> getAllFields(Class<?> clazz)
clazz
- The class
public static String getSimpleMethodName(Method method)
method
- The method, not null
public static boolean isAssignable(Type fromType, Type toType)
fromType
- The from type, not nulltoType
- The to type, not null
public static Type getGenericType(Field field)
field
- The field to get the type from, not null
public static <T> Class<T> getClassForType(Type type)
type
- The type to get a class instance for, not null
public static void copyFields(Object fromObject, Object toObject)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |