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 java.util.Properties;
014    
015    import org.picocontainer.Characteristics;
016    import org.picocontainer.ComponentAdapter;
017    import org.picocontainer.ComponentMonitor;
018    import org.picocontainer.LifecycleStrategy;
019    import org.picocontainer.Parameter;
020    import org.picocontainer.PicoCompositionException;
021    import org.picocontainer.behaviors.AbstractBehaviorFactory;
022    
023    
024    /**
025     * A {@link org.picocontainer.InjectionFactory} for JavaBeans.
026     * The factory creates {@link SetterInjector}.
027     *
028     * @author Jörg Schaible
029     */
030    @SuppressWarnings("serial")
031    public class SetterInjection extends AbstractInjectionFactory {
032    
033        private final String setterMethodPrefix;
034    
035        public SetterInjection(String setterMethodPrefix) {
036            this.setterMethodPrefix = setterMethodPrefix;
037        }
038    
039        public SetterInjection() {
040            this("set");
041        }
042    
043        /**
044         * Create a {@link SetterInjector}.
045         * 
046         * @param componentMonitor
047         * @param lifecycleStrategy
048         * @param componentProperties
049         * @param componentKey The component's key
050         * @param componentImplementation The class of the bean.
051         * @param parameters Any parameters for the setters. If null the adapter
052         *            solves the dependencies for all setters internally. Otherwise
053         *            the number parameters must match the number of the setter.
054         * @return Returns a new {@link SetterInjector}.
055         * @throws PicoCompositionException if dependencies cannot be solved
056         * @throws org.picocontainer.PicoCompositionException if the implementation
057         *             is an interface or an abstract class.
058         */
059        public <T> ComponentAdapter<T> createComponentAdapter(ComponentMonitor componentMonitor, LifecycleStrategy lifecycleStrategy, Properties componentProperties, Object componentKey, Class<T> componentImplementation, Parameter... parameters)
060                throws PicoCompositionException {
061            boolean useNames = AbstractBehaviorFactory.arePropertiesPresent(componentProperties, Characteristics.USE_NAMES);
062            return new SetterInjector(componentKey, componentImplementation, parameters, componentMonitor, lifecycleStrategy, setterMethodPrefix, useNames);
063        }
064    }