Share via


MethodHandles.FoldArguments Method

Definition

Overloads

FoldArguments(MethodHandle, Int32, MethodHandle)

Adapts a target method handle by pre-processing some of its arguments, starting at a given position, and then calling the target with the result of the pre-processing, inserted into the original sequence of arguments just before the folded arguments.

FoldArguments(MethodHandle, MethodHandle)

Adapts a target method handle by pre-processing some of its arguments, and then calling the target with the result of the pre-processing, inserted into the original sequence of arguments.

FoldArguments(MethodHandle, Int32, MethodHandle)

Adapts a target method handle by pre-processing some of its arguments, starting at a given position, and then calling the target with the result of the pre-processing, inserted into the original sequence of arguments just before the folded arguments.

[Android.Runtime.Register("foldArguments", "(Ljava/lang/invoke/MethodHandle;ILjava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=33)]
public static Java.Lang.Invoke.MethodHandle? FoldArguments (Java.Lang.Invoke.MethodHandle? target, int pos, Java.Lang.Invoke.MethodHandle? combiner);
[<Android.Runtime.Register("foldArguments", "(Ljava/lang/invoke/MethodHandle;ILjava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=33)>]
static member FoldArguments : Java.Lang.Invoke.MethodHandle * int * Java.Lang.Invoke.MethodHandle -> Java.Lang.Invoke.MethodHandle

Parameters

target
MethodHandle

the method handle to invoke after arguments are combined

pos
Int32

the position at which to start folding and at which to insert the folding result; if this is 0, the effect is the same as for #foldArguments(MethodHandle, MethodHandle).

combiner
MethodHandle

method handle to call initially on the incoming arguments

Returns

method handle which incorporates the specified argument folding logic

Attributes

Remarks

Adapts a target method handle by pre-processing some of its arguments, starting at a given position, and then calling the target with the result of the pre-processing, inserted into the original sequence of arguments just before the folded arguments.

This method is closely related to #foldArguments(MethodHandle, MethodHandle), but allows to control the position in the parameter list at which folding takes place. The argument controlling this, pos, is a zero-based index. The aforementioned method #foldArguments(MethodHandle, MethodHandle) assumes position 0.

Added in 9.

Java documentation for java.lang.invoke.MethodHandles.foldArguments(java.lang.invoke.MethodHandle, int, java.lang.invoke.MethodHandle).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to

FoldArguments(MethodHandle, MethodHandle)

Adapts a target method handle by pre-processing some of its arguments, and then calling the target with the result of the pre-processing, inserted into the original sequence of arguments.

[Android.Runtime.Register("foldArguments", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=26)]
public static Java.Lang.Invoke.MethodHandle? FoldArguments (Java.Lang.Invoke.MethodHandle? target, Java.Lang.Invoke.MethodHandle? combiner);
[<Android.Runtime.Register("foldArguments", "(Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodHandle;)Ljava/lang/invoke/MethodHandle;", "", ApiSince=26)>]
static member FoldArguments : Java.Lang.Invoke.MethodHandle * Java.Lang.Invoke.MethodHandle -> Java.Lang.Invoke.MethodHandle

Parameters

target
MethodHandle

the method handle to invoke after arguments are combined

combiner
MethodHandle

method handle to call initially on the incoming arguments

Returns

method handle which incorporates the specified argument folding logic

Attributes

Remarks

Adapts a target method handle by pre-processing some of its arguments, and then calling the target with the result of the pre-processing, inserted into the original sequence of arguments.

The pre-processing is performed by combiner, a second method handle. Of the arguments passed to the adapter, the first N arguments are copied to the combiner, which is then called. (Here, N is defined as the parameter count of the combiner.) After this, control passes to the target, with any result from the combiner inserted before the original N incoming arguments.

If the combiner returns a value, the first parameter type of the target must be identical with the return type of the combiner, and the next N parameter types of the target must exactly match the parameters of the combiner.

If the combiner has a void return, no result will be inserted, and the first N parameter types of the target must exactly match the parameters of the combiner.

The resulting adapter is the same type as the target, except that the first parameter type is dropped, if it corresponds to the result of the combiner.

(Note that #dropArguments(MethodHandle,int,List) dropArguments can be used to remove any arguments that either the combiner or the target does not wish to receive. If some of the incoming arguments are destined only for the combiner, consider using MethodHandle#asCollector asCollector instead, since those arguments will not need to be live on the stack on entry to the target.)

<b>Example:</b> <blockquote>

{@code
            import static java.lang.invoke.MethodHandles.*;
            import static java.lang.invoke.MethodType.*;
            ...
            MethodHandle trace = publicLookup().findVirtual(java.io.PrintStream.class,
              "println", methodType(void.class, String.class))
                .bindTo(System.out);
            MethodHandle cat = lookup().findVirtual(String.class,
              "concat", methodType(String.class, String.class));
            assertEquals("boojum", (String) cat.invokeExact("boo", "jum"));
            MethodHandle catTrace = foldArguments(cat, trace);
            // also prints "boo":
            assertEquals("boojum", (String) catTrace.invokeExact("boo", "jum"));
            }

</blockquote>

Here is pseudocode for the resulting adapter. In the code, T represents the result type of the target and resulting adapter. V/v represent the type and value of the parameter and argument of target that precedes the folding position; V also is the result type of the combiner. A/a denote the types and values of the N parameters and arguments at the folding position. B/b represent the types and values of the target parameters and arguments that follow the folded parameters and arguments. <blockquote>

{@code
            // there are N arguments in A...
            T target(V, A[N]..., B...);
            V combiner(A...);
            T adapter(A... a, B... b) {
              V v = combiner(a...);
              return target(v, a..., b...);
            }
            // and if the combiner has a void return:
            T target2(A[N]..., B...);
            void combiner2(A...);
            T adapter2(A... a, B... b) {
              combiner2(a...);
              return target2(a..., b...);
            }
            }

</blockquote>

<em>Note:</em> The resulting adapter is never a MethodHandle#asVarargsCollector variable-arity method handle, even if the original target method handle was.

Java documentation for java.lang.invoke.MethodHandles.foldArguments(java.lang.invoke.MethodHandle, java.lang.invoke.MethodHandle).

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to