📄 statemachinefactory.java
字号:
Object[] eventIds = annotation.on(); if (eventIds.length == 0) { throw new StateMachineCreationException("Error encountered " + "when processing method " + m + ". No event ids specified."); } if (annotation.in().length == 0) { throw new StateMachineCreationException("Error encountered " + "when processing method " + m + ". No states specified."); } State next = null; if (!annotation.next().equals(Transition.SELF)) { next = states.get(annotation.next()); if (next == null) { throw new StateMachineCreationException("Error encountered " + "when processing method " + m + ". Unknown next state: " + annotation.next() + "."); } } for (Object event : eventIds) { if (event == null) { event = Event.WILDCARD_EVENT_ID; } if (!(event instanceof String)) { event = event.toString(); } for (String in : annotation.in()) { State state = states.get(in); if (state == null) { throw new StateMachineCreationException("Error encountered " + "when processing method " + m + ". Unknown state: " + in + "."); } state.addTransition(new MethodTransition(event, next, m, handler), annotation.weight()); } } } } } static List<Field> getFields(Class<?> clazz) { LinkedList<Field> fields = new LinkedList<Field>(); for (Field f : clazz.getDeclaredFields()) { if (!f.isAnnotationPresent(org.apache.mina.statemachine.annotation.State.class)) { continue; } if ((f.getModifiers() & Modifier.STATIC) == 0 || (f.getModifiers() & Modifier.FINAL) == 0 || !f.getType().equals(String.class)) { throw new StateMachineCreationException("Error encountered when " + "processing field " + f + ". Only static final " + "String fields can be used with the @State " + "annotation."); } if (!f.isAccessible()) { f.setAccessible(true); } fields.add(f); } return fields; } static State[] createStates(List<Field> fields) { LinkedHashMap<String, State> states = new LinkedHashMap<String, State>(); while (!fields.isEmpty()) { int size = fields.size(); int numStates = states.size(); for (int i = 0; i < size; i++) { Field f = fields.remove(0); String value = null; try { value = (String) f.get(null); } catch (IllegalAccessException iae) { throw new StateMachineCreationException("Error encountered when " + "processing field " + f + ".", iae); } org.apache.mina.statemachine.annotation.State stateAnnotation = f .getAnnotation(org.apache.mina.statemachine.annotation.State.class); if (stateAnnotation.value().equals(org.apache.mina.statemachine.annotation.State.ROOT)) { states.put(value, new State(value)); } else if (states.containsKey(stateAnnotation.value())) { states.put(value, new State(value, states.get(stateAnnotation.value()))); } else { // Move to the back of the list of fields for later // processing fields.add(f); } } /* * If no new states were added to states during this iteration it * means that all fields in fields specify non-existent parents. */ if (states.size() == numStates) { throw new StateMachineCreationException("Error encountered while creating " + "FSM. The following fields specify non-existing " + "parent states: " + fields); } } return states.values().toArray(new State[0]); } private static class TransitionWrapper { private final Class<? extends Annotation> transitionClazz; private final Annotation annotation; public TransitionWrapper(Class<? extends Annotation> transitionClazz, Annotation annotation) { this.transitionClazz = transitionClazz; this.annotation = annotation; } Object[] on() { return getParameter("on", Object[].class); } String[] in() { return getParameter("in", String[].class); } String next() { return getParameter("next", String.class); } int weight() { return getParameter("weight", Integer.TYPE); } @SuppressWarnings("unchecked") private <T> T getParameter(String name, Class<T> returnType) { try { Method m = transitionClazz.getMethod(name); if (!returnType.isAssignableFrom(m.getReturnType())) { throw new NoSuchMethodException(); } return (T) m.invoke(annotation); } catch (Throwable t) { throw new StateMachineCreationException("Could not get parameter '" + name + "' from Transition annotation " + transitionClazz); } } } private static class TransitionsWrapper { private final Class<? extends Annotation> transitionsclazz; private final Class<? extends Annotation> transitionClazz; private final Annotation annotation; public TransitionsWrapper(Class<? extends Annotation> transitionClazz, Class<? extends Annotation> transitionsclazz, Annotation annotation) { this.transitionClazz = transitionClazz; this.transitionsclazz = transitionsclazz; this.annotation = annotation; } TransitionWrapper[] value() { Annotation[] annos = getParameter("value", Annotation[].class); TransitionWrapper[] wrappers = new TransitionWrapper[annos.length]; for (int i = 0; i < annos.length; i++) { wrappers[i] = new TransitionWrapper(transitionClazz, annos[i]); } return wrappers; } @SuppressWarnings("unchecked") private <T> T getParameter(String name, Class<T> returnType) { try { Method m = transitionsclazz.getMethod(name); if (!returnType.isAssignableFrom(m.getReturnType())) { throw new NoSuchMethodException(); } return (T) m.invoke(annotation); } catch (Throwable t) { throw new StateMachineCreationException("Could not get parameter '" + name + "' from Transitions annotation " + transitionsclazz); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -