1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.uruma.binding.method;
17
18 import java.lang.reflect.Array;
19 import java.lang.reflect.Method;
20
21 import org.seasar.framework.util.MethodUtil;
22 import org.seasar.uruma.core.UrumaMessageCodes;
23 import org.seasar.uruma.exception.UIllegalArgumentException;
24
25
26
27
28
29
30 public class SingleParamTypeMethodBinding extends MethodBinding implements
31 UrumaMessageCodes {
32 private Class<?> paramType;
33
34
35
36
37
38
39
40
41
42
43
44 public SingleParamTypeMethodBinding(final Object target, final Method method) {
45 super(target, method, null);
46 setup();
47 }
48
49
50
51
52
53
54
55
56
57
58
59
60
61 public SingleParamTypeMethodBinding(final Object target,
62 final Method method, final MethodCallback callback) {
63 super(target, method, callback);
64 setup();
65 }
66
67 private void setup() {
68 Class<?>[] paramTypes = method.getParameterTypes();
69 if (paramTypes.length == 0) {
70 paramType = null;
71 } else if (paramTypes.length == 1) {
72 paramType = paramTypes[0];
73 } else {
74 throw new UIllegalArgumentException(ILLEGAL_ARG_NUMBERS, method
75 .getDeclaringClass().getName(), method.getName());
76 }
77 }
78
79
80
81
82 @Override
83 public Object invoke(final Object[] args) {
84 Object[] trueArgs = null;
85 if (paramType != null && args != null) {
86 if (paramType.isArray()) {
87 Class<?> componentType = paramType.getComponentType();
88 Object[] params = (Object[]) Array.newInstance(componentType,
89 args.length);
90 System.arraycopy(args, 0, params, 0, args.length);
91 trueArgs = new Object[] { params };
92 } else {
93 trueArgs = new Object[] { paramType.cast(args[0]) };
94 }
95 }
96
97 Object result = MethodUtil.invoke(method, target, trueArgs);
98 return callback(trueArgs, result);
99 }
100 }