Community
Participate
Working Groups
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.
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.
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.
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)
Changing this actually breaks many rewriting tests. Defer post 3.2.
As of now 'LATER' and 'REMIND' resolutions are no longer supported. Please reopen this bug if it is still valid for you.