01: package org.kohsuke.args4j;
02:
03: import org.kohsuke.args4j.spi.Setter;
04:
05: import java.lang.reflect.InvocationTargetException;
06: import java.lang.reflect.Method;
07:
08: /**
09: * {@link Setter} that sets to a {@link Method}.
10: *
11: * @author Kohsuke Kawaguchi
12: */
13: final class MethodSetter implements Setter {
14: private final Object bean;
15: private final Method m;
16:
17: public MethodSetter(Object bean, Method m) {
18: this .bean = bean;
19: this .m = m;
20: if (m.getParameterTypes().length != 1)
21: throw new IllegalAnnotationError(
22: Messages.ILLEGAL_METHOD_SIGNATURE.format(m));
23: }
24:
25: public Class getType() {
26: return m.getParameterTypes()[0];
27: }
28:
29: public boolean isMultiValued() {
30: return false;
31: }
32:
33: public void addValue(Object value) throws CmdLineException {
34: try {
35: try {
36: m.invoke(bean, value);
37: } catch (IllegalAccessException _) {
38: // try again
39: m.setAccessible(true);
40: try {
41: m.invoke(bean, value);
42: } catch (IllegalAccessException e) {
43: throw new IllegalAccessError(e.getMessage());
44: }
45: }
46: } catch (InvocationTargetException e) {
47: Throwable t = e.getTargetException();
48: if (t instanceof RuntimeException)
49: throw (RuntimeException) t;
50: if (t instanceof Error)
51: throw (Error) t;
52: if (t instanceof CmdLineException)
53: throw (CmdLineException) t;
54:
55: // otherwise wrap
56: if (t != null)
57: throw new CmdLineException(t);
58: else
59: throw new CmdLineException(e);
60: }
61: }
62: }
|