Package net.sf.jguiraffe.di.impl
Class MethodInvocation
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.Invocation
-
- net.sf.jguiraffe.di.impl.MethodInvocation
-
- All Implemented Interfaces:
Invokable
public class MethodInvocation extends Invocation implements Invokable
A class that represents a method invocation.
This class stores all data, which is needed for invoking a method; i.e. the method name, the target class, optional information about the data types of the method parameters, and the current parameter values to be passed to the method.
Once initialized, an instance is immutable. So it can easily be shared between multiple components and threads without having to care about synchronization issues. The
invoke()
method actually executes the corresponding method.- Version:
- $Id: MethodInvocation.java 207 2012-02-09 07:30:13Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description MethodInvocation(String methodName, ClassDescription[] paramTypes, Dependency... paramValues)
Creates a new instance ofMethodInvocation
for non-static method invocations.MethodInvocation(ClassDescription targetClass, String methodName, boolean isStatic, ClassDescription[] paramTypes, Dependency... paramValues)
Creates a new instance ofMethodInvocation
and initializes most of the properties.MethodInvocation(ClassDescription targetClass, String methodName, ClassDescription[] paramTypes, Dependency... paramValues)
Creates a new instance ofMethodInvocation
and initializes it with information about the method to invoke and the target class.MethodInvocation(ClassDescription targetClass, Dependency targetDep, String methodName, boolean isStatic, ClassDescription[] paramTypes, Dependency... paramValues)
Creates a new instance ofMethodInvocation
and fully initializes it.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description String
getMethodName()
Returns the name of the method to be invoked.List<Dependency>
getParameterDependencies()
Returns theDependency
objects defining the current parameter values.Dependency
getTargetDependency()
Returns the targetDependency
of thisMethodInvocation
.protected void
invocationInfoToString(StringBuilder buf)
Creates a string with additional information about this invocation.Object
invoke(DependencyProvider depProvider, Object target)
Invokes the corresponding method on the specified target instance.boolean
isStaticInvocation()
Returns the static flag.-
Methods inherited from class net.sf.jguiraffe.di.impl.Invocation
checkDependencyProvider, getParameterClasses, getParameterTypes, getResolvedParameters, getTargetClass, isTypeInfoComplete, parametersToString, toString
-
-
-
-
Constructor Detail
-
MethodInvocation
public MethodInvocation(String methodName, ClassDescription[] paramTypes, Dependency... paramValues)
Creates a new instance ofMethodInvocation
for non-static method invocations. This constructor sets the target class to null, so that it can only be derived from the target instance (which prohibits static method invocations).- Parameters:
methodName
- the name of the method to be invoked (must not be null)paramTypes
- an array with the parameter typesparamValues
- the current parameter values (defined asDependency
objects); this array must not contain null elements- Throws:
IllegalArgumentException
- if the length of the parameter types array does not match the length of the parameter values array, or if the values array contains null elements, or if the method name is undefined
-
MethodInvocation
public MethodInvocation(ClassDescription targetClass, String methodName, ClassDescription[] paramTypes, Dependency... paramValues)
Creates a new instance ofMethodInvocation
and initializes it with information about the method to invoke and the target class.- Parameters:
targetClass
- the class, on which the method is to be invokedmethodName
- the name of the method to be invoked (must not be null)paramTypes
- an array with the parameter typesparamValues
- the current parameter values (defined asDependency
objects); this array must not contain null elements- Throws:
IllegalArgumentException
- if the length of the parameter types array does not match the length of the parameter values array, or if the values array contains null elements, or if the method name is undefined- See Also:
Invocation#Invocation(Class, Class[], Dependency...)
-
MethodInvocation
public MethodInvocation(ClassDescription targetClass, String methodName, boolean isStatic, ClassDescription[] paramTypes, Dependency... paramValues)
Creates a new instance ofMethodInvocation
and initializes most of the properties. This constructor is appropriate for static or non-static invocations which are performed on the target object passed to theinvoke()
method.- Parameters:
targetClass
- the class, on which the method is to be invokedmethodName
- the name of the method to be invoked (must not be null)isStatic
- determines whether a static method is to be invokedparamTypes
- an array with the parameter typesparamValues
- the current parameter values (defined asDependency
objects); this array must not contain null elements- Throws:
IllegalArgumentException
- if the length of the parameter types array does not match the length of the parameter values array, or if the values array contains null elements, or if the method name is undefined, or if the static flag is true, but no target class is defined- See Also:
Invocation#Invocation(Class, Class[], Dependency...)
-
MethodInvocation
public MethodInvocation(ClassDescription targetClass, Dependency targetDep, String methodName, boolean isStatic, ClassDescription[] paramTypes, Dependency... paramValues)
Creates a new instance ofMethodInvocation
and fully initializes it. This constructor takes all information required for arbitrary method invocations. It is especially possible to define a dependency for the target object. If set, this dependency is resolved during invocation; a target object is then ignored. Refer to the base class for a detailed explanation of the arguments.- Parameters:
targetClass
- the class, on which the method is to be invokedtargetDep
- an optionalDependency
to the target bean on which the method should be invokedmethodName
- the name of the method to be invoked (must not be null)isStatic
- determines whether a static method is to be invokedparamTypes
- an array with the parameter typesparamValues
- the current parameter values (defined asDependency
objects); this array must not contain null elements- Throws:
IllegalArgumentException
- if the length of the parameter types array does not match the length of the parameter values array, or if the values array contains null elements, or if the method name is undefined, or if the static flag is true, but no target class is defined- Since:
- 1.1
- See Also:
Invocation#Invocation(Class, Class[], Dependency...)
-
-
Method Detail
-
getMethodName
public String getMethodName()
Returns the name of the method to be invoked.- Returns:
- the method name
-
isStaticInvocation
public boolean isStaticInvocation()
Returns the static flag. This flag indicates whether a static method is to be invoked.- Returns:
- the static invocation flag
-
getTargetDependency
public Dependency getTargetDependency()
Returns the targetDependency
of thisMethodInvocation
. This dependency defines the bean on which the method is to be invoked. If there is no target dependency, result is null.- Returns:
- the target
Dependency
- Since:
- 1.1
-
getParameterDependencies
public List<Dependency> getParameterDependencies()
Returns theDependency
objects defining the current parameter values. This implementation adds the dependency to the invocation target if it exists.- Specified by:
getParameterDependencies
in interfaceInvokable
- Overrides:
getParameterDependencies
in classInvocation
- Returns:
- a list with the
Dependency
objects for the parameter values
-
invoke
public Object invoke(DependencyProvider depProvider, Object target)
Invokes the corresponding method on the specified target instance. The method's result is returned. The behavior of this method depends on theisStaticInvocation()
flag. If it is set, a passed in target object is ignored and a static method invocation on the target class is performed. Otherwise, if a non null target object is passed in, the target class is derived from this instance (an eventually set target class is ignored).- Specified by:
invoke
in interfaceInvokable
- Parameters:
depProvider
- the dependency provider for resolving the parameters (must not be null)target
- the target instance, on which to invoke the method- Returns:
- the method's return value
- Throws:
InjectionException
- in case of an errorIllegalArgumentException
- if the dependency provider is null
-
invocationInfoToString
protected void invocationInfoToString(StringBuilder buf)
Creates a string with additional information about this invocation. This implementation will output the method name.- Overrides:
invocationInfoToString
in classInvocation
- Parameters:
buf
- the target buffer
-
-