Class 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 Detail

      • MethodInvocation

        public MethodInvocation​(String methodName,
                                ClassDescription[] paramTypes,
                                Dependency... paramValues)
        Creates a new instance of MethodInvocation 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 types
        paramValues - the current parameter values (defined as Dependency 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 of MethodInvocation 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 invoked
        methodName - the name of the method to be invoked (must not be null)
        paramTypes - an array with the parameter types
        paramValues - the current parameter values (defined as Dependency 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 of MethodInvocation 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 the invoke() method.
        Parameters:
        targetClass - the class, on which the method is to be invoked
        methodName - the name of the method to be invoked (must not be null)
        isStatic - determines whether a static method is to be invoked
        paramTypes - an array with the parameter types
        paramValues - the current parameter values (defined as Dependency 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 of MethodInvocation 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 invoked
        targetDep - an optional Dependency to the target bean on which the method should be invoked
        methodName - the name of the method to be invoked (must not be null)
        isStatic - determines whether a static method is to be invoked
        paramTypes - an array with the parameter types
        paramValues - the current parameter values (defined as Dependency 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 target Dependency of this MethodInvocation. 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 the Dependency objects defining the current parameter values. This implementation adds the dependency to the invocation target if it exists.
        Specified by:
        getParameterDependencies in interface Invokable
        Overrides:
        getParameterDependencies in class Invocation
        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 the isStaticInvocation() 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 interface Invokable
        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 error
        IllegalArgumentException - 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 class Invocation
        Parameters:
        buf - the target buffer