--- old/src/share/classes/java/util/Collections.java 2014-06-23 17:20:11.989073012 -0700 +++ new/src/share/classes/java/util/Collections.java 2014-06-23 17:20:11.789073002 -0700 @@ -3031,9 +3031,10 @@ final Collection c; final Class type; - void typeCheck(Object o) { + E typeCheck(Object o) { if (o != null && !type.isInstance(o)) throw new ClassCastException(badElementMsg(o)); + return (E) o; } private String badElementMsg(Object o) { @@ -3042,10 +3043,8 @@ } CheckedCollection(Collection c, Class type) { - if (c==null || type == null) - throw new NullPointerException(); - this.c = c; - this.type = type; + this.c = Objects.requireNonNull(c, "c"); + this.type = Objects.requireNonNull(type, "type"); } public int size() { return c.size(); } @@ -3091,7 +3090,7 @@ @SuppressWarnings("unchecked") Collection checkedCopyOf(Collection coll) { - Object[] a = null; + Object[] a; try { E[] z = zeroLengthElementArray(); a = coll.toArray(z); @@ -3487,10 +3486,17 @@ return new CheckedList<>(list.subList(fromIndex, toIndex), type); } + /** + * {@inheritDoc} + * + * @throws ClassCastException if the class of an element returned by the + * operator prevents it from being added to this collection + */ @Override public void replaceAll(UnaryOperator operator) { - list.replaceAll(operator); + list.replaceAll(e -> typeCheck(operator.apply(e))); } + @Override public void sort(Comparator c) { list.sort(c);