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.NameBinding;
014    import org.picocontainer.Parameter;
015    import org.picocontainer.PicoContainer;
016    import org.picocontainer.annotations.Bind;
017    
018    import java.lang.annotation.Annotation;
019    import java.lang.reflect.*;
020    import java.security.AccessController;
021    import java.security.PrivilegedAction;
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.Collections;
025    import java.util.List;
026    import java.util.Set;
027    
028    /**
029     * Injection happens after instantiation, and fields are marked as
030     * injection points via a field type.
031     */
032    @SuppressWarnings("serial")
033    public class TypedFieldInjector<T> extends AbstractFieldInjector<T> {
034    
035        private final List<String> classes;
036    
037        public TypedFieldInjector(Object key,
038                                      Class<?> impl,
039                                      Parameter[] parameters,
040                                      ComponentMonitor componentMonitor,
041                                      String classNames) {
042            super(key, impl, parameters, componentMonitor, true);
043            this.classes = Arrays.asList(classNames.trim().split(" "));
044        }
045    
046        @Override
047        protected void initializeInjectionMembersAndTypeLists() {
048            injectionMembers = new ArrayList<AccessibleObject>();
049            List<Annotation> bindingIds = new ArrayList<Annotation>();
050            final List<Type> typeList = new ArrayList<Type>();
051            final Field[] fields = getFields();
052            for (final Field field : fields) {
053                if (isTypedForInjection(field)) {
054                    injectionMembers.add(field);
055                    typeList.add(box(field.getType()));
056                    bindingIds.add(getBinding(field));
057                }
058            }
059            injectionTypes = typeList.toArray(new Type[0]);
060            bindings = bindingIds.toArray(new Annotation[0]);
061        }
062    
063        private Annotation getBinding(Field field) {
064            Annotation[] annotations = field.getAnnotations();
065            for (Annotation annotation : annotations) {
066                if (annotation.annotationType().isAnnotationPresent(Bind.class)) {
067                    return annotation;
068                }
069            }
070            return null;
071        }
072    
073        protected boolean isTypedForInjection(Field field) {
074            return classes.contains(field.getType().getName());
075        }
076    
077        private Field[] getFields() {
078            return AccessController.doPrivileged(new PrivilegedAction<Field[]>() {
079                public Field[] run() {
080                    return getComponentImplementation().getDeclaredFields();
081                }
082            });
083        }
084    
085    
086        protected Object injectIntoMember(AccessibleObject member, Object componentInstance, Object toInject)
087            throws IllegalAccessException, InvocationTargetException {
088            Field field = (Field) member;
089            field.setAccessible(true);
090            field.set(componentInstance, toInject);
091            return null;
092        }
093    
094        @Override
095        public String getDescriptor() {
096            return "TypedFieldInjector-";
097        }
098    
099        @Override
100        protected NameBinding makeParameterNameImpl(final AccessibleObject member) {
101            return new NameBinding() {
102                public String getName() {
103                    return ((Field) member).getName();
104                }
105            };
106        }
107    
108        protected Object memberInvocationReturn(Object lastReturn, AccessibleObject member, Object instance) {
109            return instance;
110        }
111    
112        List<String> getInjectionFieldTypes() {
113            return Collections.unmodifiableList(classes);
114        }
115    
116    
117    }