8223726: j.l.c.MethodTypeDesc spec should contain precise assertions for one parameter's methods
authorvromero
Thu, 16 May 2019 13:34:33 -0400
changeset 54906 b6ee58ec8814
parent 54905 d43cc2039ccf
child 54907 6bcda7ed24ef
8223726: j.l.c.MethodTypeDesc spec should contain precise assertions for one parameter's methods Reviewed-by: darcy
src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java
src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
test/jdk/java/lang/constant/MethodHandleDescTest.java
test/jdk/java/lang/constant/MethodTypeDescTest.java
--- a/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java	Thu May 16 19:07:31 2019 +0200
+++ b/src/java.base/share/classes/java/lang/constant/MethodHandleDesc.java	Thu May 16 13:34:33 2019 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 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
@@ -177,7 +177,7 @@
      * @param paramTypes {@link ClassDesc}s describing the parameter types of
      *                   the constructor
      * @return the {@linkplain MethodHandleDesc}
-     * @throws NullPointerException if any of the arguments are null
+     * @throws NullPointerException if any argument or its contents is {@code null}
      */
     static DirectMethodHandleDesc ofConstructor(ClassDesc owner,
                                                 ClassDesc... paramTypes) {
@@ -191,6 +191,7 @@
      *
      * @param type a {@link MethodHandleDesc} describing the new method type
      * @return a {@linkplain MethodHandleDesc} for the adapted method handle
+     * @throws NullPointerException if the argument is {@code null}
      */
     default MethodHandleDesc asType(MethodTypeDesc type) {
         return (invocationType().equals(type)) ? this : new AsTypeMethodHandleDesc(this, type);
--- a/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java	Thu May 16 19:07:31 2019 +0200
+++ b/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java	Thu May 16 13:34:33 2019 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 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
@@ -49,7 +49,7 @@
      *
      * @param descriptor a method descriptor string
      * @return a {@linkplain MethodTypeDesc} describing the desired method type
-     * @throws NullPointerException if any argument is {@code null}
+     * @throws NullPointerException if the argument is {@code null}
      * @throws IllegalArgumentException if the descriptor string is not a valid
      * method descriptor
      * @jvms 4.3.3 Method Descriptors
@@ -116,7 +116,7 @@
      *
      * @param returnType a {@link ClassDesc} describing the new return type
      * @return a {@linkplain MethodTypeDesc} describing the desired method type
-     * @throws NullPointerException if any argument is {@code null}
+     * @throws NullPointerException if the argument is {@code null}
      */
     MethodTypeDesc changeReturnType(ClassDesc returnType);
 
--- a/test/jdk/java/lang/constant/MethodHandleDescTest.java	Thu May 16 19:07:31 2019 +0200
+++ b/test/jdk/java/lang/constant/MethodHandleDescTest.java	Thu May 16 13:34:33 2019 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 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
@@ -152,6 +152,24 @@
         } catch (IllegalArgumentException ex) {
             // good
         }
+
+        // null list of parameters
+        try {
+            MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList", null));
+            fail("should have failed: null list of parameters");
+        } catch (NullPointerException ex) {
+            // good
+        }
+
+        // null elements in list of parameters
+        try {
+            ClassDesc[] paramList = new ClassDesc[1];
+            paramList[0] = null;
+            MethodHandleDesc.ofConstructor(ClassDesc.of("java.util.ArrayList"), paramList);
+            fail("should have failed: null content in list of parameters");
+        } catch (NullPointerException ex) {
+            // good
+        }
     }
 
     public void testAsType() throws Throwable {
@@ -184,6 +202,13 @@
         MethodHandleDesc same = mhr.asType(mhr.invocationType());
         assertSame(mhr, same);
 
+        try {
+            mhr.asType(null);
+            fail("Expected NPE");
+        } catch (NullPointerException ex) {
+            // good
+        }
+
         // @@@ Test varargs adaptation
         // @@@ Test bad adaptations and assert runtime error on resolution
         // @@@ Test intrinsification of adapted MH
--- a/test/jdk/java/lang/constant/MethodTypeDescTest.java	Thu May 16 19:07:31 2019 +0200
+++ b/test/jdk/java/lang/constant/MethodTypeDescTest.java	Thu May 16 13:34:33 2019 -0400
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 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
@@ -103,6 +103,14 @@
             testMethodTypeDesc(newDesc, mt.changeReturnType((Class<?>)rc.resolveConstantDesc(LOOKUP)));
         }
 
+        // try with null parameter
+        try {
+            MethodTypeDesc newDesc = mtDesc.changeReturnType(null);
+            fail("should fail with NPE");
+        } catch (NullPointerException ex) {
+            // good
+        }
+
         // changeParamType
         for (int i=0; i<paramTypes.length; i++) {
             for (String p : paramDescs) {
@@ -233,6 +241,14 @@
             }
         }
 
+        // try with null argument
+        try {
+            MethodTypeDesc r = MethodTypeDesc.ofDescriptor(null);
+            fail("should fail with NPE");
+        } catch (NullPointerException ex) {
+            // good
+        }
+
         // try with void arguments, this will stress another code path in particular
         // ConstantMethodTypeDesc::init
         try {