org.springframework.util
Class MethodInvoker

java.lang.Object
  extended by org.springframework.util.MethodInvoker
Direct Known Subclasses:
ArgumentConvertingMethodInvoker

public class MethodInvoker
extends Object

Helper class that allows to specify a method to invoke in a declarative fashion, be it static or non-static.

Usage: Specify targetClass/targetMethod respectively targetObject/targetMethod, optionally specify arguments, prepare the invoker. Afterwards, you can invoke the method any number of times.

Typically not used directly but via its subclasses MethodInvokingFactoryBean and MethodInvokingJobDetailFactoryBean.

Since:
19.02.2004
Author:
Colin Sampaleanu, Juergen Hoeller
See Also:
prepare(), invoke(), MethodInvokingFactoryBean, MethodInvokingJobDetailFactoryBean

Nested Class Summary
static class MethodInvoker.VoidType
          Special marker class used for a void return value, differentiating void from a null value returned by the method.
 
Field Summary
static MethodInvoker.VoidType VOID
          Marker now used only by MethodInvokingFactoryBean, but left here for compatibility
 
Constructor Summary
MethodInvoker()
           
 
Method Summary
protected  Method findMatchingMethod()
          Find a matching method with the specified name for the specified arguments.
 Object[] getArguments()
          Retrun the arguments for the method invocation.
 Method getPreparedMethod()
          Return the prepared Method object that will be invoker.
 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.
 Object invoke()
          Invoke the specified method.
 void prepare()
          Prepare the specified method.
 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VOID

public static final MethodInvoker.VoidType VOID
Marker now used only by MethodInvokingFactoryBean, but left here for compatibility

Constructor Detail

MethodInvoker

public MethodInvoker()
Method Detail

setTargetClass

public void setTargetClass(Class targetClass)
Set the target class on which to call the target method. Only necessary when the target method is static; else, a target object needs to be specified anyway.

See Also:
setTargetObject(java.lang.Object), setTargetMethod(java.lang.String)

getTargetClass

public Class getTargetClass()
Return the target class on which to call the target method.


setTargetObject

public void setTargetObject(Object targetObject)
Set the target object on which to call the target method. Only necessary when the target method is not static; else, a target class is sufficient.

See Also:
setTargetClass(java.lang.Class), setTargetMethod(java.lang.String)

getTargetObject

public Object getTargetObject()
Return the target object on which to call the target method.


setTargetMethod

public void setTargetMethod(String targetMethod)
Set the name of the method to be invoked. Refers to either a static method or a non-static method, depending on a target object being set.

See Also:
setTargetClass(java.lang.Class), setTargetObject(java.lang.Object)

getTargetMethod

public String getTargetMethod()
Return the name of the method to be invoked.


setStaticMethod

public void setStaticMethod(String staticMethod)
                     throws ClassNotFoundException
Set a fully qualified static method name to invoke, e.g. "example.MyExampleClass.myExampleMethod". Convenient alternative to specifying targetClass and targetMethod.

Throws:
ClassNotFoundException
See Also:
setTargetClass(java.lang.Class), setTargetMethod(java.lang.String)

setArguments

public void setArguments(Object[] arguments)
Set arguments for the method invocation. If this property is not set, or the Object array is of length 0, a method with no arguments is assumed.


getArguments

public Object[] getArguments()
Retrun the arguments for the method invocation.


prepare

public void prepare()
             throws ClassNotFoundException,
                    NoSuchMethodException
Prepare the specified method. The method can be invoked any number of times afterwards.

Throws:
ClassNotFoundException
NoSuchMethodException
See Also:
getPreparedMethod(), invoke()

findMatchingMethod

protected Method findMatchingMethod()
Find a matching method with the specified name for the specified arguments.

Returns:
a matching method, or null if none
See Also:
getTargetClass(), getTargetMethod(), getArguments()

getPreparedMethod

public Method getPreparedMethod()
Return the prepared Method object that will be invoker. Can for example be used to determine the return type.

See Also:
prepare(), invoke()

invoke

public Object invoke()
              throws InvocationTargetException,
                     IllegalAccessException
Invoke the specified method. The invoker needs to have been prepared before.

Returns:
the object (possibly null) returned by the method invocation, or null if the method has a void return type
Throws:
InvocationTargetException
IllegalAccessException
See Also:
prepare()


Copyright (c) 2002-2006 The Spring Framework Project.