MethodHandles.FilterReturnValue(MethodHandle, MethodHandle) Method

Definition

Adapts a target method handle by post-processing its return value (if any) with a filter (another method handle).

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

Parameters

target
MethodHandle

the method handle to invoke before filtering the return value

filter
MethodHandle

method handle to call on the return value

Returns

method handle which incorporates the specified return value filtering logic

Attributes

Remarks

Adapts a target method handle by post-processing its return value (if any) with a filter (another method handle). The result of the filter is returned from the adapter.

If the target returns a value, the filter must accept that value as its only argument. If the target returns void, the filter must accept no arguments.

The return type of the filter replaces the return type of the target in the resulting adapted method handle. The argument type of the filter (if any) must be identical to the return type of the target.

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

{@code
            import static java.lang.invoke.MethodHandles.*;
            import static java.lang.invoke.MethodType.*;
            ...
            MethodHandle cat = lookup().findVirtual(String.class,
              "concat", methodType(String.class, String.class));
            MethodHandle length = lookup().findVirtual(String.class,
              "length", methodType(int.class));
            System.out.println((String) cat.invokeExact("x", "y")); // xy
            MethodHandle f0 = filterReturnValue(cat, length);
            System.out.println((int) f0.invokeExact("x", "y")); // 2
            }

</blockquote>

Here is pseudocode for the resulting adapter. In the code, T/t represent the result type and value of the target; V, the result type of the filter; and A/a, the types and values of the parameters and arguments of the target as well as the resulting adapter. <blockquote>

{@code
            T target(A...);
            V filter(T);
            V adapter(A... a) {
              T t = target(a...);
              return filter(t);
            }
            // and if the target has a void return:
            void target2(A...);
            V filter2();
            V adapter2(A... a) {
              target2(a...);
              return filter2();
            }
            // and if the filter has a void return:
            T target3(A...);
            void filter3(V);
            void adapter3(A... a) {
              T t = target3(a...);
              filter3(t);
            }
            }

</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.filterReturnValue(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