01: package dalma.container.model;
02:
03: import java.lang.reflect.Method;
04: import java.lang.reflect.InvocationTargetException;
05:
06: /**
07: * @author Kohsuke Kawaguchi
08: */
09: final class MethodInjector<T, V> extends Injector<T, V> {
10: private final Method m;
11:
12: public MethodInjector(Method m) throws IllegalResourceException {
13: this .m = m;
14: if (m.getParameterTypes().length == 0)
15: throw new IllegalResourceException(m.getName()
16: + " has @Resource but takes no parameter");
17: if (m.getParameterTypes().length > 1)
18: throw new IllegalResourceException(
19: m.getName()
20: + " has @Resource but takes more than one parameters");
21: }
22:
23: public String getName() {
24: String name = m.getName();
25: // proper lower casing
26: if (name.startsWith("set") && name.length() > 3) {
27: return Character.toLowerCase(name.charAt(3))
28: + name.substring(4);
29: } else {
30: return name;
31: }
32: }
33:
34: public Class<V> _getType() {
35: return (Class<V>) m.getParameterTypes()[0];
36: }
37:
38: public void set(T target, V value) throws InjectionException {
39: try {
40: m.invoke(target, value);
41: } catch (IllegalAccessException e) {
42: throw new InjectionException(e);
43: } catch (InvocationTargetException e) {
44: throw new InjectionException(e);
45: }
46: }
47: }
|