On Tue, 16 Feb 2021 23:18:55 GMT, Claes Redestad <redes...@openjdk.org> wrote:
>> Modify the `unmodifiable*` methods in `java.util.Collections` to be >> idempotent. That is, when given an immutable collection from >> `java.util.ImmutableCollections` or `java.util.Collections`, these methods >> will return the reference instead of creating a new immutable collection >> that wraps the existing one. > > src/java.base/share/classes/java/util/Collections.java line 1130: > >> 1128: public static <T> Set<T> unmodifiableSet(Set<? extends T> s) { >> 1129: if(s.getClass() == UnmodifiableSet.class || >> 1130: s.getClass() == ImmutableCollections.Set12.class || > > These might be problematic: `ImmutableCollections.Set*` differs in behavior > subtly from `UnmodifiableSet`, e.g., `set.contains(null)` will throw an NPE. > I'd limit the check and optimization to `UnmodifiableSet` here No? This unmodifiable set here just delegates call to the backing field `c`, so all exceptions from `c`'s calls are just delegated, aren't they? The NPE will still be thrown; it's just that the stack trace will be different (i.e. one more level of call in the stacktrace) ------------- PR: https://git.openjdk.java.net/jdk/pull/2596