8223803: j.l.c.MethodTypeDesc::insertParameterTypes​ doesn't control type of parameters
authorvromero
Fri, 17 May 2019 13:16:07 -0400
changeset 54929 657f6e484bc6
parent 54928 fe4c2de90b59
child 54930 43633b8e24c6
8223803: j.l.c.MethodTypeDesc::insertParameterTypes​ doesn't control type of parameters Reviewed-by: rriggs
src/java.base/share/classes/java/lang/constant/ConstantDesc.java
src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java
test/jdk/java/lang/constant/MethodTypeDescTest.java
--- a/src/java.base/share/classes/java/lang/constant/ConstantDesc.java	Fri May 17 11:47:06 2019 -0400
+++ b/src/java.base/share/classes/java/lang/constant/ConstantDesc.java	Fri May 17 13:16:07 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
@@ -97,6 +97,11 @@
      * @throws ReflectiveOperationException if a class, method, or field
      * could not be reflectively resolved in the course of resolution
      * @throws LinkageError if a linkage error occurs
+     *
+     * @apiNote {@linkplain MethodTypeDesc} can represent method type descriptors
+     * that are not representable by {@linkplain MethodType}, such as methods with
+     * more than 255 parameter slots, so attempts to resolve these may result in errors.
+     *
      * @jvms 5.4.3 Resolution
      * @jvms 5.4.4 Access Control
      */
--- a/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java	Fri May 17 11:47:06 2019 -0400
+++ b/src/java.base/share/classes/java/lang/constant/MethodTypeDesc.java	Fri May 17 13:16:07 2019 -0400
@@ -154,9 +154,11 @@
      * @param paramTypes {@link ClassDesc}s describing the new parameter types
      *                   to insert
      * @return a {@linkplain MethodTypeDesc} describing the desired method type
-     * @throws NullPointerException if any argument is {@code null}
+     * @throws NullPointerException if any argument or its contents are {@code null}
      * @throws IndexOutOfBoundsException if {@code pos} is outside the closed
      * range {[0, parameterCount]}
+     * @throws IllegalArgumentException if any element of {@code paramTypes}
+     * is a {@link ClassDesc} for {@code void}
      */
     MethodTypeDesc insertParameterTypes(int pos, ClassDesc... paramTypes);
 
--- a/test/jdk/java/lang/constant/MethodTypeDescTest.java	Fri May 17 11:47:06 2019 -0400
+++ b/test/jdk/java/lang/constant/MethodTypeDescTest.java	Fri May 17 13:16:07 2019 -0400
@@ -171,6 +171,34 @@
         } catch (IndexOutOfBoundsException ex) {
             // good
         }
+
+        try {
+            ClassDesc[] newParamTypes = new ClassDesc[1];
+            newParamTypes[0] = CD_void;
+            MethodTypeDesc newDesc = MethodTypeDesc.of(returnType, CD_int);
+            newDesc = newDesc.insertParameterTypes(0, newParamTypes);
+            fail("shouldn't allow parameters with class descriptor CD_void");
+        } catch (IllegalArgumentException ex) {
+            // good
+        }
+
+        try {
+            MethodTypeDesc newDesc = MethodTypeDesc.of(returnType, CD_int);
+            newDesc = newDesc.insertParameterTypes(0, null);
+            fail("should fail with NPE");
+        } catch (NullPointerException ex) {
+            // good
+        }
+
+        try {
+            ClassDesc[] newParamTypes = new ClassDesc[1];
+            newParamTypes[0] = null;
+            MethodTypeDesc newDesc = MethodTypeDesc.of(returnType, CD_int);
+            newDesc = newDesc.insertParameterTypes(0, newParamTypes);
+            fail("should fail with NPE");
+        } catch (NullPointerException ex) {
+            // good
+        }
     }
 
     private void badDropParametersTypes(ClassDesc returnType, String... paramDescTypes) {