org.unitils.util
Class ReflectionUtils

java.lang.Object
  extended by org.unitils.util.ReflectionUtils

public class ReflectionUtils
extends Object

Utility methods that use reflection for instance creation or class inspection.

Author:
Filip Neven, Tim Ducheyne

Constructor Summary
ReflectionUtils()
           
 
Method Summary
static void copyFields(Object fromObject, Object toObject)
           
static
<T> T
createInstanceOfType(Class<T> type, boolean bypassAccessibility)
          Creates an instance of the given type
static
<T> T
createInstanceOfType(Class<T> type, boolean bypassAccessibility, Class[] argumentTypes, Object[] arguments)
          Creates an instance of the given type
static
<T> T
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
<T> Class<T>
getClassForType(Type type)
          Gets the class instance for the given type instance.
static
<T> Class<T>
getClassWithName(String className)
          Gets the class for the given name.
static
<T extends Enum<?>>
T
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
<T> T
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 field declaration.
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
<T> T
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

ReflectionUtils

public ReflectionUtils()
Method Detail

createInstanceOfType

public static <T> T createInstanceOfType(String className,
                                         boolean bypassAccessibility)
Creates an instance of the class with the given name. The class's no argument constructor is used to create an instance.

Parameters:
className - The name of the class, not null
bypassAccessibility - If true, no exception is thrown if the parameterless constructor is not public
Returns:
An instance of this class
Throws:
UnitilsException - if the class could not be found or no instance could be created

createInstanceOfType

public static <T> T createInstanceOfType(Class<T> type,
                                         boolean bypassAccessibility)
Creates an instance of the given type

Type Parameters:
T - The type of the instance
Parameters:
type - The type of the instance
bypassAccessibility - If true, no exception is thrown if the parameterless constructor is not public
Returns:
An instance of this type
Throws:
UnitilsException - If an instance could not be created

createInstanceOfType

public static <T> T createInstanceOfType(Class<T> type,
                                         boolean bypassAccessibility,
                                         Class[] argumentTypes,
                                         Object[] arguments)
Creates an instance of the given type

Type Parameters:
T - The type of the instance
Parameters:
type - The type of the instance
bypassAccessibility - If true, no exception is thrown if the parameterless constructor is not public
argumentTypes - The constructor arg types, not null
arguments - The constructor args, not null
Returns:
An instance of this type
Throws:
UnitilsException - If an instance could not be created

getFieldValue

public static <T> T getFieldValue(Object object,
                                  Field field)
Returns the value of the given field (may be private) in the given object

Parameters:
object - The object containing the field, null for static fields
field - The field, not null
Returns:
The value of the given field in the given object
Throws:
UnitilsException - if the field could not be accessed

setFieldValue

public static void setFieldValue(Object object,
                                 Field field,
                                 Object value)
Sets the given value to the given field on the given object

Parameters:
object - The object containing the field, not null
field - The field, not null
value - The value for the given field in the given object
Throws:
UnitilsException - if the field could not be accessed

setFieldAndSetterValue

public 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.

Parameters:
object - The object containing the field and setters, not null
fields - The fields, not null
setterMethods - The setter methods, not null
value - The value for the given field and setters in the given object

invokeMethod

public static <T> T invokeMethod(Object target,
                                 Method method,
                                 Object... arguments)
                      throws InvocationTargetException
Invokes the given method with the given parameters on the given target object

Parameters:
target - The object containing the method, not null
method - The method, not null
arguments - The method arguments
Returns:
The result of the invocation, null if void
Throws:
UnitilsException - if the method could not be invoked
InvocationTargetException - If the called method throwed an exception

getFieldsAssignableFrom

public 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.

Parameters:
clazz - The class to get fields from, not null
type - The type, not null
isStatic - True if static fields are to be returned, false for non-static
Returns:
A list of Fields, empty list if none found

getFieldsOfType

public static Set<Field> getFieldsOfType(Class<?> clazz,
                                         Type type,
                                         boolean isStatic)
Returns the fields in the given class that have the exact given type. The class's superclasses are also investigated.

Parameters:
clazz - The class to get the field from, not null
type - The type, not null
isStatic - True if static fields are to be returned, false for non-static
Returns:
The fields with the given type

getSettersAssignableFrom

public 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.

