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