    001    package org.picocontainer.injectors;
    002
    003    import org.picocontainer.ComponentAdapter;
    004    import org.picocontainer.ComponentMonitor;
    005    import org.picocontainer.LifecycleStrategy;
    006    import org.picocontainer.Parameter;
    007
    008    import java.lang.annotation.Annotation;
    009
    010    /**
    011     * convenience class providing static methods to conveniently create injectors
    012     * ( like org.junit.Assert )
    013     *
    014     * @author Konstantin Pribluda
    015     */
    016    public class Injector {
    017        /**
    018         * Constructor injector that uses no monitor and no lifecycle adapter.  This is a more
    019         * convenient constructor for use when instantiating a constructor injector directly.
    020         *
    021         * @param componentKey            the search key for this implementation
    022         * @param componentImplementation the concrete implementation
    023         * @param parameters              the parameters used for initialization
    024         */
    025
    026        public static ComponentAdapter constructor(final Object componentKey, final Class<?> componentImplementation, Parameter... parameters) {
    027            return new ConstructorInjector(componentKey, componentImplementation, parameters);
    028        }
    029
    030        /**
    031         * Creates a ConstructorInjector
    032         *
    033         * @param componentKey            the search key for this implementation
    034         * @param componentImplementation the concrete implementation
    035         * @param parameters              the parameters to use for the initialization
    036         * @param monitor                 the component monitor used by this addAdapter
    037         * @param lifecycleStrategy       the component lifecycle strategy used by this addAdapter
    038         * @param useNames                use argument names when looking up dependencies
    039         * @throws org.picocontainer.injectors.AbstractInjector.NotConcreteRegistrationException
    040         *                              if the implementation is not a concrete class.
    041         * @throws NullPointerException if one of the parameters is <code>null</code>
    042         */
    043        public static ComponentAdapter constructor(final Object componentKey, final Class componentImplementation, Parameter[] parameters, ComponentMonitor monitor,
    044                                                   LifecycleStrategy lifecycleStrategy, boolean useNames) throws AbstractInjector.NotConcreteRegistrationException {
    045            return new ConstructorInjector(componentKey, componentImplementation, parameters, monitor, lifecycleStrategy, useNames);
    046        }
    047
    048        /**
    049         * Creates a ConstructorInjector
    050         *
    051         * @param componentKey            the search key for this implementation
    052         * @param componentImplementation the concrete implementation
    053         * @param parameters              the parameters to use for the initialization
    054         * @param monitor                 the component monitor used by this addAdapter
    055         * @param lifecycleStrategy       the component lifecycle strategy used by this addAdapter
    056         * @param useNames                use argument names when looking up dependencies
    057         * @param rememberChosenCtor      remember the chosen constructor (to speed up second/subsequent calls)
    058         * @throws org.picocontainer.injectors.AbstractInjector.NotConcreteRegistrationException
    059         *                              if the implementation is not a concrete class.
    060         * @throws NullPointerException if one of the parameters is <code>null</code>
    061         */
    062        public static ComponentAdapter constructor(final Object componentKey, final Class componentImplementation, Parameter[] parameters, ComponentMonitor monitor,
    063                                                   LifecycleStrategy lifecycleStrategy, boolean useNames, boolean rememberChosenCtor) throws AbstractInjector.NotConcreteRegistrationException {
    064            return new ConstructorInjector(componentKey, componentImplementation, parameters, monitor,
    065                    lifecycleStrategy, useNames, rememberChosenCtor);
    066        }
    067
    068        /**
    069         * convenience method to create annotated field injector
    070         *
    071         * @param key
    072         * @param impl
    073         * @param parameters
    074         * @param componentMonitor
    075         * @param lifecycleStrategy
    076         * @param injectionAnnotation
    077         * @param useNames
    078         * @return
    079         */
    080        public static ComponentAdapter annotatedField(Object key,
    081                                                      Class<?> impl,
    082                                                      Parameter[] parameters,
    083                                                      ComponentMonitor componentMonitor,
    084                                                      LifecycleStrategy lifecycleStrategy,
    085                                                      Class<? extends Annotation> injectionAnnotation, boolean useNames) {
    086            return componentMonitor.newInjector(new AnnotatedFieldInjector(key, impl, parameters, componentMonitor, lifecycleStrategy, injectionAnnotation, useNames));
    087        }
    088
    089        /**
    090         * convenience method to create annotated method injector
    091         *
    092         * @param key
    093         * @param impl
    094         * @param parameters
    095         * @param monitor
    096         * @param lifecycleStrategy
    097         * @param injectionAnnotation
    098         * @param useNames
    099         * @return
    100         */
    101        public static ComponentAdapter annotatedMethod(Object key,
    102                                                       Class<?> impl,
    103                                                       Parameter[] parameters,
    104                                                       ComponentMonitor monitor,
    105                                                       LifecycleStrategy lifecycleStrategy, Class<? extends Annotation> injectionAnnotation, boolean useNames) {
    106            return monitor.newInjector(new AnnotatedMethodInjector(key, impl, parameters, monitor, lifecycleStrategy, injectionAnnotation, useNames));
    107
    108        }
    109
    110
    111        /**
    112         * creates composite injector
    113         *
    114         * @param componentKey
    115         * @param componentImplementation
    116         * @param parameters
    117         * @param monitor
    118         * @param lifecycleStrategy
    119         * @param useNames
    120         * @param injectors
    121         * @return
    122         */
    123        public static ComponentAdapter composite(Object componentKey, Class<?> componentImplementation, Parameter[] parameters, ComponentMonitor monitor, LifecycleStrategy lifecycleStrategy,
    124                                                 boolean useNames, org.picocontainer.Injector... injectors) {
    125            return monitor.newInjector(new CompositeInjector(componentKey, componentImplementation, parameters, monitor, lifecycleStrategy, useNames, injectors));
    126        }
    127
    128
    129        /**
    130         * convenience method to create method injector
    131         *
    132         * @param componentKey
    133         * @param componentImplementation
    134         * @param parameters
    135         * @param monitor
    136         * @param lifecycleStrategy
    137         * @param methodName
    138         * @param useNames
    139         * @return
    140         * @throws AbstractInjector.NotConcreteRegistrationException
    141         *
    142         */
    143        public static ComponentAdapter method(final Object componentKey, final Class componentImplementation, Parameter[] parameters, ComponentMonitor monitor,
    144                                              LifecycleStrategy lifecycleStrategy, String methodName, boolean useNames) throws AbstractInjector.NotConcreteRegistrationException {
    145            return monitor.newInjector(new MethodInjector(componentKey, componentImplementation, parameters, monitor,
    146                    lifecycleStrategy, methodName, useNames));
    147        }
    148
    149        /**
    150         * convenience method to create multi component adapter
    151         *
    152         * @param componentKey
    153         * @param componentImplementation
    154         * @param parameters
    155         * @param componentMonitor
    156         * @param lifecycleStrategy
    157         * @param setterPrefix
    158         * @param useNames
    159         * @return
    160         */
    161
    162        public static ComponentAdapter multi(Object componentKey,
    163                                             Class componentImplementation,
    164                                             Parameter[] parameters,
    165                                             ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, String setterPrefix, boolean useNames) {
    166            return componentMonitor.newInjector(new MultiInjector(componentKey, componentImplementation, parameters, componentMonitor, lifecycleStrategy, setterPrefix, useNames));
    167        }
    168
    169        /**
    170         * convenience method to create named field injector
    171         *
    172         * @param key
    173         * @param impl
    174         * @param parameters
    175         * @param componentMonitor
    176         * @param lifecycleStrategy
    177         * @param fieldNames
    178         * @return
    179         */
    180        public static ComponentAdapter namedField(Object key,
    181                                                  Class<?> impl,
    182                                                  Parameter[] parameters,
    183                                                  ComponentMonitor componentMonitor,
    184                                                  LifecycleStrategy lifecycleStrategy,
    185                                                  String fieldNames) {
    186            return componentMonitor.newInjector(new NamedFieldInjector(key, impl, parameters, componentMonitor, lifecycleStrategy, fieldNames));
    187        }
    188
    189        /**
    190         * convenience method to create setter injector
    191         *
    192         * @param componentKey
    193         * @param componentImplementation
    194         * @param parameters
    195         * @param monitor
    196         * @param lifecycleStrategy
    197         * @param setterMethodPrefix
    198         * @param useNames
    199         * @return
    200         * @throws AbstractInjector.NotConcreteRegistrationException
    201         *
    202         */
    203        public static ComponentAdapter setter(final Object componentKey,
    204                                              final Class componentImplementation,
    205                                              Parameter[] parameters,
    206                                              ComponentMonitor monitor,
    207                                              LifecycleStrategy lifecycleStrategy,
    208                                              String setterMethodPrefix, boolean useNames) throws AbstractInjector.NotConcreteRegistrationException {
    209            return monitor.newInjector(new SetterInjector(componentKey, componentImplementation, parameters, monitor, lifecycleStrategy, setterMethodPrefix, useNames));
    210        }
    211
    212        /**
    213         * conveniently create typed field injector
    214         *
    215         * @param key
    216         * @param impl
    217         * @param parameters
    218         * @param componentMonitor
    219         * @param lifecycleStrategy
    220         * @param classNames
    221         * @return
    222         */
    223        public static ComponentAdapter typedField(Object key,
    224                                                  Class<?> impl,
    225                                                  Parameter[] parameters,
    226                                                  ComponentMonitor componentMonitor,
    227                                                  LifecycleStrategy lifecycleStrategy,
    228                                                  String classNames) {
    229            return componentMonitor.newInjector(new TypedFieldInjector(key, impl, parameters, componentMonitor, lifecycleStrategy, classNames));
    230        }
    231    }