Description
Let's say I have a state machine with the following structure :
Initial State --> Choice "Choice_State" :
(guard) --> State "One"
(else) --> State "Default"
And I configure an Interceptor that looks just like this:
public class CustomInterceptor extends StateMachineInterceptorAdapter<CustomState, CustomEvent> {
@Override
public void preStateChange(State<Custom State, CustomEvent> state, Message<CustomEvent> message, Transition<Custom State, CustomEvent> transition, StateMachine<Custom State, CustomEvent> stateMachine) {
System.out.printf("Interceptor - preState : %s\n", state.getId());
super.preStateChange(state, message, transition, stateMachine);
}
@Override
public void postStateChange(State<Custom State, CustomEvent> state, Message<CustomEvent> message, Transition<Custom State, CustomEvent> transition, StateMachine<Custom State, CustomEvent> stateMachine) {
System.out.printf("Interceptor - postState : %s\n", state.getId());
super.postStateChange(state, message, transition, stateMachine);
}
Depending on what the guard function returns, the following paths are possible :
- if guard returns true :
Initial State --> "Choice_State" -> "One"
The interceptor prints the following info:
Interceptor - preState One
Interceptor - postState Choice_State
- if guard returns false :
Initial State --> "Choice_State" -> "Default"
The interceptor prints the following info:
Interceptor - preState Default
Interceptor - postState Choice_State
In both cases, I expected the postStateChangeInterceptor to show the same state as the preState interceptor.
I believe the culprit is the following call in AbstractStateMachine::switchToState
:
callPostStateChangeInterceptors(state, message, transition, stateMachine);
When it should be
callPostStateChangeInterceptors(toState, message, transition, stateMachine);
The toState
variable holds a reference to the actual state the state machine will transition to (after pseudo states transitions are followed), whereas the state
variable holds a reference to the destination state before following pseudo states transitions.