8058291: Missing some checks during parameter validation
Reviewed-by: jrose
diff --git a/src/share/classes/java/lang/invoke/MethodHandleImpl.java b/src/share/classes/java/lang/invoke/MethodHandleImpl.java
index 026b2cd..90283fd 100644
--- a/src/share/classes/java/lang/invoke/MethodHandleImpl.java
+++ b/src/share/classes/java/lang/invoke/MethodHandleImpl.java
@@ -188,7 +188,6 @@
     static MethodHandle makePairwiseConvert(MethodHandle target, MethodType srcType,
                                             boolean strict, boolean monobox) {
         MethodType dstType = target.type();
-        assert(dstType.parameterCount() == target.type().parameterCount());
         if (srcType == dstType)
             return target;
         if (USE_LAMBDA_FORM_EDITOR) {
@@ -265,6 +264,7 @@
 
     static MethodHandle makePairwiseConvertIndirect(MethodHandle target, MethodType srcType,
                                                     boolean strict, boolean monobox) {
+        assert(target.type().parameterCount() == srcType.parameterCount());
         // Calculate extra arguments (temporaries) required in the names array.
         Object[] convSpecs = computeValueConversions(srcType, target.type(), strict, monobox);
         final int INARG_COUNT = srcType.parameterCount();
diff --git a/src/share/classes/java/lang/invoke/MethodHandles.java b/src/share/classes/java/lang/invoke/MethodHandles.java
index 650fc34..9ba1be9 100644
--- a/src/share/classes/java/lang/invoke/MethodHandles.java
+++ b/src/share/classes/java/lang/invoke/MethodHandles.java
@@ -2024,8 +2024,9 @@
      */
     public static
     MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
-        MethodType oldType = target.type();
+        explicitCastArgumentsChecks(target, newType);
         // use the asTypeCache when possible:
+        MethodType oldType = target.type();
         if (oldType == newType)  return target;
         if (oldType.explicitCastEquivalentToAsType(newType)) {
             return target.asType(newType);
@@ -2033,6 +2034,12 @@
         return MethodHandleImpl.makePairwiseConvert(target, newType, false);
     }
 
+    private static void explicitCastArgumentsChecks(MethodHandle target, MethodType newType) {
+        if (target.type().parameterCount() != newType.parameterCount()) {
+            throw new WrongMethodTypeException("cannot explicitly cast " + target + " to " + newType);
+        }
+    }
+
     /**
      * Produces a method handle which adapts the calling sequence of the
      * given method handle to a new type, by reordering the arguments.
@@ -2480,6 +2487,7 @@
     MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
         MethodType oldType = target.type();  // get NPE
         int dropped = dropArgumentChecks(oldType, pos, valueTypes);
+        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
         if (dropped == 0)  return target;
         BoundMethodHandle result = target.rebind();
         LambdaForm lform = result.form;
@@ -2491,7 +2499,6 @@
         } else {
             lform = lform.addArguments(pos, valueTypes);
         }
-        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
         result = result.copyWith(newType, lform);
         return result;
     }
diff --git a/src/share/classes/java/lang/invoke/MethodType.java b/src/share/classes/java/lang/invoke/MethodType.java
index c23953c..e96b70e 100644
--- a/src/share/classes/java/lang/invoke/MethodType.java
+++ b/src/share/classes/java/lang/invoke/MethodType.java
@@ -871,9 +871,7 @@
         if (dstTypes == srcTypes) {
             return true;
         }
-        if (dstTypes.length != srcTypes.length) {
-            return false;
-        }
+        assert(dstTypes.length == srcTypes.length);
         for (int i = 0; i < dstTypes.length; i++) {
             if (!explicitCastEquivalentToAsType(srcTypes[i], dstTypes[i])) {
                 return false;