Parameters:
clazz - The class to get setters from, not null
type - The type, not null
isStatic - True if static setters are to be returned, false for non-static
Returns:
A list of Methods, empty list if none found

getSettersOfType

public 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. The class's superclasses are also investigated.

Parameters:
clazz - The class to get the setter from, not null
type - The type, not null
isStatic - True if static setters are to be returned, false for non-static
Returns:
All setters for an object of the given type

getSetter

public 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. If isStatic == true, a static setter is searched. If no such setter exists in the given class, null is returned

Parameters:
clazz - The class to get the setter from, not null
propertyName - The name of the property, not null
isStatic - True if a static setter is to be returned, false for non-static
Returns:
The setter method that matches the given parameters, null if not found

getGetter

public static Method getGetter(Class<?> clazz,
                               String propertyName,
                               boolean isStatic)
From the given class, returns the getter for the given property name. If isStatic == true, a static getter is searched. If no such getter exists in the given class, null is returned. When the given field is a boolean the getGetter will also try the isXxxxx.

Parameters:
clazz - The class to get the setter from, not null
propertyName - The name of the property, not null
isStatic - True if a static getter is to be returned, false for non-static
Returns:
The getter method that matches the given parameters, or null if no such method exists

getGetter

public static Method getGetter(Method setter,
                               boolean isStatic)
From the given class, returns the getter for the given setter method. If no such getter exists in the given class, null is returned.

Parameters:
setter - The setter method, not null
isStatic - True if a static getter is to be returned, false for non-static
Returns:
The getter method that matches the given setter, or null if no such method exists

getFieldWithName

public 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.

Parameters:
clazz - The class to get the field from, not null
fieldName - The name, not null
isStatic - True if a static field is to be returned, false for non-static
Returns:
The field that matches the given parameters, or null if no such field exists

getEnumValue

public static <T extends Enum<?>> T getEnumValue(Class<T> enumClass,
                                                 String enumValueName)
Gets the enum value that has the given name.

Parameters:
enumClass - The enum class, not null
enumValueName - The name of the enum value, not null
Returns:
The actual enum value, not null
Throws:
UnitilsException - if no value could be found with the given name

isSetter

public static boolean isSetter(Method method)
For each method, check if it can be a setter for an object of the given type. A setter is a method with the following properties:

Parameters:
method - The method to check, not null
Returns:
True if the given method is a setter, false otherwise

getPropertyName

public static String getPropertyName(Method setterMethod)
Gets the name of the field for the given setter method. An exception is raised when the field name could not be extracted.

Parameters:
setterMethod - The method, not null
Returns:
The field name, not null

getClassWithName

public static <T> Class<T> getClassWithName(String className)
Gets the class for the given name. An UnitilsException is thrown when the class could not be loaded.

Parameters:
className - The name of the class, not null
Returns:
The class, not null

getMethod

public 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.

Parameters:
clazz - The class containing the method
methodName - The name of the method, not null
isStatic - True for a static method, false for non-static
parameterTypes - The parameter types
Returns:
The method, null if no matching method was found

getAllMethods

public static Set<Method> getAllMethods(Class<?> clazz)
Gets all methods of the given class and all its super-classes.

Parameters:
clazz - The class
Returns:
The methods, not null

getAllFields

public static Set<Field> getAllFields(Class<?> clazz)
Gets all fields of the given class and all its super-classes.

Parameters:
clazz - The class
Returns:
The fields, not null

getSimpleMethodName

public static String getSimpleMethodName(Method method)
Gets the string representation of the method as follows: 'class name'.'method name'()

Parameters:
method - The method, not null
Returns:
The string representation, not null

isAssignable

public 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.

Parameters:
fromType - The from type, not null
toType - The to type, not null
Returns:
True if assignable

getGenericType

public static Type getGenericType(Field field)
Gets the T from a Class field declaration. An exception is raised if the field type is not generic or has more than 1 generic type

Parameters:
field - The field to get the type from, not null
Returns:
The declared generic type

getClassForType

public static <T> Class<T> getClassForType(Type type)
Gets the class instance for the given type instance.

Parameters:
type - The type to get a class instance for, not null
Returns:
The class instance, not null

copyFields

public static void copyFields(Object fromObject,
                              Object toObject)


Copyright © 2011. All Rights Reserved.