Hi there. It's my first time posting to the development list, so
please forgive any formatting errors.

My proposed change is pretty small. Basically, a pattern I see with
some frequency is calling Objects.requireNonNull(object, "objectName")
before (or within) Map.of(), because otherwise there's not much of a
way to tell _which_ parameter caused the exception. Even just an index
and key/value hint would make tracking down these types of errors
easier without that bit of boilerplate.

I'd expect that any performance impact to be negligible, already being
off the happy path, here.

Diff as I'm imagining it, but very open to suggestions:

diff --git a/src/java.base/share/classes/java/util/ImmutableCollections.java
b/src/java.base/share/classes/java/util/ImmutableCollections.java
index 3de7e1d5eae..814c1a9ec1b 100644
--- a/src/java.base/share/classes/java/util/ImmutableCollections.java
+++ b/src/java.base/share/classes/java/util/ImmutableCollections.java
@@ -1103,8 +1103,8 @@ class ImmutableCollections {
         private final V v0;

         Map1(K k0, V v0) {
-            this.k0 = Objects.requireNonNull(k0);
-            this.v0 = Objects.requireNonNull(v0);
+            this.k0 = Objects.requireNonNull(k0, () -> "Key in map
entry was null");
+            this.v0 = Objects.requireNonNull(v0, () -> "Value in map
entry was null");
         }

         @Override
@@ -1183,9 +1183,9 @@ class ImmutableCollections {

             for (int i = 0; i < input.length; i += 2) {
                 @SuppressWarnings("unchecked")
-                    K k = Objects.requireNonNull((K)input[i]);
+                    K k = Objects.requireNonNull((K)input[i], () ->
"Key for map entry " + (i / 2) + " was null");
                 @SuppressWarnings("unchecked")
-                    V v = Objects.requireNonNull((V)input[i+1]);
+                    V v = Objects.requireNonNull((V)input[i+1], () ->
"Value for map entry " + (i / 2) + " was null");
                 int idx = probe(k);
                 if (idx >= 0) {
                     throw new IllegalArgumentException("duplicate key: " + k);

-- 
This email message may contain privileged or confidential information, and 
is for the use of intended recipients only. Do not share with or forward to 
additional parties except as necessary to conduct the business for which 
this email (and attachments) was clearly intended. If you have received 
this message in error, please immediately advise the sender by reply email 
and then delete this message.

Reply via email to