    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.Parameter;
    013    import org.picocontainer.ComponentMonitor;
    014    import org.picocontainer.LifecycleStrategy;
    015
    016    import java.lang.annotation.Annotation;
    017    import java.lang.reflect.Method;
    018    import java.lang.reflect.AccessibleObject;
    019    import java.lang.reflect.InvocationTargetException;
    020
    021    @SuppressWarnings("serial")
    022    public class AnnotatedMethodInjector extends SetterInjector {
    023
    024            private final Class<? extends Annotation> injectionAnnotation;
    025
    026        public AnnotatedMethodInjector(Object key,
    027                                       Class<?> impl,
    028                                       Parameter[] parameters,
    029                                       ComponentMonitor monitor,
    030                                       LifecycleStrategy lifecycleStrategy, Class<? extends Annotation> injectionAnnotation, boolean useNames) {
    031            super(key, impl, parameters, monitor, lifecycleStrategy, "", useNames);
    032            this.injectionAnnotation = injectionAnnotation;
    033        }
    034
    035        protected Object injectIntoMember(AccessibleObject member, Object componentInstance, Object toInject)
    036            throws IllegalAccessException, InvocationTargetException {
    037            return ((Method)member).invoke(componentInstance, toInject);
    038        }
    039
    040        protected final boolean isInjectorMethod(Method method) {
    041            return method.getAnnotation(injectionAnnotation) != null;
    042        }
    043
    044        public String toString() {
    045            return "MethodInjection-" + super.toString();
    046        }
    047
    048    }