提交 412ce557 编写于 作者: M mduigou

4802647: Throw required NPEs from removeAll()/retainAll()

Reviewed-by: mduigou, chegar, dholmes
Contributed-by: NBrandon Passanisi <brandon.passanisi@oracle.com>
上级 f6ef9ec5
......@@ -368,6 +368,7 @@ public abstract class AbstractCollection<E> implements Collection<E> {
* @see #contains(Object)
*/
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
Iterator<?> it = iterator();
while (it.hasNext()) {
......@@ -401,6 +402,7 @@ public abstract class AbstractCollection<E> implements Collection<E> {
* @see #contains(Object)
*/
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
Iterator<E> it = iterator();
while (it.hasNext()) {
......
......@@ -166,6 +166,7 @@ public abstract class AbstractSet<E> extends AbstractCollection<E> implements Se
* @see #contains(Object)
*/
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
boolean modified = false;
if (size() > c.size()) {
......
......@@ -671,6 +671,7 @@ public class ArrayList<E> extends AbstractList<E>
* @see Collection#contains(Object)
*/
public boolean removeAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, false);
}
......@@ -691,6 +692,7 @@ public class ArrayList<E> extends AbstractList<E>
* @see Collection#contains(Object)
*/
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
......
......@@ -26,6 +26,7 @@
* @bug 6207984 6272521 6192552 6269713 6197726 6260652 5073546 4137464
* 4155650 4216399 4294891 6282555 6318622 6355327 6383475 6420753
* 6431845 4802633 6570566 6570575 6570631 6570924 6691185 6691215
* 4802647 7123424
* @summary Run many tests on many Collection and Map implementations
* @author Martin Buchholz
* @run main MOAT
......@@ -58,6 +59,8 @@ import static java.util.Collections.*;
public class MOAT {
public static void realMain(String[] args) {
testCollection(new NewAbstractCollection<Integer>());
testCollection(new NewAbstractSet<Integer>());
testCollection(new LinkedHashSet<Integer>());
testCollection(new HashSet<Integer>());
testCollection(new Vector<Integer>());
......@@ -753,6 +756,14 @@ public class MOAT {
// The "all" operations should throw NPE when passed null
//----------------------------------------------------------------
{
clear(c);
try {
c.removeAll(null);
fail("Expected NullPointerException");
}
catch (NullPointerException e) { pass(); }
catch (Throwable t) { unexpected(t); }
oneElement(c);
try {
c.removeAll(null);
......@@ -761,6 +772,14 @@ public class MOAT {
catch (NullPointerException e) { pass(); }
catch (Throwable t) { unexpected(t); }
clear(c);
try {
c.retainAll(null);
fail("Expected NullPointerException");
}
catch (NullPointerException e) { pass(); }
catch (Throwable t) { unexpected(t); }
oneElement(c);
try {
c.retainAll(null);
......@@ -1205,4 +1224,35 @@ public class MOAT {
static <T> T serialClone(T obj) {
try { return (T) readObject(serializedForm(obj)); }
catch (Exception e) { throw new Error(e); }}
private static class NewAbstractCollection<E> extends AbstractCollection<E> {
ArrayList<E> list = new ArrayList<>();
public boolean remove(Object obj) {
return list.remove(obj);
}
public boolean add(E e) {
return list.add(e);
}
public Iterator<E> iterator() {
return list.iterator();
}
public int size() {
return list.size();
}
}
private static class NewAbstractSet<E> extends AbstractSet<E> {
HashSet<E> set = new HashSet<>();
public boolean remove(Object obj) {
return set.remove(obj);
}
public boolean add(E e) {
return set.add(e);
}
public Iterator<E> iterator() {
return set.iterator();
}
public int size() {
return set.size();
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册