8221924: get(null) on single-entry unmodifiable Map returns null instead of throwing NPE
authorsmarks
Tue, 09 Apr 2019 09:49:36 -0700
changeset 54475 805584336738
parent 54474 0fa2903fb272
child 54476 20f7bbfc61d3
8221924: get(null) on single-entry unmodifiable Map returns null instead of throwing NPE Reviewed-by: redestad, lancea
src/java.base/share/classes/java/util/ImmutableCollections.java
test/jdk/java/util/Map/MapFactories.java
--- a/src/java.base/share/classes/java/util/ImmutableCollections.java	Mon Apr 08 14:40:31 2019 +0800
+++ b/src/java.base/share/classes/java/util/ImmutableCollections.java	Tue Apr 09 09:49:36 2019 -0700
@@ -882,6 +882,11 @@
         }
 
         @Override
+        public V get(Object o) {
+            return o.equals(k0) ? v0 : null; // implicit nullcheck of o
+        }
+
+        @Override
         public boolean containsKey(Object o) {
             return o.equals(k0); // implicit nullcheck of o
         }
--- a/test/jdk/java/util/Map/MapFactories.java	Mon Apr 08 14:40:31 2019 +0800
+++ b/test/jdk/java/util/Map/MapFactories.java	Tue Apr 09 09:49:36 2019 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -50,7 +50,7 @@
 
 /*
  * @test
- * @bug 8048330
+ * @bug 8048330 8221924
  * @summary Test convenience static factory methods on Map.
  * @run testng MapFactories
  */
@@ -386,6 +386,11 @@
         act.containsKey(null);
     }
 
+    @Test(dataProvider="all", expectedExceptions=NullPointerException.class)
+    public void getNullShouldThrowNPE(Map<Integer,String> act, Map<Integer,String> exp) {
+        act.get(null);
+    }
+
     @Test(dataProvider="all")
     public void serialEquality(Map<Integer, String> act, Map<Integer, String> exp) {
         // assume that act.equals(exp) tested elsewhere