|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.util.MethodInvoker
public class MethodInvoker
Helper class that allows for specifying a method to invoke in a declarative fashion, be it static or non-static.
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
.
prepare()
,
invoke()
Field Summary | |
---|---|
private java.lang.Object[] |
arguments
|
private java.lang.reflect.Method |
methodObject
The method we will call |
private java.lang.String |
staticMethod
|
private java.lang.Class |
targetClass
|
private java.lang.String |
targetMethod
|
private java.lang.Object |
targetObject
|
Constructor Summary | |
---|---|
MethodInvoker()
|
Method Summary | |
---|---|
protected java.lang.reflect.Method |
findMatchingMethod()
Find a matching method with the specified name for the specified arguments. |
java.lang.Object[] |
getArguments()
Return the arguments for the method invocation. |
java.lang.reflect.Method |
getPreparedMethod()
Return the prepared Method object that will be invoked. |
java.lang.Class |
getTargetClass()
Return the target class on which to call the target method. |
java.lang.String |
getTargetMethod()
Return the name of the method to be invoked. |
java.lang.Object |
getTargetObject()
Return the target object on which to call the target method. |
static int |
getTypeDifferenceWeight(java.lang.Class[] paramTypes,
java.lang.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. |
java.lang.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 java.lang.Class |
resolveClassName(java.lang.String className)
Resolve the given class name into a Class. |
void |
setArguments(java.lang.Object[] arguments)
Set arguments for the method invocation. |
void |
setStaticMethod(java.lang.String staticMethod)
Set a fully qualified static method name to invoke, e.g. |
void |
setTargetClass(java.lang.Class targetClass)
Set the target class on which to call the target method. |
void |
setTargetMethod(java.lang.String targetMethod)
Set the name of the method to be invoked. |
void |
setTargetObject(java.lang.Object targetObject)
Set the target object on which to call the target method. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
private java.lang.Class targetClass
private java.lang.Object targetObject
private java.lang.String targetMethod
private java.lang.String staticMethod
private java.lang.Object[] arguments
private java.lang.reflect.Method methodObject
Constructor Detail |
---|
public MethodInvoker()
Method Detail |
---|
public void setTargetClass(java.lang.Class targetClass)
setTargetObject(java.lang.Object)
,
setTargetMethod(java.lang.String)
public java.lang.Class getTargetClass()
public void setTargetObject(java.lang.Object targetObject)
setTargetClass(java.lang.Class)
,
setTargetMethod(java.lang.String)
public java.lang.Object getTargetObject()
public void setTargetMethod(java.lang.String targetMethod)
setTargetClass(java.lang.Class)
,
setTargetObject(java.lang.Object)
public java.lang.String getTargetMethod()
public void setStaticMethod(java.lang.String staticMethod)
setTargetClass(java.lang.Class)
,
setTargetMethod(java.lang.String)
public void setArguments(java.lang.Object[] arguments)
public java.lang.Object[] getArguments()
public void prepare() throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException
java.lang.ClassNotFoundException
java.lang.NoSuchMethodException
getPreparedMethod()
,
invoke()
protected java.lang.Class resolveClassName(java.lang.String className) throws java.lang.ClassNotFoundException
The default implementations uses ClassUtils.forName
,
using the thread context class loader.
className
- the class name to resolve
java.lang.ClassNotFoundException
- if the class name was invalidprotected java.lang.reflect.Method findMatchingMethod()
null
if nonegetTargetClass()
,
getTargetMethod()
,
getArguments()
public java.lang.reflect.Method getPreparedMethod() throws java.lang.IllegalStateException
Can for example be used to determine the return type.
null
)
java.lang.IllegalStateException
- if the invoker hasn't been prepared yetprepare()
,
invoke()
public boolean isPrepared()
getPreparedMethod()
already.
public java.lang.Object invoke() throws java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException
The invoker needs to have been prepared before.
null
if the method has a void return type
java.lang.reflect.InvocationTargetException
- if the target method threw an exception
java.lang.IllegalAccessException
- if the target method couldn't be accessedprepare()
public static int getTypeDifferenceWeight(java.lang.Class[] paramTypes, java.lang.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.
paramTypes
- the parameter types to matchargs
- the arguments to match
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |