Hi all,

Please review this change to make the immutable collections (List.of, Set.of, Map.of) throw UnsupportedOperationException unconditionally. That is, they should throw UOE even if a mutator method is called with arguments that make it a no-op. Calling a mutator method on an immutable collection is always a programming error, and having it sometimes be a no-op potentially leads to errors.

Note that the existing Collections unmodifiable wrappers always throw UOE unconditionally. This change makes the immutable collections behave consistently with the unmodifiable wrappers. For example,

    List<String> unmodList = Collections.unmodifiableList(new ArrayList<>());
    unmodList.addAll(List.of()); // throws UOE
    List.of().addAll(List.of()); // currently does nothing, change to throw UOE

Unfortunately, various other specialized collections such as emptyList() etc. behave differently, e.g.

    Collections.emptyList().addAll(List.of()); // does nothing

However, that change will be left for another day.

Bug:

    https://bugs.openjdk.java.net/browse/JDK-8159404

Webrev:

    http://cr.openjdk.java.net/~smarks/reviews/8159404/webrev.0/

Thanks,

s'marks

Reply via email to