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.List;
026    
027    /**
028     * Injection happens after instantiation, and through fields marked as injection points via an Annotation.
029     * The default annotation of org.picocontainer.annotations.@Inject can be overridden.
030     */
031    @SuppressWarnings("serial")
032    public class AnnotatedFieldInjector extends IterativeInjector {
033    
034        private final Class<? extends Annotation> injectionAnnotation;
035    
036        public AnnotatedFieldInjector(Object key,
037                                      Class<?> impl,
038                                      Parameter[] parameters,
039                                      ComponentMonitor componentMonitor,
040                                      Class<? extends Annotation> injectionAnnotation, boolean useNames) {
041    
042            super(key, impl, parameters, componentMonitor, useNames);
043            this.injectionAnnotation = injectionAnnotation;
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            Class drillInto = getComponentImplementation();
052            while (drillInto != Object.class) {
053                final Field[] fields = getFields(drillInto);
054                for (final Field field : fields) {
055                    if (isAnnotatedForInjection(field)) {
056                        injectionMembers.add(field);
057                        typeList.add(box(field.getType()));
058                        bindingIds.add(getBinding(field));
059                    }
060                }
061                drillInto = drillInto.getSuperclass();
062            }
063            injectionTypes = typeList.toArray(new Type[0]);
064            bindings = bindingIds.toArray(new Annotation[0]);
065        }
066    
067        private Annotation getBinding(Field field) {
068            Annotation[] annotations = field.getAnnotations();
069            for (Annotation annotation : annotations) {
070                if (annotation.annotationType().isAnnotationPresent(Bind.class)) {
071                    return annotation;
072                }
073            }
074            return null;
075        }
076    
077        protected boolean isAnnotatedForInjection(Field field) {
078            return field.getAnnotation(injectionAnnotation) != null;
079        }
080    
081        private Field[] getFields(final Class clazz) {
082            return AccessController.doPrivileged(new PrivilegedAction<Field[]>() {
083                public Field[] run() {
084                    return clazz.getDeclaredFields();
085                }
086            });
087        }
088    
089        protected Object injectIntoMember(AccessibleObject member, Object componentInstance, Object toInject)
090                throws IllegalAccessException, InvocationTargetException {
091            Field field = (Field) member;
092            field.setAccessible(true);
093            field.set(componentInstance, toInject);
094            return null;
095        }
096    
097        @Override
098        public String getDescriptor() {
099            return "AnnotatedFieldInjector-";
100        }
101    
102        @Override
103        protected NameBinding makeParameterNameImpl(final AccessibleObject member) {
104            return new NameBinding() {
105                public String getName() {
106                    return ((Field) member).getName();
107                }
108            };
109        }
110    
111        protected Object memberInvocationReturn(Object lastReturn, AccessibleObject member, Object instance) {
112            return instance;
113        }
114    }