jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
changeset 26625 cbeb3711fa94
parent 26482 cea1ab1c3ac7
child 26626 14bb608ce64e
equal deleted inserted replaced
26624:e6383382c939 26625:cbeb3711fa94
  2021      * @throws WrongMethodTypeException if the conversion cannot be made
  2021      * @throws WrongMethodTypeException if the conversion cannot be made
  2022      * @see MethodHandle#asType
  2022      * @see MethodHandle#asType
  2023      */
  2023      */
  2024     public static
  2024     public static
  2025     MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
  2025     MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
       
  2026         explicitCastArgumentsChecks(target, newType);
       
  2027         // use the asTypeCache when possible:
  2026         MethodType oldType = target.type();
  2028         MethodType oldType = target.type();
  2027         // use the asTypeCache when possible:
       
  2028         if (oldType == newType)  return target;
  2029         if (oldType == newType)  return target;
  2029         if (oldType.explicitCastEquivalentToAsType(newType)) {
  2030         if (oldType.explicitCastEquivalentToAsType(newType)) {
  2030             return target.asType(newType);
  2031             return target.asType(newType);
  2031         }
  2032         }
  2032         return MethodHandleImpl.makePairwiseConvert(target, newType, false);
  2033         return MethodHandleImpl.makePairwiseConvert(target, newType, false);
       
  2034     }
       
  2035 
       
  2036     private static void explicitCastArgumentsChecks(MethodHandle target, MethodType newType) {
       
  2037         if (target.type().parameterCount() != newType.parameterCount()) {
       
  2038             throw new WrongMethodTypeException("cannot explicitly cast " + target + " to " + newType);
       
  2039         }
  2033     }
  2040     }
  2034 
  2041 
  2035     /**
  2042     /**
  2036      * Produces a method handle which adapts the calling sequence of the
  2043      * Produces a method handle which adapts the calling sequence of the
  2037      * given method handle to a new type, by reordering the arguments.
  2044      * given method handle to a new type, by reordering the arguments.
  2477      */
  2484      */
  2478     public static
  2485     public static
  2479     MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
  2486     MethodHandle dropArguments(MethodHandle target, int pos, List<Class<?>> valueTypes) {
  2480         MethodType oldType = target.type();  // get NPE
  2487         MethodType oldType = target.type();  // get NPE
  2481         int dropped = dropArgumentChecks(oldType, pos, valueTypes);
  2488         int dropped = dropArgumentChecks(oldType, pos, valueTypes);
       
  2489         MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
  2482         if (dropped == 0)  return target;
  2490         if (dropped == 0)  return target;
  2483         BoundMethodHandle result = target.rebind();
  2491         BoundMethodHandle result = target.rebind();
  2484         LambdaForm lform = result.form;
  2492         LambdaForm lform = result.form;
  2485         if (USE_LAMBDA_FORM_EDITOR) {
  2493         if (USE_LAMBDA_FORM_EDITOR) {
  2486             int insertFormArg = 1 + pos;
  2494             int insertFormArg = 1 + pos;
  2488                 lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
  2496                 lform = lform.editor().addArgumentForm(insertFormArg++, BasicType.basicType(ptype));
  2489             }
  2497             }
  2490         } else {
  2498         } else {
  2491             lform = lform.addArguments(pos, valueTypes);
  2499             lform = lform.addArguments(pos, valueTypes);
  2492         }
  2500         }
  2493         MethodType newType = oldType.insertParameterTypes(pos, valueTypes);
       
  2494         result = result.copyWith(newType, lform);
  2501         result = result.copyWith(newType, lform);
  2495         return result;
  2502         return result;
  2496     }
  2503     }
  2497 
  2504 
  2498     private static int dropArgumentChecks(MethodType oldType, int pos, List<Class<?>> valueTypes) {
  2505     private static int dropArgumentChecks(MethodType oldType, int pos, List<Class<?>> valueTypes) {