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: *****************************************************************************/package org.picocontainer.injectors;
009:
010: import org.picocontainer.ComponentMonitor;
011: import org.picocontainer.LifecycleStrategy;
012: import org.picocontainer.Parameter;
013: import org.picocontainer.NameBinding;
014: import org.picocontainer.PicoContainer;
015: import org.picocontainer.annotations.Bind;
016:
017: import java.lang.annotation.Annotation;
018: import java.lang.reflect.AccessibleObject;
019: import java.lang.reflect.Constructor;
020: import java.lang.reflect.Method;
021:
022: import com.thoughtworks.paranamer.CachingParanamer;
023: import com.thoughtworks.paranamer.Paranamer;
024:
025: /**
026: * Injection will happen in a single member function on the component.
027: *
028: * @author Paul Hammant
029: *
030: */
031: public abstract class SingleMemberInjector<T> extends
032: AbstractInjector<T> {
033:
034: private transient CachingParanamer paranamer = new CachingParanamer();
035:
036: public SingleMemberInjector(Object componentKey,
037: Class componentImplementation, Parameter[] parameters,
038: ComponentMonitor monitor,
039: LifecycleStrategy lifecycleStrategy, boolean useNames) {
040: super (componentKey, componentImplementation, parameters,
041: monitor, lifecycleStrategy, useNames);
042: }
043:
044: protected CachingParanamer getParanamer() {
045: return paranamer;
046: }
047:
048: /**
049: * TODO: shall it box everything? a bit too few for me (konstantin)
050: */
051: protected Class box(Class parameterType) {
052: if (parameterType.isPrimitive()) {
053: String parameterTypeName = parameterType.getName();
054: if (parameterTypeName == "int") {
055: return Integer.class;
056: } else if (parameterTypeName == "boolean") {
057: return Boolean.class;
058: } else if (parameterTypeName == "long") {
059: return Long.class;
060: } else if (parameterTypeName == "float") {
061: return Float.class;
062: } else if (parameterTypeName == "double") {
063: return Double.class;
064: } else if (parameterTypeName == "char") {
065: return Character.class;
066: } else if (parameterTypeName == "byte") {
067: return Byte.class;
068: } else if (parameterTypeName == "short") {
069: return Short.class;
070: }
071: }
072: return parameterType;
073: }
074:
075: @SuppressWarnings("unchecked")
076: protected Object[] getMemberArguments(PicoContainer container,
077: final AccessibleObject member,
078: final Class[] parameterTypes, final Annotation[] bindings) {
079: for (int i = 0; i < parameterTypes.length; i++) {
080: parameterTypes[i] = box(parameterTypes[i]);
081:
082: }
083: Object[] result = new Object[parameterTypes.length];
084: Parameter[] currentParameters = parameters != null ? parameters
085: : createDefaultParameters(parameterTypes);
086:
087: for (int i = 0; i < currentParameters.length; i++) {
088: result[i] = currentParameters[i].resolveInstance(container,
089: this , parameterTypes[i], new ParameterNameBinding(
090: paranamer, getComponentImplementation(),
091: member, i), useNames(), bindings[i]);
092: }
093: return result;
094: }
095:
096: protected Annotation[] getBindings(Annotation[][] annotationss) {
097: Annotation[] retVal = new Annotation[annotationss.length];
098: for (int i = 0; i < annotationss.length; i++) {
099: Annotation[] annotations = annotationss[i];
100: for (int j = 0; j < annotations.length; j++) {
101: Annotation annotation = annotations[j];
102: if (annotation.annotationType().getAnnotation(
103: Bind.class) != null) {
104: retVal[i] = annotation;
105: break;
106: }
107: }
108: }
109: return retVal;
110: }
111:
112: }
|