Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 345520 - BasicCommandStack.flush() should reset mostRecentCommand before notifying its CommandStackListeners
Summary: BasicCommandStack.flush() should reset mostRecentCommand before notifying its...
Status: CLOSED FIXED
Alias: None
Product: EMF
Classification: Modeling
Component: Core (show other bugs)
Version: unspecified   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Ed Merks CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-05-12 01:40 EDT by thammers CLA
Modified: 2011-06-02 11:40 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description thammers CLA 2011-05-12 01:40:23 EDT
Build Identifier: I20110428-0848

Its hard for a CommandStackListener to distinguish whether its commandStackChanged() method was called from the (Basic)CommandStack because of an actual command execution (execute, redo, undo) or because of flushing the CommandStack. A simple workaround is to cache the last mostRecentCommand in the CommandStackListener and do a simple reference check against the the "new" mostRecentCommand. But this does not consider the fact, that commands are stateful (executed, undone, redone). If the mostRecentCommand in the BasicCommandStack would be set to null before notifying the command stack listeners these listeners would be able to realize that the command stack has been flushed (or according to the current implementation a RuntimeException occured during execute/undo/redo).

Reproducible: Always

Steps to Reproduce:
import java.util.EventObject;

import org.eclipse.emf.common.command.BasicCommandStack;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.common.command.CommandStack;
import org.eclipse.emf.common.command.CommandStackListener;
import org.eclipse.emf.common.command.IdentityCommand;

public class FlushTest {
  
  public static void main(final String[] args) {
    final BasicCommandStack cmdStack = new BasicCommandStack();
    cmdStack.addCommandStackListener(new CommandStackListener() {
      Command lastCmd;
      @Override
      public void commandStackChanged(final EventObject event) {
        final Command cmd = ((CommandStack) event.getSource()).getMostRecentCommand();
        if (cmd == lastCmd) {
          throw new IllegalStateException("mostRecentCommand has not changed");
        }
        lastCmd = cmd;
      }
    });
    cmdStack.execute(new IdentityCommand());
    cmdStack.flush(); // boom
  }
  
}
Comment 1 Ed Merks CLA 2011-05-12 15:25:08 EDT
The fix is committed to CVS for 2.7.
Comment 2 Ed Merks CLA 2011-06-02 11:40:17 EDT
The fix is available in 3.7RC3 and later.