    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     * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
    009     *****************************************************************************/
    010
    011    package org.picocontainer.injectors;
    012
    013    import org.picocontainer.ComponentAdapter;
    014    import org.picocontainer.Parameter;
    015    import org.picocontainer.PicoCompositionException;
    016    import org.picocontainer.ComponentMonitor;
    017    import org.picocontainer.LifecycleStrategy;
    018    import org.picocontainer.annotations.Inject;
    019
    020    import java.lang.annotation.Annotation;
    021    import java.util.Properties;
    022
    023
    024    /**
    025     * A {@link org.picocontainer.InjectionFactory} for Guice-style annotated methods.
    026     * The factory creates {@link AnnotatedMethodInjector}.
    027     *
    028     * @author Paul Hammant
    029     */
    030    @SuppressWarnings("serial")
    031    public class AnnotatedMethodInjection extends AbstractInjectionFactory {
    032
    033
    034            private final Class<? extends Annotation> injectionAnnotation;
    035        private final boolean useNames;
    036
    037        public AnnotatedMethodInjection(Class<? extends Annotation> injectionAnnotation, boolean useNames) {
    038            this.injectionAnnotation = injectionAnnotation;
    039            this.useNames = useNames;
    040        }
    041
    042        public AnnotatedMethodInjection() {
    043            this(Inject.class, false);
    044        }
    045
    046        /**
    047         * Create a {@link SetterInjector}.
    048         *
    049         * @param componentMonitor
    050         * @param lifecycleStrategy
    051         * @param componentProperties
    052         * @param componentKey The component's key
    053         * @param componentImplementation The class of the bean.
    054         * @param parameters Any parameters for the setters. If null the adapter
    055         *            solves the dependencies for all setters internally. Otherwise
    056         *            the number parameters must match the number of the setter.
    057         * @return Returns a new {@link SetterInjector}.
    058         * @throws org.picocontainer.PicoCompositionException if dependencies cannot
    059         *             be solved or if the implementation is an interface or an
    060         *             abstract class.
    061         */
    062        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties,
    063                                                       Object componentKey, Class<T> componentImplementation, Parameter... parameters)
    064                throws PicoCompositionException {
    065            return componentMonitor.newInjector(new AnnotatedMethodInjector(componentKey, componentImplementation, parameters, componentMonitor, lifecycleStrategy, injectionAnnotation, useNames));
    066        }
    067    }