Package net.sf.jguiraffe.di
Class ReflectionUtils
- java.lang.Object
-
- net.sf.jguiraffe.di.ReflectionUtils
-
public final class ReflectionUtils extends Object
An utility class that provides some functionality related to reflection and dependency injection.
This class implements some basic functionality that is needed by other parts of the dependency injection package. It especially deals with row reflection calls and exception handling. It is not intended to be used directly by applications using this framework. It will be called under the hood.
- Version:
- $Id: ReflectionUtils.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> List<Constructor<T>>
findConstructors(Class<T> targetClass, Class<?>[] paramTypes, boolean exactTypeMatch)
Finds all constructors matching the specified search criteria.static List<Method>
findMethods(Class<?> targetClass, String methodName, Class<?>[] paramTypes, boolean exactTypeMatch)
Finds all methods matching the given search criteria.static Object
getProperty(Object bean, String name)
Returns the value of the specified property from the given bean.static <T> T
invokeConstructor(Constructor<T> ctor, Object... args)
Creates an object by invoking the specified constructor with the given arguments.static Object
invokeMethod(Method method, Object target, Object... args)
Helper method for invoking a method using reflection.static Class<?>
loadClass(String className, ClassLoader loader)
Loads the class with the specified name using the given class loader.static List<Method>
removeCovariantDuplicates(List<Method> methods)
Removes duplicate methods from the specified list.static void
setProperty(Object bean, String name, Object value)
Sets the value of a property for the specified bean.
-
-
-
Method Detail
-
loadClass
public static Class<?> loadClass(String className, ClassLoader loader)
Loads the class with the specified name using the given class loader. This is a thin wrapper over theClass.forName()
method.ClassNotFoundException
exceptions are caught and re-thrown asInjectionException
exceptions.- Parameters:
className
- the name of the class to be loadedloader
- the class loader to use- Returns:
- the loaded class
- Throws:
IllegalArgumentException
- if the class name or the class loader is undefinedInjectionException
- if the class cannot be resolved
-
invokeMethod
public static Object invokeMethod(Method method, Object target, Object... args)
Helper method for invoking a method using reflection. This method catches the variety of possible exceptions and re-throws them as runtime exceptions.- Parameters:
method
- the method to be invokedtarget
- the target objectargs
- the arguments of the method- Returns:
- the return value of the method
- Throws:
InjectionException
- if invoking the method causes an errorIllegalArgumentException
- if the method object is null
-
invokeConstructor
public static <T> T invokeConstructor(Constructor<T> ctor, Object... args)
Creates an object by invoking the specified constructor with the given arguments. LikeinvokeMethod()
, this is a helper method that deals with all possible exceptions and redirects them asInjectionException
s.- Type Parameters:
T
- the type of the constructor- Parameters:
ctor
- the constructor to be invoked (must not be null)args
- the arguments to be passed to the constructor- Returns:
- the newly created instance
- Throws:
InjectionException
- if construction of the object failsIllegalArgumentException
- if the constructor object is null
-
findMethods
public static List<Method> findMethods(Class<?> targetClass, String methodName, Class<?>[] paramTypes, boolean exactTypeMatch)
Finds all methods matching the given search criteria. With this method a list of the methods of the specified target class can be obtained that have the given name and are compatible with the given parameter types. Wild cards are supported as follows:- The method name can be null, then only parameter types are compared.
- Each element of the
paramTypes
can be null, then arbitrary parameter types are accepted at this place. - The whole
paramTypes
array can be null, then all methods with the given name and arbitrary signature are accepted.
exactTypeMatch
parameter controls how parameter types are compared: If set to true the parameter types must match exactly; otherwise, the parameters of the method in the target class can be super classes of the provided parameter types. So this method allows searching for methods of a given class in a flexible way including the following use cases:- Search for all methods with a given name: just pass in null for the parameter types array.
- Search for all methods with a given signature: pass in null for the method name and specify a corresponding array with parameter types.
- Search for methods with a given name and a partly known signature: here the name of the method and a type array with all known types set has to be passed in.
- Parameters:
targetClass
- the target class (must not be null)methodName
- the name of the method to be searched forparamTypes
- an array with the parameter typesexactTypeMatch
- a flag whether parameter types should be matched exactly- Returns:
- a list with methods matching the search criteria
- Throws:
IllegalArgumentException
- if the target class is null
-
removeCovariantDuplicates
public static List<Method> removeCovariantDuplicates(List<Method> methods)
Removes duplicate methods from the specified list. When callingfindMethods(Class, String, Class[], boolean)
it is possible that the resulting list contains multiple methods with the same name and signature, but with a different result type. This is the case for instance if a class overrides a super class method with a covariant return type. With this method such duplicates can be eliminated. It searches for methods with the same name and signature and drops all of them except for the one with the most specific return type. The result of the method depends on the operations performed: If no duplicates have been found, the same list is returned without changes. Otherwise, a new list is created and returned.- Parameters:
methods
- the list with methods to be checked (must not be null- Returns:
- a list with duplicates removed
- Throws:
IllegalArgumentException
- if the passed in list is null or contain null elements
-
findConstructors
public static <T> List<Constructor<T>> findConstructors(Class<T> targetClass, Class<?>[] paramTypes, boolean exactTypeMatch)
Finds all constructors matching the specified search criteria. This method works likefindMethods(Class, String, Class[], boolean)
, but deals with constructors of the target class.- Type Parameters:
T
- the type of the constructor- Parameters:
targetClass
- the target class (must not be null)paramTypes
- an array with the parameter typesexactTypeMatch
- a flag whether parameter types should be matched exactly- Returns:
- a list with constructors matching the search criteria
- Throws:
IllegalArgumentException
- if the target class is null
-
getProperty
public static Object getProperty(Object bean, String name)
Returns the value of the specified property from the given bean.- Parameters:
bean
- the beanname
- the name of the property to retrieve- Returns:
- the value of this property
- Throws:
InjectionException
- if accessing the property failsIllegalArgumentException
- if invalid parameters are specified
-
setProperty
public static void setProperty(Object bean, String name, Object value)
Sets the value of a property for the specified bean. The given value will be directly written into the property, without performing any type conversions. Occurring exceptions will be re-thrown asInjectionException
s.- Parameters:
bean
- the bean, on which to set the propertyname
- the name of the property to be setvalue
- the new value of the property- Throws:
InjectionException
- if an error occurs when setting the propertyIllegalArgumentException
- if invalid parameters are passed in
-
-