changeset 54720:c48f141e7c5b

8219483: j.l.c.ClassDesc::nested(String, String...) doesn't throw NPE if any arg is null Reviewed-by: darcy
author vromero
date Mon, 06 May 2019 14:47:55 -0400
parents 4f2fd02922b1
children 3661ad97da8f
files src/java.base/share/classes/java/lang/constant/ClassDesc.java test/jdk/java/lang/constant/ClassDescTest.java
diffstat 2 files changed, 30 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/lang/constant/ClassDesc.java	Mon May 06 09:58:14 2019 -0700
+++ b/src/java.base/share/classes/java/lang/constant/ClassDesc.java	Mon May 06 14:47:55 2019 -0400
@@ -213,7 +213,7 @@
      * @param moreNestedNames the unqualified name(s) of the remaining levels of
      *                       nested class
      * @return a {@linkplain ClassDesc} describing the nested class
-     * @throws NullPointerException if any argument is {@code null}
+     * @throws NullPointerException if any argument or its contents is {@code null}
      * @throws IllegalStateException if this {@linkplain ClassDesc} does not
      * describe a class or interface type
      * @throws IllegalArgumentException if the nested class name is invalid
@@ -221,6 +221,11 @@
     default ClassDesc nested(String firstNestedName, String... moreNestedNames) {
         if (!isClassOrInterface())
             throw new IllegalStateException("Outer class is not a class or interface type");
+        validateMemberName(firstNestedName, false);
+        requireNonNull(moreNestedNames);
+        for (String addNestedNames : moreNestedNames) {
+            validateMemberName(addNestedNames, false);
+        }
         return moreNestedNames.length == 0
                ? nested(firstNestedName)
                : nested(firstNestedName + Stream.of(moreNestedNames).collect(joining("$", "$", "")));
--- a/test/jdk/java/lang/constant/ClassDescTest.java	Mon May 06 09:58:14 2019 -0700
+++ b/test/jdk/java/lang/constant/ClassDescTest.java	Mon May 06 14:47:55 2019 -0400
@@ -306,4 +306,28 @@
         assertEquals(s.resolveConstantDesc(LOOKUP), s);
         assertEquals(s.describeConstable().get(), s);
     }
+
+    public void testNullNestedClasses() {
+        ClassDesc cd = ClassDesc.of("Bar");
+        try {
+            cd.nested(null);
+            fail("");
+        } catch (NullPointerException e) {
+            // good
+        }
+
+        try {
+            cd.nested("good", null);
+            fail("");
+        } catch (NullPointerException e) {
+            // good
+        }
+
+        try {
+            cd.nested("good", "goodToo", null);
+            fail("");
+        } catch (NullPointerException e) {
+            // good
+        }
+    }
 }