Buggy code:
public final class ContextManager implements IContextManager { private SortedSet activeContextIds = new TreeSet ( ) ; private SortedMap contextElementsById = new TreeMap ( ) ; private IContextManagerEvent contextManagerEvent ; private List contextManagerListeners ; private SortedMap contextsById = new TreeMap ( ) ; private SortedSet definedContextIds = new TreeSet ( ) ; private RegistryReader registryReader ; public ContextManager ( ) { } public void addContextManagerListener ( IContextManagerListener contextManagerListener ) throws IllegalArgumentException { } public SortedSet getActiveContextIds ( ) { } public IContext getContext ( String contextId ) throws IllegalArgumentException { } public SortedSet getDefinedContextIds ( ) { } public void removeContextManagerListener ( IContextManagerListener contextManagerListener ) throws IllegalArgumentException { } public void setActiveContextIds ( SortedSet activeContextIds ) throws IllegalArgumentException { } public void updateDefinedContextIds ( ) { if ( ( registryReader ) == null ) registryReader = new RegistryReader ( Platform . getPluginRegistry ( ) ) ; registryReader . load ( ) ; SortedMap contextElementsById = ContextElement . sortedMapById ( registryReader . getContextElements ( ) ) ; SortedSet contextElementAdditions = new TreeSet ( ) ; SortedSet contextElementChanges = new TreeSet ( ) ; SortedSet contextElementRemovals = new TreeSet ( ) ; Iterator iterator = contextElementsById . entrySet ( ) . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry entry = ( ( Map . Entry ) ( iterator . next ( ) ) ) ; String id = ( ( String ) ( entry . getKey ( ) ) ) ; ContextElement contextElement = ( ( ContextElement ) ( entry . getValue ( ) ) ) ; if ( ! ( this . contextElementsById . containsKey ( id ) ) ) contextElementAdditions . add ( id ) ; else if ( ! ( Util . equals ( contextElement , this . contextElementsById . get ( id ) ) ) ) contextElementChanges . add ( id ) ; } iterator = this . contextElementsById . entrySet ( ) . iterator ( ) ; while ( iterator . hasNext ( ) ) { Map . Entry entry = ( ( Map . Entry ) ( iterator . next ( ) ) ) ; String id = ( ( String ) ( entry . getKey ( ) ) ) ; ContextElement contextElement = ( ( ContextElement ) ( entry . getValue ( ) ) ) ; if ( ! ( contextElementsById . containsKey ( id ) ) ) contextElementRemovals . add ( id ) ; } SortedSet contextChanges = new TreeSet ( ) ; contextChanges . addAll ( contextElementAdditions ) ; contextChanges . addAll ( contextElementChanges ) ; contextChanges . addAll ( contextElementRemovals ) ; if ( ! ( contextChanges . isEmpty ( ) ) ) { this . contextElementsById = contextElementsById ; <START_BUG> SortedSet definedContextIds = ( ( SortedSet ) ( contextElementsById . keySet ( ) ) ) ; <END_BUG> if ( ! ( Util . equals ( definedContextIds , this . definedContextIds ) ) ) { this . definedContextIds = definedContextIds ; fireContextManagerChanged ( ) ; } iterator = contextChanges . iterator ( ) ; while ( iterator . hasNext ( ) ) fireContextChanged ( ( ( String ) ( iterator . next ( ) ) ) ) ; } } ContextElement getContextElement ( String contextId ) { } private void fireContextChanged ( String contextId ) { } private void fireContextManagerChanged ( ) { } }

Fix:
SortedSet definedContextIds = new TreeSet ( contextElementsById . keySet ( ) ) ;