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