Class ConstructorInvocation

  • All Implemented Interfaces:
    Invokable

    public class ConstructorInvocation
    extends Invocation
    implements Invokable

    A class that represents a constructor invocation.

    Instances of this class can be used for creating objects by invoking constructors on their classes. For this purpose the parameters to be passed to the constructor must be specified, which is done in form of an array of Dependency objects. These dependencies can either refer to other beans (they will then be resolved by the dependency injection framework), or can contain constant values that will be directly passed to the constructor. If the constructor's signature is known, the types of its arguments can be defined. If this is not the case, the parameter types will be derived from the current parameter values. The InvocationHelper class is used to find the correct constructor to be invoked.

    Objects of this class cannot be changed after they have been created. So they can be shared between multiple clients.

    Version:
    $Id: ConstructorInvocation.java 205 2012-01-29 18:29:57Z oheger $
    Author:
    Oliver Heger
    • Constructor Detail

      • ConstructorInvocation

        public ConstructorInvocation​(ClassDescription targetClass,
                                     ClassDescription[] paramTypes,
                                     Dependency... paramValues)
        Creates a new instance of ConstructorInvocation and initializes it. Refer to the base class for a detailed explanation of the arguments.
        Parameters:
        targetClass - the class, on which the method is 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, of if the target class is null
        See Also:
        Invocation#Invocation(Class, Class[], Dependency...)
    • Method Detail

      • invoke

        public Object invoke​(DependencyProvider depProvider)
        Invokes the corresponding constructor on the specified target class and returns the newly created instance. The required dependencies are resolved using the given DependencyProvider.
        Parameters:
        depProvider - the dependency provider (must not be null)
        Returns:
        the newly created instance of the target class
        Throws:
        InjectionException - if an error occurs
        IllegalArgumentException - if the dependency provider is null
      • invoke

        public Object invoke​(DependencyProvider depProvider,
                             Object target)
        Performs the invocation. This method is required by the Invokable interface. It delegates to the method with the same name, ignoring the target parameter (which is not needed for a constructor invocation).
        Specified by:
        invoke in interface Invokable
        Parameters:
        depProvider - the dependency provider (must not be null)
        target - the target of the invocation
        Returns:
        the result of the invocation
        Throws:
        InjectionException - if an error occurs
        IllegalArgumentException - if the dependency provider is null
      • invocationInfoToString

        protected void invocationInfoToString​(StringBuilder buf)
        Adds additional information about this invocation to the string buffer. This implementation adds a name for the constructor.
        Overrides:
        invocationInfoToString in class Invocation
        Parameters:
        buf - the target buffer