jdk/src/java.base/share/classes/java/lang/invoke/MethodHandles.java
changeset 36112 e71e97060bf5
parent 35772 044f660251cc
child 36114 a5ed9456c9be
equal deleted inserted replaced
36111:00ec08e93b0c 36112:e71e97060bf5
  2137      * An incoming argument will be duplicated if its index appears
  2137      * An incoming argument will be duplicated if its index appears
  2138      * more than once in the array, and an incoming argument will be dropped
  2138      * more than once in the array, and an incoming argument will be dropped
  2139      * if its index does not appear in the array.
  2139      * if its index does not appear in the array.
  2140      * As in the case of {@link #dropArguments(MethodHandle,int,List) dropArguments},
  2140      * As in the case of {@link #dropArguments(MethodHandle,int,List) dropArguments},
  2141      * incoming arguments which are not mentioned in the reordering array
  2141      * incoming arguments which are not mentioned in the reordering array
  2142      * are may be any type, as determined only by {@code newType}.
  2142      * may be of any type, as determined only by {@code newType}.
  2143      * <blockquote><pre>{@code
  2143      * <blockquote><pre>{@code
  2144 import static java.lang.invoke.MethodHandles.*;
  2144 import static java.lang.invoke.MethodHandles.*;
  2145 import static java.lang.invoke.MethodType.*;
  2145 import static java.lang.invoke.MethodType.*;
  2146 ...
  2146 ...
  2147 MethodType intfn1 = methodType(int.class, int.class);
  2147 MethodType intfn1 = methodType(int.class, int.class);
  2155 assert(add.type().equals(intfn2));
  2155 assert(add.type().equals(intfn2));
  2156 MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
  2156 MethodHandle twice = permuteArguments(add, intfn1, 0, 0);
  2157 assert(twice.type().equals(intfn1));
  2157 assert(twice.type().equals(intfn1));
  2158 assert((int)twice.invokeExact(21) == 42);
  2158 assert((int)twice.invokeExact(21) == 42);
  2159      * }</pre></blockquote>
  2159      * }</pre></blockquote>
       
  2160      * <p>
       
  2161      * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
       
  2162      * variable-arity method handle}, even if the original target method handle was.
  2160      * @param target the method handle to invoke after arguments are reordered
  2163      * @param target the method handle to invoke after arguments are reordered
  2161      * @param newType the expected type of the new method handle
  2164      * @param newType the expected type of the new method handle
  2162      * @param reorder an index array which controls the reordering
  2165      * @param reorder an index array which controls the reordering
  2163      * @return a method handle which delegates to the target after it
  2166      * @return a method handle which delegates to the target after it
  2164      *           drops unused arguments and moves and/or duplicates the other arguments
  2167      *           drops unused arguments and moves and/or duplicates the other arguments
  2419      * <p>
  2422      * <p>
  2420      * The {@code pos} argument selects which parameters are to be bound.
  2423      * The {@code pos} argument selects which parameters are to be bound.
  2421      * It may range between zero and <i>N-L</i> (inclusively),
  2424      * It may range between zero and <i>N-L</i> (inclusively),
  2422      * where <i>N</i> is the arity of the target method handle
  2425      * where <i>N</i> is the arity of the target method handle
  2423      * and <i>L</i> is the length of the values array.
  2426      * and <i>L</i> is the length of the values array.
       
  2427      * <p>
       
  2428      * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
       
  2429      * variable-arity method handle}, even if the original target method handle was.
  2424      * @param target the method handle to invoke after the argument is inserted
  2430      * @param target the method handle to invoke after the argument is inserted
  2425      * @param pos where to insert the argument (zero for the first)
  2431      * @param pos where to insert the argument (zero for the first)
  2426      * @param values the series of arguments to insert
  2432      * @param values the series of arguments to insert
  2427      * @return a method handle which inserts an additional argument,
  2433      * @return a method handle which inserts an additional argument,
  2428      *         before calling the original method handle
  2434      *         before calling the original method handle
  2645      * A[i] filter[i](V[i]);
  2651      * A[i] filter[i](V[i]);
  2646      * T adapter(P... p, V[i]... v[i], B... b) {
  2652      * T adapter(P... p, V[i]... v[i], B... b) {
  2647      *   return target(p..., f[i](v[i])..., b...);
  2653      *   return target(p..., f[i](v[i])..., b...);
  2648      * }
  2654      * }
  2649      * }</pre></blockquote>
  2655      * }</pre></blockquote>
       
  2656      * <p>
       
  2657      * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
       
  2658      * variable-arity method handle}, even if the original target method handle was.
  2650      *
  2659      *
  2651      * @param target the method handle to invoke after arguments are filtered
  2660      * @param target the method handle to invoke after arguments are filtered
  2652      * @param pos the position of the first argument to filter
  2661      * @param pos the position of the first argument to filter
  2653      * @param filters method handles to call initially on filtered arguments
  2662      * @param filters method handles to call initially on filtered arguments
  2654      * @return method handle which incorporates the specified argument filtering logic
  2663      * @return method handle which incorporates the specified argument filtering logic
  2789      * is equivalent to {@code filterReturnValue(coll, mh)}.
  2798      * is equivalent to {@code filterReturnValue(coll, mh)}.
  2790      * If the filter method handle {@code coll} consumes one argument and produces
  2799      * If the filter method handle {@code coll} consumes one argument and produces
  2791      * a non-void result, then {@code collectArguments(mh, N, coll)}
  2800      * a non-void result, then {@code collectArguments(mh, N, coll)}
  2792      * is equivalent to {@code filterArguments(mh, N, coll)}.
  2801      * is equivalent to {@code filterArguments(mh, N, coll)}.
  2793      * Other equivalences are possible but would require argument permutation.
  2802      * Other equivalences are possible but would require argument permutation.
       
  2803      * <p>
       
  2804      * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
       
  2805      * variable-arity method handle}, even if the original target method handle was.
  2794      *
  2806      *
  2795      * @param target the method handle to invoke after filtering the subsequence of arguments
  2807      * @param target the method handle to invoke after filtering the subsequence of arguments
  2796      * @param pos the position of the first adapter argument to pass to the filter,
  2808      * @param pos the position of the first adapter argument to pass to the filter,
  2797      *            and/or the target argument which receives the result of the filter
  2809      *            and/or the target argument which receives the result of the filter
  2798      * @param filter method handle to call on the subsequence of arguments
  2810      * @param filter method handle to call on the subsequence of arguments
  2885      * void adapter3(A... a) {
  2897      * void adapter3(A... a) {
  2886      *   V v = target3(a...);
  2898      *   V v = target3(a...);
  2887      *   filter3(v);
  2899      *   filter3(v);
  2888      * }
  2900      * }
  2889      * }</pre></blockquote>
  2901      * }</pre></blockquote>
       
  2902      * <p>
       
  2903      * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
       
  2904      * variable-arity method handle}, even if the original target method handle was.
  2890      * @param target the method handle to invoke before filtering the return value
  2905      * @param target the method handle to invoke before filtering the return value
  2891      * @param filter method handle to call on the return value
  2906      * @param filter method handle to call on the return value
  2892      * @return method handle which incorporates the specified return value filtering logic
  2907      * @return method handle which incorporates the specified return value filtering logic
  2893      * @throws NullPointerException if either argument is null
  2908      * @throws NullPointerException if either argument is null
  2894      * @throws IllegalArgumentException if the argument list of {@code filter}
  2909      * @throws IllegalArgumentException if the argument list of {@code filter}
  2979      * T adapter2(A... a, B... b) {
  2994      * T adapter2(A... a, B... b) {
  2980      *   combiner2(a...);
  2995      *   combiner2(a...);
  2981      *   return target2(a..., b...);
  2996      *   return target2(a..., b...);
  2982      * }
  2997      * }
  2983      * }</pre></blockquote>
  2998      * }</pre></blockquote>
       
  2999      * <p>
       
  3000      * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
       
  3001      * variable-arity method handle}, even if the original target method handle was.
  2984      * @param target the method handle to invoke after arguments are combined
  3002      * @param target the method handle to invoke after arguments are combined
  2985      * @param combiner method handle to call initially on the incoming arguments
  3003      * @param combiner method handle to call initially on the incoming arguments
  2986      * @return method handle which incorporates the specified argument folding logic
  3004      * @return method handle which incorporates the specified argument folding logic
  2987      * @throws NullPointerException if either argument is null
  3005      * @throws NullPointerException if either argument is null
  2988      * @throws IllegalArgumentException if {@code combiner}'s return type
  3006      * @throws IllegalArgumentException if {@code combiner}'s return type
  3826      * a {@code null}, zero, or {@code false} value of the required type is supplied as a placeholder.
  3844      * a {@code null}, zero, or {@code false} value of the required type is supplied as a placeholder.
  3827      * The second argument is not present if the {@code target} handle has a {@code void} return type.
  3845      * The second argument is not present if the {@code target} handle has a {@code void} return type.
  3828      * (Note that, except for argument type conversions, combinators represent {@code void} values in parameter lists
  3846      * (Note that, except for argument type conversions, combinators represent {@code void} values in parameter lists
  3829      * by omitting the corresponding paradoxical arguments, not by inserting {@code null} or zero values.)
  3847      * by omitting the corresponding paradoxical arguments, not by inserting {@code null} or zero values.)
  3830      * <p>
  3848      * <p>
  3831      * The {@code target} and {@code cleanup} handles' return types must be the same. Their parameter type lists also
  3849      * The {@code target} and {@code cleanup} handles must have the same corresponding argument and return types, except
  3832      * must be the same, but the {@code cleanup} handle must accept one or two more leading parameters:<ul>
  3850      * that the {@code cleanup} handle may omit trailing arguments. Also, the {@code cleanup} handle must have one or
       
  3851      * two extra leading parameters:<ul>
  3833      * <li>a {@code Throwable}, which will carry the exception thrown by the {@code target} handle (if any); and
  3852      * <li>a {@code Throwable}, which will carry the exception thrown by the {@code target} handle (if any); and
  3834      * <li>a parameter of the same type as the return type of both {@code target} and {@code cleanup}, which will carry
  3853      * <li>a parameter of the same type as the return type of both {@code target} and {@code cleanup}, which will carry
  3835      * the result from the execution of the {@code target} handle.
  3854      * the result from the execution of the {@code target} handle.
  3836      * This parameter is not present if the {@code target} returns {@code void}.
  3855      * This parameter is not present if the {@code target} returns {@code void}.
  3837      * </ul>
  3856      * </ul>
  3947      * T adapter2(Z... z, A... a, B... b) {
  3966      * T adapter2(Z... z, A... a, B... b) {
  3948      *   combiner2(a...);
  3967      *   combiner2(a...);
  3949      *   return target2(z..., a..., b...);
  3968      *   return target2(z..., a..., b...);
  3950      * }
  3969      * }
  3951      * }</pre></blockquote>
  3970      * }</pre></blockquote>
       
  3971      * <p>
       
  3972      * <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector
       
  3973      * variable-arity method handle}, even if the original target method handle was.
  3952      *
  3974      *
  3953      * @param target the method handle to invoke after arguments are combined
  3975      * @param target the method handle to invoke after arguments are combined
  3954      * @param pos the position at which to start folding and at which to insert the folding result; if this is {@code
  3976      * @param pos the position at which to start folding and at which to insert the folding result; if this is {@code
  3955      *            0}, the effect is the same as for {@link #foldArguments(MethodHandle, MethodHandle)}.
  3977      *            0}, the effect is the same as for {@link #foldArguments(MethodHandle, MethodHandle)}.
  3956      * @param combiner method handle to call initially on the incoming arguments
  3978      * @param combiner method handle to call initially on the incoming arguments