MyStateMachineUtils类使用反射就能解决包名问题了。
public abstract class MyStateMachineUtils extends StateMachineUtils {
public static <S, E> void setCurrentState(StateMachine<S, E> stateMachine, S state) {
if (stateMachine instanceof AbstractStateMachine) {
setCurrentState((AbstractStateMachine<S, E>) stateMachine, state);
System.out.println("StateMachine Current:" + stateMachine);
} else {
throw new IllegalArgumentException("Provided StateMachine is not a valid type");
}
}
public static <S, E> void setCurrentState(AbstractStateMachine<S, E> stateMachine, S state) {
Method method = ReflectUtil.getMethod(stateMachine.getClass(),"setCurrentStateInternal"
,State.class, Message.class, Transition.class,Boolean.class,StateMachine.class,
Collection.class,Collection.class);
method.setAccessible(true);
ReflectionUtils.invokeMethod(method,stateMachine,findState(stateMachine, state), null, null, false, stateMachine,null,null);
}
private static <S, E> State<S, E> findState(AbstractStateMachine<S, E> stateMachine, S stateId) {
for (State<S, E> state : stateMachine.getStates()) {
if (state.getId() == stateId) {
return state;
}
}
throw new IllegalArgumentException("Specified State ID is not valid");
}
}
ReflectUtil是hutool里的,ReflectionUtils是spring的。
ReflectUtil能反射出方法但执行不了,ReflectionUtils反射不出方法,所以混着用
Spring StateMachine状态机引擎在项目中的应用(八)-灵活指定引擎实例状态一直以来,总觉得ssm不够灵活,主要原因是没找到为状态机指定状态的方式,也就意味着状态机引擎实例必须要跟对应的业务数据一起持久化,虽然ssm提供了多重持久化的方式,依然觉得有...