This is the mail archive of the
java-patches@gcc.gnu.org
mailing list for the Java project.
FYI: fix PR 27028, 24752 - Iterator and ConcurrentModificationException
- From: Bryce McKinlay <bryce at mckinlay dot net dot nz>
- To: Classpath patch list <classpath-patches at gnu dot org>, Java Patches <java-patches at gcc dot gnu dot org>
- Date: Wed, 05 Apr 2006 14:30:30 -0400
- Subject: FYI: fix PR 27028, 24752 - Iterator and ConcurrentModificationException
This patch fixes bugs 27028 and 24752.
As a new muave test case
(gnu.testlet.java.util.Iterator.ConcurrentModification) demonstrates,
Sun's Iterator and ListIterator implementations do not throw
ConcurrentModificationException on the hasNext(), hasPrevious(),
nextIndex(), and previousIndex() methods. It turns out that some
applications, including Eclipse, rely on the behaviour of Sun's
implementation, and there is a small performance benefit to removing the
checks.
Although the spec states that it is invalid to perform any operation on
an Iterator after its backing collection has been modified, in practice
it is arguably redundant for these methods to do the check as they will
typically be called in tandem with next(), etc, if their results are
actually used.
I'm checking this in to Classpath HEAD, and gcc-4_1-branch.
Bryce
2006-04-05 Bryce McKinlay <mckinlay@redhat.com>
PR classpath/27028, PR classpath/24752:
* java/util/AbstractList.java (hasNext): Don't throw
ConcurrentModificationException. Update Javadoc.
(hasPrevious): Likewise.
(nextIndex): Likewise.
(previousIndex): Likewise.
* java/util/HashMap.java (hasNext): Likewise.
* java/util/Hashtable.java (hasNext): Likewise.
* java/util/IdentityHashMap.java (hasNext): Likewise.
* java/util/LinkedHashMap.java (hasNext): Likewise.
* java/util/LinkedList.java (nextIndex): Likewise.
(previousIndex): Likewise.
(hasNext): Likewise.
(hasPrevious): Likewise.
* java/util/TreeMap.java (hasNext): Likewise.
* java/util/WeakHashMap.java (hasNext): Likewise.
Index: java/util/AbstractList.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/AbstractList.java,v
retrieving revision 1.25
diff -u -r1.25 AbstractList.java
--- java/util/AbstractList.java 2 Jul 2005 20:32:41 -0000 1.25
+++ java/util/AbstractList.java 5 Apr 2006 17:53:41 -0000
@@ -327,12 +327,9 @@
*
* @return True if the end of the list has not yet been
* reached.
- * @throws ConcurrentModificationException if the
- * list has been modified elsewhere.
*/
public boolean hasNext()
{
- checkMod();
return pos < size;
}
@@ -461,12 +458,9 @@
*
* @return True if the end of the list has not yet been
* reached.
- * @throws ConcurrentModificationException if the
- * list has been modified elsewhere.
*/
public boolean hasNext()
{
- checkMod();
return position < size;
}
@@ -476,12 +470,9 @@
*
* @return True if objects exist prior to the current
* position of the iterator.
- * @throws ConcurrentModificationException if the
- * list has been modified elsewhere.
*/
public boolean hasPrevious()
{
- checkMod();
return position > 0;
}
@@ -526,12 +517,9 @@
* list, which will be retrieved by <code>next()</code>
*
* @return The index of the next element.
- * @throws ConcurrentModificationException if the list
- * has been modified elsewhere.
*/
public int nextIndex()
{
- checkMod();
return position;
}
@@ -540,12 +528,9 @@
* list, which will be retrieved by <code>previous()</code>
*
* @return The index of the previous element.
- * @throws ConcurrentModificationException if the list
- * has been modified elsewhere.
*/
public int previousIndex()
{
- checkMod();
return position - 1;
}
@@ -1030,12 +1015,9 @@
*
* @return True if the end of the list has not yet been
* reached.
- * @throws ConcurrentModificationException if the
- * list has been modified elsewhere.
*/
public boolean hasNext()
{
- checkMod();
return position < size;
}
@@ -1045,12 +1027,9 @@
*
* @return True if objects exist prior to the current
* position of the iterator.
- * @throws ConcurrentModificationException if the
- * list has been modified elsewhere.
*/
public boolean hasPrevious()
{
- checkMod();
return position > 0;
}
@@ -1093,8 +1072,6 @@
* list, which will be retrieved by <code>next()</code>
*
* @return The index of the next element.
- * @throws ConcurrentModificationException if the
- * list has been modified elsewhere.
*/
public int nextIndex()
{
@@ -1106,8 +1083,6 @@
* list, which will be retrieved by <code>previous()</code>
*
* @return The index of the previous element.
- * @throws ConcurrentModificationException if the
- * list has been modified elsewhere.
*/
public int previousIndex()
{
Index: java/util/HashMap.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/HashMap.java,v
retrieving revision 1.31
diff -u -r1.31 HashMap.java
--- java/util/HashMap.java 13 Sep 2005 21:25:10 -0000 1.31
+++ java/util/HashMap.java 5 Apr 2006 17:53:42 -0000
@@ -849,12 +849,9 @@
/**
* Returns true if the Iterator has more elements.
* @return true if there are more elements
- * @throws ConcurrentModificationException if the HashMap was modified
*/
public boolean hasNext()
{
- if (knownMod != modCount)
- throw new ConcurrentModificationException();
return count > 0;
}
Index: java/util/Hashtable.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/Hashtable.java,v
retrieving revision 1.38
diff -u -r1.38 Hashtable.java
--- java/util/Hashtable.java 12 Jan 2006 16:19:06 -0000 1.38
+++ java/util/Hashtable.java 5 Apr 2006 17:53:42 -0000
@@ -1017,12 +1017,9 @@
/**
* Returns true if the Iterator has more elements.
* @return true if there are more elements
- * @throws ConcurrentModificationException if the hashtable was modified
*/
public boolean hasNext()
{
- if (knownMod != modCount)
- throw new ConcurrentModificationException();
return count > 0;
}
Index: java/util/IdentityHashMap.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/IdentityHashMap.java,v
retrieving revision 1.18
diff -u -r1.18 IdentityHashMap.java
--- java/util/IdentityHashMap.java 2 Jul 2005 20:32:42 -0000 1.18
+++ java/util/IdentityHashMap.java 5 Apr 2006 17:53:42 -0000
@@ -705,12 +705,9 @@
/**
* Returns true if the Iterator has more elements.
* @return true if there are more elements
- * @throws ConcurrentModificationException if the Map was modified
*/
public boolean hasNext()
{
- if (knownMod != modCount)
- throw new ConcurrentModificationException();
return count > 0;
}
Index: java/util/LinkedHashMap.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/LinkedHashMap.java,v
retrieving revision 1.9
diff -u -r1.9 LinkedHashMap.java
--- java/util/LinkedHashMap.java 2 Jul 2005 20:32:42 -0000 1.9
+++ java/util/LinkedHashMap.java 5 Apr 2006 17:53:42 -0000
@@ -452,12 +452,9 @@
* Returns true if the Iterator has more elements.
*
* @return true if there are more elements
- * @throws ConcurrentModificationException if the HashMap was modified
*/
public boolean hasNext()
{
- if (knownMod != modCount)
- throw new ConcurrentModificationException();
return current != null;
}
Index: java/util/LinkedList.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/LinkedList.java,v
retrieving revision 1.26
diff -u -r1.26 LinkedList.java
--- java/util/LinkedList.java 5 Jul 2005 10:28:03 -0000 1.26
+++ java/util/LinkedList.java 5 Apr 2006 17:53:42 -0000
@@ -804,11 +804,9 @@
* Returns the index of the next element.
*
* @return the next index
- * @throws ConcurrentModificationException if the list was modified
*/
public int nextIndex()
{
- checkMod();
return position;
}
@@ -816,11 +814,9 @@
* Returns the index of the previous element.
*
* @return the previous index
- * @throws ConcurrentModificationException if the list was modified
*/
public int previousIndex()
{
- checkMod();
return position - 1;
}
@@ -828,11 +824,9 @@
* Returns true if more elements exist via next.
*
* @return true if next will succeed
- * @throws ConcurrentModificationException if the list was modified
*/
public boolean hasNext()
{
- checkMod();
return (next != null);
}
@@ -840,11 +834,9 @@
* Returns true if more elements exist via previous.
*
* @return true if previous will succeed
- * @throws ConcurrentModificationException if the list was modified
*/
public boolean hasPrevious()
{
- checkMod();
return (previous != null);
}
Index: java/util/TreeMap.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/TreeMap.java,v
retrieving revision 1.29
diff -u -r1.29 TreeMap.java
--- java/util/TreeMap.java 13 Sep 2005 22:19:15 -0000 1.29
+++ java/util/TreeMap.java 5 Apr 2006 17:53:43 -0000
@@ -1434,12 +1434,9 @@
/**
* Returns true if the Iterator has more elements.
* @return true if there are more elements
- * @throws ConcurrentModificationException if the TreeMap was modified
*/
public boolean hasNext()
{
- if (knownMod != modCount)
- throw new ConcurrentModificationException();
return next != max;
}
Index: java/util/WeakHashMap.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/WeakHashMap.java,v
retrieving revision 1.21
diff -u -r1.21 WeakHashMap.java
--- java/util/WeakHashMap.java 10 Jan 2006 07:26:04 -0000 1.21
+++ java/util/WeakHashMap.java 5 Apr 2006 17:53:43 -0000
@@ -292,12 +292,9 @@
/**
* Checks if there are more entries.
* @return true, iff there are more elements.
- * @throws ConcurrentModificationException if the hash map was
- * modified.
*/
public boolean hasNext()
{
- checkMod();
return nextEntry != null;
}