Class MethodInvocationBeanProvider

  • All Implemented Interfaces:
    BeanProvider

    public class MethodInvocationBeanProvider
    extends SimpleBeanProvider

    A specialized BeanProvider that creates beans by invoking a method.

    This BeanProvider implementation is initialized with a MethodInvocation object describing the method to be invoked. Optional a Dependency can be provided for the instance, on which the method is to be invoked (if no such dependency is specified, the method to be invoked must be static). This class is intended for the following use cases:

    • It can be used for obtaining instances that are not created using a constructor, but by invoking a static factory method. In this case the passed in MethodInvocation must refer to this factory method and must provide the parameters for this invocation.
    • Another use case is the handling of factory classes: Some objects are not directly created, but a method on a specific factory class is used for this purpose. An example could be a Connection object that is obtained from a DataSource. In this case the factory is defined as a separate bean, and a dependency to this bean is specified to this bean provider class. The MethodInvocation defines the method of this factory bean that has to be called for obtaining an instance.

    As is true for other SimpleBeanProviders, this provider is intended to be used together with a life-cycle-aware BeanProvider. It does not provide any life-cycle support on its own.

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

      • MethodInvocationBeanProvider

        public MethodInvocationBeanProvider​(Dependency targetBean,
                                            MethodInvocation methodInv)
        Creates a new instance of MethodInvocationBeanProvider and initializes it with the dependency to the target object (on which the method is to be invoked) and the description of the method invocation.
        Parameters:
        targetBean - the dependency to the target bean (can be null)
        methodInv - the description of the method to invoke (must not be null)
        Throws:
        IllegalArgumentException - if the MethodInvocation is undefined
      • MethodInvocationBeanProvider

        public MethodInvocationBeanProvider​(MethodInvocation methodInv)
        Creates a new instance of MethodInvocationBeanProvider and initializes it with the description of the method to invoke. No dependency for the target instance is provided, so a static method must be specified.
        Parameters:
        methodInv - the description of the method to invoke (must not be null)
        Throws:
        IllegalArgumentException - if the MethodInvocation is undefined
      • MethodInvocationBeanProvider

        public MethodInvocationBeanProvider​(Dependency targetBean,
                                            MethodInvocation methodInv,
                                            ClassDescription beanClsDsc)
        Creates a new instance of MethodInvocationBeanProvider and initializes it with the dependency to the target object (on which the method is to be invoked), the description of the method invocation, and the class of the managed bean. Depending on the used dependency and/or the method invocation, it is not always possible to determine the class of the managed bean. With this constructor it can be explicitly set.
        Parameters:
        targetBean - the dependency to the target bean (can be null)
        methodInv - the description of the method to invoke (must not be null)
        beanClsDsc - a description of the class of the managed bean (can be null)
        Throws:
        IllegalArgumentException - if the MethodInvocation is undefined
    • Method Detail

      • getTargetDependency

        public Dependency getTargetDependency()
        Returns the dependency to the target bean. This can be null if none is provided.
        Returns:
        the dependency to the target bean
      • getInvocation

        public MethodInvocation getInvocation()
        Returns the MethodInvocation for the method to be invoked.
        Returns:
        the method invocation
      • getBeanClassDescription

        public ClassDescription getBeanClassDescription()
        Returns the description of the class of the managed bean. If a class description was explicitly set in the constructor, this description is returned. Otherwise this implementation tries to obtain the bean class from the MethodInvocation object owned by this provider. Because a class is optional for a method invocation, result can be null. To avoid this, a valid ClassDescription should always be set either on the MethodInvocation or when an instance of this class is constructed.
        Returns:
        a class description of the managed bean
      • getBean

        public Object getBean​(DependencyProvider dependencyProvider)
        Returns the bean managed by this provider. If a target dependency is set, the corresponding bean will be fetched from the specified dependency provider and used as target instance for the method invocation. Otherwise the method is invoked on a null instance, so this has to be a static method.
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the bean managed by this provider
      • getBeanClass

        public Class<?> getBeanClass​(DependencyProvider dependencyProvider)
        Returns the bean class of the bean managed by this provider. This implementation delegates to getBeanClassDescription() for obtaining the description of the bean class. If this is successful, the class is resolved; otherwise result is null.
        Parameters:
        dependencyProvider - the dependency provider
        Returns:
        the class of the managed bean
        See Also:
        getBeanClassDescription()
      • getDependencies

        public Set<Dependency> getDependencies()
        Returns the dependencies of this bean provider. These are the parameter dependencies of the MethodInvocation object. If a dependency for the target instance is provided, it will also be contained in the returned set.
        Specified by:
        getDependencies in interface BeanProvider
        Overrides:
        getDependencies in class SimpleBeanProvider
        Returns:
        a set with the dependencies of this provider
        See Also:
        Dependency
      • toString

        public String toString()
        Returns a string representation for this object. This string will contain information about the method invoked by this bean provider. If a target dependency is provided, it will also be output.
        Overrides:
        toString in class Object
        Returns:
        a string for this object