public class MethodInvoker extends Object
Usage: Specify "targetClass"/"targetMethod" or "targetObject"/"targetMethod", optionally specify arguments, prepare the invoker. Afterwards, you may invoke the method any number of times, obtaining the invocation result.
Typically not used directly but via its subclasses
MethodInvokingFactoryBean
and
MethodInvokingJobDetailFactoryBean
.
Constructor and Description |
---|
MethodInvoker() |
Modifier and Type | Method and Description |
---|---|
protected Method |
findMatchingMethod()
Find a matching method with the specified name for the specified arguments.
|
Object[] |
getArguments()
Return the arguments for the method invocation.
|
Method |
getPreparedMethod()
Return the prepared Method object that will be invoked.
|
Class<?> |
getTargetClass()
Return the target class on which to call the target method.
|
String |
getTargetMethod()
Return the name of the method to be invoked.
|
Object |
getTargetObject()
Return the target object on which to call the target method.
|
static int |
getTypeDifferenceWeight(Class<?>[] paramTypes,
Object[] args)
Algorithm that judges the match between the declared parameter types of a candidate method
and a specific list of arguments that this method is supposed to be invoked with.
|
Object |
invoke()
Invoke the specified method.
|
boolean |
isPrepared()
Return whether this invoker has been prepared already,
i.e.
|
void |
prepare()
Prepare the specified method.
|
protected Class<?> |
resolveClassName(String className)
Resolve the given class name into a Class.
|
void |
setArguments(Object[] arguments)
Set arguments for the method invocation.
|
void |
setStaticMethod(String staticMethod)
Set a fully qualified static method name to invoke,
e.g.
|
void |
setTargetClass(Class<?> targetClass)
Set the target class on which to call the target method.
|
void |
setTargetMethod(String targetMethod)
Set the name of the method to be invoked.
|
void |
setTargetObject(Object targetObject)
Set the target object on which to call the target method.
|
public void setTargetClass(Class<?> targetClass)
public Class<?> getTargetClass()
public void setTargetObject(Object targetObject)
public Object getTargetObject()
public void setTargetMethod(String targetMethod)
public String getTargetMethod()
public void setStaticMethod(String staticMethod)
public void setArguments(Object[] arguments)
public Object[] getArguments()
public void prepare() throws ClassNotFoundException, NoSuchMethodException
ClassNotFoundException
NoSuchMethodException
getPreparedMethod()
,
invoke()
protected Class<?> resolveClassName(String className) throws ClassNotFoundException
The default implementations uses ClassUtils.forName
,
using the thread context class loader.
className
- the class name to resolveClassNotFoundException
- if the class name was invalidprotected Method findMatchingMethod()
null
if nonegetTargetClass()
,
getTargetMethod()
,
getArguments()
public Method getPreparedMethod() throws IllegalStateException
Can for example be used to determine the return type.
null
)IllegalStateException
- if the invoker hasn't been prepared yetprepare()
,
invoke()
public boolean isPrepared()
getPreparedMethod()
already.public Object invoke() throws InvocationTargetException, IllegalAccessException
The invoker needs to have been prepared before.
null
if the method has a void return typeInvocationTargetException
- if the target method threw an exceptionIllegalAccessException
- if the target method couldn't be accessedprepare()
public static int getTypeDifferenceWeight(Class<?>[] paramTypes, Object[] args)
Determines a weight that represents the class hierarchy difference between types and arguments. A direct match, i.e. type Integer -> arg of class Integer, does not increase the result - all direct matches means weight 0. A match between type Object and arg of class Integer would increase the weight by 2, due to the superclass 2 steps up in the hierarchy (i.e. Object) being the last one that still matches the required type Object. Type Number and class Integer would increase the weight by 1 accordingly, due to the superclass 1 step up the hierarchy (i.e. Number) still matching the required type Number. Therefore, with an arg of type Integer, a constructor (Integer) would be preferred to a constructor (Number) which would in turn be preferred to a constructor (Object). All argument weights get accumulated.
Note: This is the algorithm used by MethodInvoker itself and also the algorithm used for constructor and factory method selection in Spring's bean container (in case of lenient constructor resolution which is the default for regular bean definitions).
paramTypes
- the parameter types to matchargs
- the arguments to match