public class InvocationHelper extends Object
A helper class providing some more complex functionality related to reflection.
This class builds on top of ReflectionUtils
which implements
low-level utility methods for various operations related to reflection.
InvocationHelper
in contrast focuses on some more advanced
operations. It deals with stuff like finding suitable methods and
constructors to be invoked, getting and setting properties, and data type
conversions (this is delegated to an instance of ConversionHelper
which is maintained by this class).
Implementation note: This class is thread-safe.
Constructor and Description |
---|
InvocationHelper()
Creates a new instance of
InvocationHelper . |
InvocationHelper(ConversionHelper convHlp)
Creates a new instance of
InvocationHelper and initializes it
with the given ConversionHelper instance. |
Modifier and Type | Method and Description |
---|---|
protected Object[] |
convertArguments(Class<?>[] parameterTypes,
Object[] args)
Performs necessary type conversions before invoking a method.
|
<T> Constructor<T> |
findUniqueConstructor(Class<T> targetClass,
Class<?>[] parameterTypes,
Object[] args)
Tries to match a single constructor of the specified target class given
the parameter types and the call arguments.
|
Method |
findUniqueMethod(Class<?> targetClass,
String methodName,
Class<?>[] parameterTypes,
Object[] args)
Determines the method to be called given the name, the parameter types,
and the arguments.
|
ConversionHelper |
getConversionHelper()
Returns the
ConversionHelper instance associated with this
object. |
<T> T |
invokeConstructor(Class<T> targetClass,
Class<?>[] parameterTypes,
Object[] args)
Invokes a constructor.
|
Object |
invokeInstanceMethod(Object instance,
String methodName,
Class<?>[] parameterTypes,
Object[] args)
Invokes a method on the specified object.
|
Object |
invokeMethod(Class<?> targetClass,
Object instance,
String methodName,
Class<?>[] parameterTypes,
Object[] args)
Invokes a method.
|
Object |
invokeStaticMethod(Class<?> targetClass,
String methodName,
Class<?>[] parameterTypes,
Object[] args)
Invokes the specified static method and returns its result.
|
void |
setProperty(Object bean,
String property,
Object value)
Sets a property of the given bean.
|
public InvocationHelper()
InvocationHelper
. A default
ConversionHelper
instance is set.public InvocationHelper(ConversionHelper convHlp)
InvocationHelper
and initializes it
with the given ConversionHelper
instance. If no helper object is
provided, a new default instance is created.convHlp
- the ConversionHelper
public ConversionHelper getConversionHelper()
ConversionHelper
instance associated with this
object.ConversionHelper
public Method findUniqueMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes, Object[] args)
ReflectionUtils.findMethods(Class, String, Class[], boolean)
to
find a single method that matches the specified method signature. It also
takes the specified method arguments into account in order to find a
unique match. If this is not possible, an exception is thrown.targetClass
- the class on which to invoke the methodmethodName
- the name of the method to be invokedparameterTypes
- an array with the known parameter types (may be
null or contain null entries representing wild
cards)args
- an array with the method argumentsInjectionException
- if the method cannot be determinedIllegalArgumentException
- if the target class is nullpublic <T> Constructor<T> findUniqueConstructor(Class<T> targetClass, Class<?>[] parameterTypes, Object[] args)
findUniqueMethod(Class, String, Class[], Object[])
, but it
searches for a matching constructor.T
- the type of the target classtargetClass
- the class on which to invoke the methodparameterTypes
- an array with the known parameter types (may be
null or contain null entries representing wild
cards)args
- an array with the call argumentsInjectionException
- if no unique constructor can be determinedIllegalArgumentException
- if the target class is nullpublic Object invokeStaticMethod(Class<?> targetClass, String methodName, Class<?>[] parameterTypes, Object[] args)
targetClass
- the target class on which to invoke the method (must
not be null)methodName
- the name of the methodparameterTypes
- an array with the known parameter types (may be
null or contain null entries representing wild
cards)args
- an array with the method argumentsInjectionException
- if an exception occursIllegalArgumentException
- if the target class is undefinedpublic Object invokeInstanceMethod(Object instance, String methodName, Class<?>[] parameterTypes, Object[] args)
invokeMethod(Class, Object, String, Class[], Object[])
.instance
- the instance on which to invoke the methodmethodName
- the name of the method to be invokedparameterTypes
- an array with the known parameter types (may be
null or contain null entries representing wild
cards)args
- an array with the method argumentsInjectionException
- if an exception occursIllegalArgumentException
- if the instance is undefinedpublic Object invokeMethod(Class<?> targetClass, Object instance, String methodName, Class<?>[] parameterTypes, Object[] args)
findUniqueMethod(Class, String, Class[], Object[])
to find the
method to be invoked. Then it performs necessary type conversions of the
parameters. Eventually, it invokes the method. Using this method it is
possible to invoke a specific method in a given class, even if the
parameter types are not or only partly known. All possible exceptions
(e.g. no unique method is found, the parameters cannot be converted,
reflection-related exceptions) are thrown as InjectionException
exceptions.targetClass
- the target class on which to invoke the method (may be
null if an object instance is provided)instance
- the instance on which to invoke the method (may be
null for static methods)methodName
- the name of the method to be invokedparameterTypes
- an array with the known parameter types (may be
null or contain null entries representing wild
cards)args
- an array with the method argumentsInjectionException
- if an exception occursIllegalArgumentException
- if a required parameter is missing or
the specification of the method to be called is invalidpublic <T> T invokeConstructor(Class<T> targetClass, Class<?>[] parameterTypes, Object[] args)
findUniqueConstructor(Class, Class[], Object[])
to obtain the
constructor to be invoked. Then it performs necessary type conversions of
the parameters. Eventually, it invokes the constructor and returns the
newly created instance. Using this method it is possible to invoke a
specific constructor of a given class, even if the parameter types are
not or only partly known. All possible exceptions (e.g. no unique method
is found, the parameters cannot be converted, reflection-related
exceptions) are thrown as InjectionException
exceptions.T
- the type of the target classtargetClass
- the target class on which the constructor is to be
invokedparameterTypes
- an array with the known parameter types (may be
null or contain null entries representing wild
cards)args
- an array with the method argumentsInjectionException
- if an exception occursIllegalArgumentException
- if the specification of the constructor
is invalidpublic void setProperty(Object bean, String property, Object value)
InjectionException
(if they are related to reflection operations)
or as IllegalArgumentException
if they are related to the
parameters passed to this method.bean
- the bean on which to set the propertyproperty
- the name of the property to be setvalue
- the value of the propertyInjectionException
- if an error occurs related to reflectionIllegalArgumentException
- if invalid arguments are passed inprotected Object[] convertArguments(Class<?>[] parameterTypes, Object[] args)
parameterTypes
- the array with the parameter typesargs
- the array with the call argumentsInjectionException
- if a conversion failsCopyright © 2016 The JGUIraffe Team. All rights reserved.