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