2007 * widening and/or narrowing.)
2008 * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, an unboxing
2009 * conversion will be applied at runtime, possibly followed
2010 * by a Java casting conversion (JLS 5.5) on the primitive value,
2011 * possibly followed by a conversion from byte to boolean by testing
2012 * the low-order bit.
2013 * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive,
2014 * and if the reference is null at runtime, a zero value is introduced.
2015 * </ul>
2016 * @param target the method handle to invoke after arguments are retyped
2017 * @param newType the expected type of the new method handle
2018 * @return a method handle which delegates to the target after performing
2019 * any necessary argument conversions, and arranges for any
2020 * necessary return value conversions
2021 * @throws NullPointerException if either argument is null
2022 * @throws WrongMethodTypeException if the conversion cannot be made
2023 * @see MethodHandle#asType
2024 */
2025 public static
2026 MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
2027 if (!target.type().isCastableTo(newType)) {
2028 throw new WrongMethodTypeException("cannot explicitly cast "+target+" to "+newType);
2029 }
2030 return MethodHandleImpl.makePairwiseConvert(target, newType, false);
2031 }
2032
2033 /**
2034 * Produces a method handle which adapts the calling sequence of the
2035 * given method handle to a new type, by reordering the arguments.
2036 * The resulting method handle is guaranteed to report a type
2037 * which is equal to the desired new type.
2038 * <p>
2039 * The given array controls the reordering.
2040 * Call {@code #I} the number of incoming parameters (the value
2041 * {@code newType.parameterCount()}, and call {@code #O} the number
2042 * of outgoing parameters (the value {@code target.type().parameterCount()}).
2043 * Then the length of the reordering array must be {@code #O},
2044 * and each element must be a non-negative number less than {@code #I}.
2045 * For every {@code N} less than {@code #O}, the {@code N}-th
2046 * outgoing argument will be taken from the {@code I}-th incoming
2047 * argument, where {@code I} is {@code reorder[N]}.
2048 * <p>
|
2007 * widening and/or narrowing.)
2008 * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, an unboxing
2009 * conversion will be applied at runtime, possibly followed
2010 * by a Java casting conversion (JLS 5.5) on the primitive value,
2011 * possibly followed by a conversion from byte to boolean by testing
2012 * the low-order bit.
2013 * <li>If <em>T0</em> is a reference and <em>T1</em> a primitive,
2014 * and if the reference is null at runtime, a zero value is introduced.
2015 * </ul>
2016 * @param target the method handle to invoke after arguments are retyped
2017 * @param newType the expected type of the new method handle
2018 * @return a method handle which delegates to the target after performing
2019 * any necessary argument conversions, and arranges for any
2020 * necessary return value conversions
2021 * @throws NullPointerException if either argument is null
2022 * @throws WrongMethodTypeException if the conversion cannot be made
2023 * @see MethodHandle#asType
2024 */
2025 public static
2026 MethodHandle explicitCastArguments(MethodHandle target, MethodType newType) {
2027 MethodType oldType = target.type();
2028 // use the asTypeCache when possible:
2029 if (oldType == newType) return target;
2030 if (oldType.explicitCastEquivalentToAsType(newType)) {
2031 return target.asType(newType);
2032 }
2033 return MethodHandleImpl.makePairwiseConvert(target, newType, false);
2034 }
2035
2036 /**
2037 * Produces a method handle which adapts the calling sequence of the
2038 * given method handle to a new type, by reordering the arguments.
2039 * The resulting method handle is guaranteed to report a type
2040 * which is equal to the desired new type.
2041 * <p>
2042 * The given array controls the reordering.
2043 * Call {@code #I} the number of incoming parameters (the value
2044 * {@code newType.parameterCount()}, and call {@code #O} the number
2045 * of outgoing parameters (the value {@code target.type().parameterCount()}).
2046 * Then the length of the reordering array must be {@code #O},
2047 * and each element must be a non-negative number less than {@code #I}.
2048 * For every {@code N} less than {@code #O}, the {@code N}-th
2049 * outgoing argument will be taken from the {@code I}-th incoming
2050 * argument, where {@code I} is {@code reorder[N]}.
2051 * <p>
|