diff --git a/jdk/src/share/classes/java/util/ArrayList.java b/jdk/src/share/classes/java/util/ArrayList.java index b06c8fd1913..cdae99d0534 100644 --- a/jdk/src/share/classes/java/util/ArrayList.java +++ b/jdk/src/share/classes/java/util/ArrayList.java @@ -609,11 +609,14 @@ public class ArrayList extends AbstractList * @throws IndexOutOfBoundsException if {@code fromIndex} or * {@code toIndex} is out of range * ({@code fromIndex < 0 || - * fromIndex >= size() || * toIndex > size() || * toIndex < fromIndex}) */ protected void removeRange(int fromIndex, int toIndex) { + if (fromIndex > toIndex) { + throw new IndexOutOfBoundsException( + outOfBoundsMsg(fromIndex, toIndex)); + } modCount++; int numMoved = size - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, @@ -655,6 +658,13 @@ public class ArrayList extends AbstractList return "Index: "+index+", Size: "+size; } + /** + * A version used in checking (fromIndex > toIndex) condition + */ + private static String outOfBoundsMsg(int fromIndex, int toIndex) { + return "From Index: " + fromIndex + " > To Index: " + toIndex; + } + /** * Removes from this list all of its elements that are contained in the * specified collection. diff --git a/jdk/test/java/util/Collection/MOAT.java b/jdk/test/java/util/Collection/MOAT.java index 88914952799..f50682fea24 100644 --- a/jdk/test/java/util/Collection/MOAT.java +++ b/jdk/test/java/util/Collection/MOAT.java @@ -54,6 +54,7 @@ import java.io.*; import java.util.*; import java.util.concurrent.*; import static java.util.Collections.*; +import java.lang.reflect.*; public class MOAT { public static void realMain(String[] args) { @@ -721,6 +722,28 @@ public class MOAT { equal(l instanceof RandomAccess, l.subList(0,0) instanceof RandomAccess); + + l.iterator(); + l.listIterator(); + l.listIterator(0); + l.listIterator(l.size()); + THROWS(IndexOutOfBoundsException.class, + new Fun(){void f(){l.listIterator(-1);}}, + new Fun(){void f(){l.listIterator(l.size() + 1);}}); + + if (l instanceof AbstractList) { + try { + int size = l.size(); + AbstractList abList = (AbstractList) l; + Method m = AbstractList.class.getDeclaredMethod("removeRange", new Class[] { int.class, int.class }); + m.setAccessible(true); + m.invoke(abList, new Object[] { 0, 0 }); + m.invoke(abList, new Object[] { size, size }); + equal(size, l.size()); + } + catch (UnsupportedOperationException ignored) {/* OK */} + catch (Throwable t) { unexpected(t); } + } } private static void testCollection(Collection c) {