jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
changeset 26625 cbeb3711fa94
parent 26482 cea1ab1c3ac7
child 26626 14bb608ce64e
--- a/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Sep 16 12:06:53 2014 +0200
+++ b/jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java	Tue Sep 16 18:05:01 2014 +0400
@@ -2023,8 +2023,9 @@
      */
     public static
     MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
+        explicitCastArgumentsChecks(target, newType);
+        // use the asTypeCache when possible:
         MethodType oldType = target.type();
-        // use the asTypeCache when possible:
         if (oldType == newType)  return target;
         if (oldType.explicitCastEquivalentToAsType(newType)) {
             return target.asType(newType);
@@ -2032,6 +2033,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.
@@ -2479,6 +2486,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;
@@ -2490,7 +2498,6 @@
         } else {
             lform = lform.addArguments(pos, valueTypes);
         }
-        MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
         result = result.copyWith(newType, lform);
         return result;
     }