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     * Original code by                                                          *
009     *****************************************************************************/
010    package org.picocontainer.injectors;
011    
012    import org.picocontainer.ComponentMonitor;
013    import org.picocontainer.Parameter;
014    import org.picocontainer.LifecycleStrategy;
015    import org.picocontainer.behaviors.PropertyApplicator;
016    import org.picocontainer.behaviors.Cached;
017    
018    import java.lang.reflect.Method;
019    
020    /**
021     * Instantiates components using empty constructors and
022     * <a href="http://picocontainer.org/setter-injection.html">Setter Injection</a>.
023     * For easy setting of primitive properties, also see {@link PropertyApplicator}.
024     * <p/>
025     * <em>
026     * Note that this class doesn't cache instances. If you want caching,
027     * use a {@link Cached} around this one.
028     * </em>
029     * </p>
030     *
031     * @author Aslak Helles&oslash;y
032     * @author J&ouml;rg Schaible
033     * @author Mauro Talevi
034     * @author Paul Hammant
035     */
036    public class SetterInjector extends IterativeInjector {
037    
038        private final String setterMethodPrefix;
039    
040        /**
041         * Constructs a SetterInjector
042         *
043         * @param componentKey            the search key for this implementation
044         * @param componentImplementation the concrete implementation
045         * @param parameters              the parameters to use for the initialization
046         * @param monitor                 the component monitor used by this addAdapter
047         * @param lifecycleStrategy       the component lifecycle strategy used by this addAdapter
048         * @param setterMethodPrefix
049         * @throws org.picocontainer.injectors.AbstractInjector.NotConcreteRegistrationException
050         *                              if the implementation is not a concrete class.
051         * @throws NullPointerException if one of the parameters is <code>null</code>
052         */
053        public SetterInjector(final Object componentKey,
054                              final Class componentImplementation,
055                              Parameter[] parameters,
056                              ComponentMonitor monitor,
057                              LifecycleStrategy lifecycleStrategy,
058                              String setterMethodPrefix) throws  NotConcreteRegistrationException {
059            super(componentKey, componentImplementation, parameters, monitor, lifecycleStrategy);
060            this.setterMethodPrefix = setterMethodPrefix;
061        }
062    
063    
064        protected boolean isInjectorMethod(Method method) {
065            String methodName = method.getName();
066            return methodName.length() >= getInjectorPrefix().length() + 1 && methodName.startsWith(getInjectorPrefix()) && Character.isUpperCase(methodName.charAt(getInjectorPrefix().length()));
067        }
068    
069        protected String getInjectorPrefix() {
070            return setterMethodPrefix;
071        }
072    
073        public String toString() {
074            return "SetterInjector-" + super.toString(); 
075        }
076    
077    
078    }