| 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 | 0 | super(target, method, null); |
| 46 | 0 | setup(); |
| 47 | 0 | } |
| 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 | 0 | super(target, method, callback); |
| 64 | 0 | setup(); |
| 65 | 0 | } |
| 66 | |
|
| 67 | |
private void setup() { |
| 68 | 0 | Class<?>[] paramTypes = method.getParameterTypes(); |
| 69 | 0 | if (paramTypes.length == 0) { |
| 70 | 0 | paramType = null; |
| 71 | 0 | } else if (paramTypes.length == 1) { |
| 72 | 0 | paramType = paramTypes[0]; |
| 73 | |
} else { |
| 74 | 0 | throw new UIllegalArgumentException(ILLEGAL_ARG_NUMBERS, method |
| 75 | |
.getDeclaringClass().getName(), method.getName()); |
| 76 | |
} |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
@Override |
| 83 | |
public Object invoke(final Object[] args) { |
| 84 | 0 | Object[] trueArgs = null; |
| 85 | 0 | if (paramType != null && args != null) { |
| 86 | 0 | if (paramType.isArray()) { |
| 87 | 0 | Class<?> componentType = paramType.getComponentType(); |
| 88 | 0 | Object[] params = (Object[]) Array.newInstance(componentType, |
| 89 | |
args.length); |
| 90 | 0 | System.arraycopy(args, 0, params, 0, args.length); |
| 91 | 0 | trueArgs = new Object[] { params }; |
| 92 | 0 | } else { |
| 93 | 0 | trueArgs = new Object[] { paramType.cast(args[0]) }; |
| 94 | |
} |
| 95 | |
} |
| 96 | |
|
| 97 | 0 | Object result = MethodUtil.invoke(method, target, trueArgs); |
| 98 | 0 | return callback(trueArgs, result); |
| 99 | |
} |
| 100 | |
} |