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.commons.lang.builder.EqualsBuilder;
27 import org.apache.commons.lang.builder.HashCodeBuilder;
28 import org.apache.commons.lang.builder.ToStringBuilder;
29 import org.apache.mina.statemachine.State;
30 import org.apache.mina.statemachine.StateMachine;
31 import org.apache.mina.statemachine.StateMachineFactory;
32 import org.apache.mina.statemachine.annotation.Transition;
33 import org.apache.mina.statemachine.context.StateContext;
34 import org.apache.mina.statemachine.event.Event;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public class MethodTransition extends AbstractTransition {
61 private static final Logger LOGGER = LoggerFactory.getLogger( MethodTransition.class );
62 private static final Object[] EMPTY_ARGUMENTS = new Object[0];
63
64 private final Method method;
65 private final Object target;
66
67
68
69
70
71
72
73
74
75
76 public MethodTransition(Object eventId, State nextState, Method method, Object target) {
77 super(eventId, nextState);
78 this.method = method;
79 this.target = target;
80 }
81
82
83
84
85
86
87
88
89
90 public MethodTransition(Object eventId, Method method, Object target) {
91 this(eventId, null, method, target);
92 }
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public MethodTransition(Object eventId, State nextState, Object target) {
109 this(eventId, nextState, eventId.toString(), target);
110 }
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125 public MethodTransition(Object eventId, Object target) {
126 this(eventId, eventId.toString(), target);
127 }
128
129
130
131
132
133
134
135
136
137
138
139
140 public MethodTransition(Object eventId, String methodName, Object target) {
141 this(eventId, null, methodName, target);
142 }
143
144
145
146
147
148
149
150
151
152
153
154
155
156 public MethodTransition(Object eventId, State nextState, String methodName, Object target) {
157 super(eventId, nextState);
158
159 this.target = target;
160
161 Method[] candidates = target.getClass().getMethods();
162 Method result = null;
163 for (int i = 0; i < candidates.length; i++) {
164 if (candidates[i].getName().equals(methodName)) {
165 if (result != null) {
166 throw new AmbiguousMethodException(methodName);
167 }
168 result = candidates[i];
169 }
170 }
171
172 if (result == null) {
173 throw new NoSuchMethodException(methodName);
174 }
175
176 this.method = result;
177 }
178
179
180
181
182
183
184 public Method getMethod() {
185 return method;
186 }
187
188
189
190
191
192
193 public Object getTarget() {
194 return target;
195 }
196
197 public boolean doExecute(Event event) {
198 Class<?>[] types = method.getParameterTypes();
199
200 if (types.length == 0) {
201 invokeMethod(EMPTY_ARGUMENTS);
202 return true;
203 }
204
205 if (types.length > 2 + event.getArguments().length) {
206 return false;
207 }
208
209 Object[] args = new Object[types.length];
210
211 int i = 0;
212 if (match(types[i], event, Event.class)) {
213 args[i++] = event;
214 }
215 if (i < args.length && match(types[i], event.getContext(), StateContext.class)) {
216 args[i++] = event.getContext();
217 }
218 Object[] eventArgs = event.getArguments();
219 for (int j = 0; i < args.length && j < eventArgs.length; j++) {
220 if (match(types[i], eventArgs[j], Object.class)) {
221 args[i++] = eventArgs[j];
222 }
223 }
224
225 if (args.length > i) {
226 return false;
227 }
228
229 invokeMethod(args);
230
231 return true;
232 }
233
234 @SuppressWarnings("unchecked")
235 private boolean match(Class<?> paramType, Object arg, Class argType) {
236 if (paramType.isPrimitive()) {
237 if (paramType.equals(Boolean.TYPE)) {
238 return arg instanceof Boolean;
239 }
240 if (paramType.equals(Integer.TYPE)) {
241 return arg instanceof Integer;
242 }
243 if (paramType.equals(Long.TYPE)) {
244 return arg instanceof Long;
245 }
246 if (paramType.equals(Short.TYPE)) {
247 return arg instanceof Short;
248 }
249 if (paramType.equals(Byte.TYPE)) {
250 return arg instanceof Byte;
251 }
252 if (paramType.equals(Double.TYPE)) {
253 return arg instanceof Double;
254 }
255 if (paramType.equals(Float.TYPE)) {
256 return arg instanceof Float;
257 }
258 if (paramType.equals(Character.TYPE)) {
259 return arg instanceof Character;
260 }
261 }
262 return argType.isAssignableFrom(paramType)
263 && paramType.isAssignableFrom(arg.getClass());
264 }
265
266 private void invokeMethod(Object[] arguments) {
267 try {
268 if (LOGGER.isDebugEnabled()) {
269 LOGGER.debug("Executing method " + method
270 + " with arguments " + Arrays.asList(arguments));
271 }
272 method.invoke(target, arguments);
273 } catch (InvocationTargetException ite) {
274 if (ite.getCause() instanceof RuntimeException) {
275 throw (RuntimeException) ite.getCause();
276 }
277 throw new MethodInvocationException(method, ite);
278 } catch (IllegalAccessException iae) {
279 throw new MethodInvocationException(method, iae);
280 }
281 }
282
283 public boolean equals(Object o) {
284 if (!(o instanceof MethodTransition)) {
285 return false;
286 }
287 if (o == this) {
288 return true;
289 }
290 MethodTransition that = (MethodTransition) o;
291 return new EqualsBuilder()
292 .appendSuper(super.equals(that))
293 .append(method, that.method)
294 .append(target, that.target)
295 .isEquals();
296 }
297
298 public int hashCode() {
299 return new HashCodeBuilder(13, 33).appendSuper(super.hashCode()).append(method).append(target).toHashCode();
300 }
301
302 public String toString() {
303 return new ToStringBuilder(this)
304 .appendSuper(super.toString())
305 .append("method", method)
306 .append("target", target)
307 .toString();
308 }
309 }