|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sf.jguiraffe.di.ReflectionUtils
public class ReflectionUtils
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.
| Method Summary | ||
|---|---|---|
static
|
convert(java.lang.Class<T> targetClass,
java.lang.Object value)
Performs a type conversion if required. |
|
static
|
findConstructorByParamTypes(java.lang.Class<T> targetClass,
java.lang.Class<?>... types)
Finds a constructor in the specified target class when all parameter types are known. |
|
static
|
findConstructorByParamValues(java.lang.Class<T> targetClass,
java.lang.Class<?>[] types,
java.lang.Object... values)
Finds a constructor if not all parameter types are exactly known. |
|
static java.lang.reflect.Method |
findMethodByParamTypes(java.lang.Class<?> targetClass,
java.lang.String methodName,
java.lang.Class<?>... types)
Finds a method in the specified target class when all parameter types are known. |
|
static java.lang.reflect.Method |
findMethodByParamValues(java.lang.Class<?> targetClass,
java.lang.String methodName,
java.lang.Class<?>[] types,
java.lang.Object... values)
Finds a method if not all parameter types are exactly known. |
|
static java.lang.Object |
getProperty(java.lang.Object bean,
java.lang.String name)
Returns the value of the specified property from the given bean. |
|
static
|
invokeConstructor(java.lang.reflect.Constructor<T> ctor,
java.lang.Object... args)
Creates an object by invoking the specified constructor with the given arguments. |
|
static
|
invokeConstructorByParamTypes(java.lang.Class<T> targetClass,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
Finds a constructor and invokes it when the exact signature is known. |
|
static
|
invokeConstructorByParamValues(java.lang.Class<T> targetClass,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
Finds a constructor and invokes it when the signature is only partly known. |
|
static java.lang.Object |
invokeMethod(java.lang.reflect.Method method,
java.lang.Object target,
java.lang.Object... args)
Helper method for invoking a method using reflection. |
|
static java.lang.Object |
invokeMethodByParamTypes(java.lang.Class<?> targetClass,
java.lang.String methodName,
java.lang.Object target,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
Finds a method and invokes it when all its parameter types are known. |
|
static java.lang.Object |
invokeMethodByParamTypes(java.lang.String methodName,
java.lang.Object target,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
Finds a method and invokes it on the specified target object when all its parameter types are known. |
|
static java.lang.Object |
invokeMethodByParamValues(java.lang.Class<?> targetClass,
java.lang.String methodName,
java.lang.Object target,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
Finds a method and invokes it when the parameter types are only partly known. |
|
static java.lang.Object |
invokeMethodByParamValues(java.lang.String methodName,
java.lang.Object target,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
Finds a method and invokes it on the specified target object when the parameter types are only partly known. |
|
static java.lang.Class<?> |
loadClass(java.lang.String className,
java.lang.ClassLoader loader)
Loads the class with the specified name using the given class loader. |
|
static void |
setProperty(java.lang.Object bean,
java.lang.String name,
java.lang.Object value)
Sets the value of a property for the specified bean. |
|
static void |
setPropertyWithConversion(java.lang.Object bean,
java.lang.String name,
java.lang.Object value)
Sets the value of a property for the specified bean applying type conversions if necessary. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static java.lang.Class<?> loadClass(java.lang.String className,
java.lang.ClassLoader loader)
Class.forName() method.
ClassNotFoundException exceptions are caught and re-thrown
as InjectionException exceptions.
className - the name of the class to be loadedloader - the class loader to use
java.lang.IllegalArgumentException - if the class name or the class loader is
undefined
InjectionException - if the class cannot be resolved
public static java.lang.Object invokeMethod(java.lang.reflect.Method method,
java.lang.Object target,
java.lang.Object... args)
method - the method to be invokedtarget - the target objectargs - the arguments of the method
InjectionException - if invoking the method causes an error
java.lang.IllegalArgumentException - if the method object is null
public static <T> T invokeConstructor(java.lang.reflect.Constructor<T> ctor,
java.lang.Object... args)
invokeMethod(), this is a helper method
that deals with all possible exceptions and redirects them as
InjectionExceptions.
ctor - the constructor to be invoked (must not be null)args - the arguments to be passed to the constructor
InjectionException - if construction of the object fails
java.lang.IllegalArgumentException - if the constructor object is null
public static java.lang.reflect.Method findMethodByParamTypes(java.lang.Class<?> targetClass,
java.lang.String methodName,
java.lang.Class<?>... types)
targetClass - the target classmethodName - the name of the searched methodtypes - an array with the types of all parameters
InjectionException - if the method cannot be found
java.lang.IllegalArgumentException - if the method name is null or
the target class is null
public static java.lang.reflect.Method findMethodByParamValues(java.lang.Class<?> targetClass,
java.lang.String methodName,
java.lang.Class<?>[] types,
java.lang.Object... values)
Finds a method if not all parameter types are exactly known.
This method can be used if a method is to be invoked and only the current parameter values (and maybe some of the exact parameter types) are known. In this case the signature of the method cannot directly be derived from the values of the parameter objects because calling the method could involve some legal type conversions like unboxing or widening conversions (see The Java Language Specification). So each parameter value has to be checked for compatibility with the type defined in the method signature.
This method expects two arrays as arguments: The 2nd array holds the parameter values to be passed to the method. The first array contains the known types of the signature. Its number of elements must equal the number of elements of the 2nd array. If the signature of the method to be called is partly known, the elements with the corresponding parameter indices can contain the exact parameter class. Otherwise they must be set to null. This implementation will iterate over the two arrays. If the current element of the first array is not null, the corresponding parameter type of the currently tested method is checked for equality. Otherwise it is checked for compatibility with the current element of the parameter values array.
targetClass - the class, on which to find the method (must not be
null)methodName - the name of the method to invoke (must not be null)types - an array with the partly known exact parameter types of the
searched method (unknown parameter types are represented by null
values in this array)values - an array with the current parameter values
InjectionException - if no compatible method can be found
java.lang.IllegalArgumentException - if either the target class is null
or the method name is null or the number of elements in the
arrays differ
public static <T> java.lang.reflect.Constructor<T> findConstructorByParamTypes(java.lang.Class<T> targetClass,
java.lang.Class<?>... types)
targetClass - the target classtypes - an array with the types of all parameters
InjectionException - if the constructor cannot be found
public static <T> java.lang.reflect.Constructor<T> findConstructorByParamValues(java.lang.Class<T> targetClass,
java.lang.Class<?>[] types,
java.lang.Object... values)
findMethodByParamValues(), but searches
for a compatible constructor rather than a method.
targetClass - the class, on which to find the method (must not be
null)types - an array with the partly known exact parameter types of the
searched method (unknown parameter types are represented by null
values in this array)values - an array with the current parameter values
InjectionException - if no compatible constructor can be found
java.lang.IllegalArgumentException - if either the target class is null
or the number of elements in the arrays differ
public static java.lang.Object invokeMethodByParamTypes(java.lang.Class<?> targetClass,
java.lang.String methodName,
java.lang.Object target,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
findMethodByParamTypes() is called for retrieving
the method to be called. Then it is actually invoked passing in the
specified parameters.
targetClass - the target class the method belongs tomethodName - the name of the method to invoketarget - the target object, on which to invoke the methodparamTypes - the classes of the parameters; specifies the signature
of the method to invokeparamValues - the actual values to be passed to the method
InjectionException - if an error occurs during method invocation
java.lang.IllegalArgumentException - if invalid arguments are passed infindMethodByParamTypes(Class, String, Class[]),
invokeMethod(Method, Object, Object...)
public static java.lang.Object invokeMethodByParamTypes(java.lang.String methodName,
java.lang.Object target,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
methodName - the name of the method to invoketarget - the target object, on which to invoke the methodparamTypes - the classes of the parameters; specifies the signature
of the method to invokeparamValues - the actual values to be passed to the method
InjectionException - if an error occurs during method invocation
java.lang.IllegalArgumentException - if invalid arguments are passed in
public static java.lang.Object invokeMethodByParamValues(java.lang.Class<?> targetClass,
java.lang.String methodName,
java.lang.Object target,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
findMethodByParamValues() is called for
retrieving the method to be called. Then it is actually invoked passing
in the specified parameters.
targetClass - the target class the method belongs tomethodName - the name of the method to invoketarget - the target object, on which to invoke the methodparamTypes - the classes of the parameters; specifies the signature
of the method to invoke; array elements that represent unknown parameter
types must be set to nullparamValues - the actual values to be passed to the method
InjectionException - if an error occurs during method invocation
java.lang.IllegalArgumentException - if invalid arguments are passed infindMethodByParamValues(Class, String, Class[], Object[]),
invokeMethod(Method, Object, Object...)
public static java.lang.Object invokeMethodByParamValues(java.lang.String methodName,
java.lang.Object target,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
methodName - the name of the method to invoketarget - the target object, on which to invoke the methodparamTypes - the classes of the parameters; specifies the signature
of the method to invoke; array elements that represent unknown parameter
types must be set to nullparamValues - the actual values to be passed to the method
InjectionException - if an error occurs during method invocation
java.lang.IllegalArgumentException - if invalid arguments are passed in
public static <T> T invokeConstructorByParamTypes(java.lang.Class<T> targetClass,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
findConstructorByParamTypes() will
be called. Then the found Constructor object is passed to
invokeConstructor().
targetClass - the target class, to which the constructor belongsparamTypes - an array with the parameter types of the constructorparamValues - the current parameter values to be passed to the
constructor
InjectionException - if invocation of the constructor causes an
error
java.lang.IllegalArgumentException - if invalid parameters are passed infindConstructorByParamTypes(Class, Class...),
invokeConstructor(Constructor, Object...)
public static <T> T invokeConstructorByParamValues(java.lang.Class<T> targetClass,
java.lang.Class<?>[] paramTypes,
java.lang.Object... paramValues)
findConstructorByParamValues() will be called. Then the
found Constructor object is passed to
invokeConstructor().
targetClass - the target class, to which the constructor belongsparamTypes - an array with the parameter types of the constructor
(unknown types are represented by null elements)paramValues - the current parameter values to be passed to the
constructor
InjectionException - if invocation of the constructor causes an
error
java.lang.IllegalArgumentException - if invalid parameters are passed infindConstructorByParamValues(Class, Class[], Object...),
invokeConstructor(Constructor, Object...)
public static java.lang.Object getProperty(java.lang.Object bean,
java.lang.String name)
bean - the beanname - the name of the property to retrieve
InjectionException - if accessing the property fails
java.lang.IllegalArgumentException - if invalid parameters are specified
public static void setProperty(java.lang.Object bean,
java.lang.String name,
java.lang.Object value)
InjectionExceptions.
bean - the bean, on which to set the propertyname - the name of the property to be setvalue - the new value of the property
InjectionException - if an error occurs when setting the property
java.lang.IllegalArgumentException - if invalid parameters are passed in
public static void setPropertyWithConversion(java.lang.Object bean,
java.lang.String name,
java.lang.Object value)
BeanUtils.copyProperty() method from Apache Commons Beanutils.
Type conversions are handled by this library; especially specific
converters can be registered for custom data types.
bean - the bean, on which to set the propertyname - the name of the property to be setvalue - the new value of the property
InjectionException - if an error occurs when setting the property
java.lang.IllegalArgumentException - if invalid parameters are passed in
public static <T> T convert(java.lang.Class<T> targetClass,
java.lang.Object value)
T - the type of the resulting objecttargetClass - the target class to convert to (if null, no
conversion will be performed)value - the object to be converted
java.lang.IllegalArgumentException - if conversion fails
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||