Bug 136900 - NoSuchElementException on removing elements from ASTNode.NodeList
Summary: NoSuchElementException on removing elements from ASTNode.NodeList
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-04-15 01:50 EDT by Ahmed Ashour CLA
Modified: 2009-08-30 02:20 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 Ahmed Ashour CLA 2006-04-15 01:50:44 EDT
3.2RC1, during iterating over CompilationUnit.imports() and removing some entries, NoSuchElementException is raised.

CompilationUnit unit = ...
for( ImportDeclaration id : (List<ImportDeclaration>)unit.imports() )
	if( condition )
		unit.imports().remove( id );

Java 5 features are not the cause. After some checking, ASTNode.NodeList does not overrise iterator() to be synchronized with NodeList.store.

If this is an improper usage, UnsupportedOperationException should be raised during remove() instead.
Comment 1 Olivier Thomann CLA 2006-04-18 23:36:05 EDT
If I modify it the way you suggest it, I get a java.util.ConcurrentModificationException instead of a current NoSuchElementException.

Is this what you want?

This would be consistent with what happens when trying to modify a list in the same time the list is iterated.
Comment 2 Ahmed Ashour CLA 2006-04-19 07:02:28 EDT
What about something like:

	public static void main( String[] args ) throws Exception {
		List<String> list = new ArrayList<String>();
		list.add( "Hello" );
		list.add( "there" );
		for( String s : list )
			if( s.equals( "Hello" ) )
				list.remove( s );
		System.out.println( "Finished " + list.size() );
	}

There is no exception.

I don't know when ConcurrentModificationException is exactly thrown, as the documentation says something about fail-fast iterator. But I guess someway should be there for a much better usability.

Hope that clarifies, many thanks.
Comment 3 Olivier Thomann CLA 2006-04-19 13:14:35 EDT
If you put more elements in your list, you would get the exception.
For example,
import java.util.ArrayList;
import java.util.List;

public class X {

	public static void main(String[] args) throws Exception {
		List<String> list = new ArrayList<String>();
		list.add("Hello");
		list.add("there");
		list.add("Hello");
		list.add("there");
		list.add("Hello");
		list.add("there");
		list.add("Hello");
		list.add("there");
		list.add("Hello");
		list.add("there");
		for (String s : list)
			if (s.equals("Hello"))
				list.remove(s);
		System.out.println("Finished " + list.size());
	}
}

leads to:
Exception in thread "main" java.util.ConcurrentModificationException
	at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:449)
	at java.util.AbstractList$Itr.next(AbstractList.java:420)
	at X.main(X.java:15)
Comment 4 Olivier Thomann CLA 2006-04-19 14:26:14 EDT
Changing this actually breaks many rewriting tests.
Defer post 3.2.
Comment 5 Denis Roy CLA 2009-08-30 02:20:53 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.