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 017 import java.lang.reflect.AccessibleObject; 018 import java.lang.reflect.Method; 019 import java.lang.reflect.Type; 020 import java.util.Set; 021 022 @SuppressWarnings("serial") 023 public class NamedMethodInjector<T> extends SetterInjector<T> { 024 025 private final boolean optional; 026 027 public NamedMethodInjector(Object key, 028 Class<?> impl, 029 Parameter[] parameters, 030 ComponentMonitor monitor, 031 boolean optional) { 032 this(key, impl, parameters, monitor, "set", optional); 033 } 034 035 public NamedMethodInjector(Object key, 036 Class<?> impl, 037 Parameter[] parameters, 038 ComponentMonitor monitor) { 039 this(key, impl, parameters, monitor, "set", true); 040 } 041 042 public NamedMethodInjector(Object key, 043 Class<?> impl, 044 Parameter[] parameters, 045 ComponentMonitor monitor, 046 String prefix) { 047 this(key, impl, parameters, monitor, prefix, true); 048 } 049 050 public NamedMethodInjector(Object key, 051 Class<?> impl, 052 Parameter[] parameters, 053 ComponentMonitor monitor, 054 String prefix, 055 boolean optional) { 056 super(key, impl, parameters, monitor, prefix, true); 057 this.optional = optional; 058 } 059 060 @Override 061 protected NameBinding makeParameterNameImpl(final AccessibleObject member) { 062 return new NameBinding() { 063 public String getName() { 064 String name = ((Method)member).getName().substring(prefix.length()); // string off 'set' or chosen prefix 065 return name.substring(0,1).toLowerCase() + name.substring(1); // change "SomeThing" to "someThing" 066 } 067 }; 068 } 069 070 @Override 071 protected void unsatisfiedDependencies(PicoContainer container, Set<Type> unsatisfiableDependencyTypes) { 072 if (!optional) { 073 super.unsatisfiedDependencies(container, unsatisfiableDependencyTypes); 074 } 075 } 076 077 @Override 078 public String getDescriptor() { 079 return "NamedMethodInjection"; 080 } 081 082 }