01: /*****************************************************************************
02: * Copyright (C) PicoContainer Organization. All rights reserved. *
03: * ------------------------------------------------------------------------- *
04: * The software in this package is published under the terms of the BSD *
05: * style license a copy of which has been included with this distribution in *
06: * the LICENSE.txt file. *
07: * *
08: * Original code by *
09: *****************************************************************************/package org.picocontainer.injectors;
10:
11: import org.picocontainer.ComponentMonitor;
12: import org.picocontainer.Parameter;
13: import org.picocontainer.LifecycleStrategy;
14: import org.picocontainer.behaviors.PropertyApplicator;
15: import org.picocontainer.behaviors.Cached;
16:
17: import java.lang.reflect.Method;
18:
19: /**
20: * Instantiates components using empty constructors and
21: * <a href="http://picocontainer.org/setter-injection.html">Setter Injection</a>.
22: * For easy setting of primitive properties, also see {@link PropertyApplicator}.
23: * <p/>
24: * <em>
25: * Note that this class doesn't cache instances. If you want caching,
26: * use a {@link Cached} around this one.
27: * </em>
28: * </p>
29: *
30: * @author Aslak Hellesøy
31: * @author Jörg Schaible
32: * @author Mauro Talevi
33: * @author Paul Hammant
34: */
35: public class SetterInjector extends IterativeInjector {
36:
37: private final String setterMethodPrefix;
38:
39: /**
40: * Constructs a SetterInjector
41: *
42: * @param componentKey the search key for this implementation
43: * @param componentImplementation the concrete implementation
44: * @param parameters the parameters to use for the initialization
45: * @param monitor the component monitor used by this addAdapter
46: * @param lifecycleStrategy the component lifecycle strategy used by this addAdapter
47: * @param setterMethodPrefix
48: * @throws org.picocontainer.injectors.AbstractInjector.NotConcreteRegistrationException
49: * if the implementation is not a concrete class.
50: * @throws NullPointerException if one of the parameters is <code>null</code>
51: */
52: public SetterInjector(final Object componentKey,
53: final Class componentImplementation,
54: Parameter[] parameters, ComponentMonitor monitor,
55: LifecycleStrategy lifecycleStrategy,
56: String setterMethodPrefix, boolean useNames)
57: throws NotConcreteRegistrationException {
58: super (componentKey, componentImplementation, parameters,
59: monitor, lifecycleStrategy, useNames);
60: this .setterMethodPrefix = setterMethodPrefix;
61: }
62:
63: protected boolean isInjectorMethod(Method method) {
64: String methodName = method.getName();
65: return methodName.length() >= getInjectorPrefix().length() + 1
66: && methodName.startsWith(getInjectorPrefix())
67: && Character.isUpperCase(methodName
68: .charAt(getInjectorPrefix().length()));
69: }
70:
71: protected String getInjectorPrefix() {
72: return setterMethodPrefix;
73: }
74:
75: public String getDescriptor() {
76: return "SetterInjector-";
77: }
78:
79: }
|