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.
Modifier and Type | Method and 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.
|
public static Class<?> loadClass(String className, 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 useIllegalArgumentException
- if the class name or the class loader is
undefinedInjectionException
- if the class cannot be resolvedpublic static Object invokeMethod(Method method, Object target, Object... args)
method
- the method to be invokedtarget
- the target objectargs
- the arguments of the methodInjectionException
- if invoking the method causes an errorIllegalArgumentException
- if the method object is nullpublic static <T> T invokeConstructor(Constructor<T> ctor, Object... args)
invokeMethod()
, this is a helper method
that deals with all possible exceptions and redirects them as
InjectionException
s.T
- the type of the constructorctor
- the constructor to be invoked (must not be null)args
- the arguments to be passed to the constructorInjectionException
- if construction of the object failsIllegalArgumentException
- if the constructor object is nullpublic static List<Method> findMethods(Class<?> targetClass, String methodName, Class<?>[] paramTypes, boolean exactTypeMatch)
paramTypes
can be null, then
arbitrary parameter types are accepted at this place.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:
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
exactlyIllegalArgumentException
- if the target class is nullpublic static List<Method> removeCovariantDuplicates(List<Method> methods)
findMethods(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.methods
- the list with methods to be checked (must not be
nullIllegalArgumentException
- if the passed in list is null or
contain null elementspublic static <T> List<Constructor<T>> findConstructors(Class<T> targetClass, Class<?>[] paramTypes, boolean exactTypeMatch)
findMethods(Class, String, Class[], boolean)
,
but deals with constructors of the target class.T
- the type of the constructortargetClass
- the target class (must not be null)paramTypes
- an array with the parameter typesexactTypeMatch
- a flag whether parameter types should be matched
exactlyIllegalArgumentException
- if the target class is nullpublic static Object getProperty(Object bean, String name)
bean
- the beanname
- the name of the property to retrieveInjectionException
- if accessing the property failsIllegalArgumentException
- if invalid parameters are specifiedpublic static void setProperty(Object bean, String name, Object value)
InjectionException
s.bean
- the bean, on which to set the propertyname
- the name of the property to be setvalue
- the new value of the propertyInjectionException
- if an error occurs when setting the propertyIllegalArgumentException
- if invalid parameters are passed inCopyright © 2016 The JGUIraffe Team. All rights reserved.