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反射不出方法,所以混着用

IP属地:天津