1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.mina.statemachine.transition;
21
22 import java.lang.reflect.InvocationTargetException;
23 import java.lang.reflect.Method;
24 import java.util.Arrays;
25
26 import org.apache.mina.statemachine.State;
27 import org.apache.mina.statemachine.StateMachine;
28 import org.apache.mina.statemachine.context.StateContext;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class MethodSelfTransition extends AbstractSelfTransition {
46 private static final Logger LOGGER = LoggerFactory.getLogger(MethodTransition.class);
47
48 private Method method;
49
50 private final Object target;
51
52 private static final Object[] EMPTY_ARGUMENTS = new Object[0];
53
54 public MethodSelfTransition(Method method, Object target) {
55 super();
56 this.method = method;
57 this.target = target;
58 }
59
60
61
62
63
64
65
66
67
68 public MethodSelfTransition(String methodName, Object target) {
69
70 this.target = target;
71
72 Method[] candidates = target.getClass().getMethods();
73 Method result = null;
74
75 for (int i = 0; i < candidates.length; i++) {
76 if (candidates[i].getName().equals(methodName)) {
77 if (result != null) {
78 throw new AmbiguousMethodException(methodName);
79 }
80 result = candidates[i];
81 }
82 }
83
84 if (result == null) {
85 throw new NoSuchMethodException(methodName);
86 }
87
88 this.method = result;
89
90 }
91
92
93
94
95
96
97 public Method getMethod() {
98 return method;
99 }
100
101 public boolean doExecute(StateContext stateContext, State state) {
102 Class<?>[] types = method.getParameterTypes();
103
104 if (types.length == 0) {
105 invokeMethod(EMPTY_ARGUMENTS);
106 return true;
107 }
108
109 if (types.length > 2) {
110 return false;
111 }
112
113 Object[] args = new Object[types.length];
114
115 int i = 0;
116
117 if (types[i].isAssignableFrom(StateContext.class)) {
118 args[i++] = stateContext;
119 }
120 if ((i < types.length) && types[i].isAssignableFrom(State.class)) {
121 args[i++] = state;
122 }
123
124 invokeMethod(args);
125
126 return true;
127 }
128
129 private void invokeMethod(Object[] arguments) {
130 try {
131 if (LOGGER.isDebugEnabled()) {
132 LOGGER.debug("Executing method " + method + " with arguments " + Arrays.asList(arguments));
133 }
134 method.invoke(target, arguments);
135 } catch (InvocationTargetException ite) {
136 if (ite.getCause() instanceof RuntimeException) {
137 throw (RuntimeException) ite.getCause();
138 }
139 throw new MethodInvocationException(method, ite);
140 } catch (IllegalAccessException iae) {
141 throw new MethodInvocationException(method, iae);
142 }
143 }
144
145 }