MethodHandles.GuardWithTest(MethodHandle, MethodHandle, MethodHandle) Method

Definition

Makes a method handle which adapts a target method handle, by guarding it with a test, a boolean-valued method handle.

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

Parameters

test
MethodHandle

method handle used for test, must return boolean

target
MethodHandle

method handle to call if test passes

fallback
MethodHandle

method handle to call if test fails

Returns

method handle which incorporates the specified if/then/else logic

Attributes

Remarks

Makes a method handle which adapts a target method handle, by guarding it with a test, a boolean-valued method handle. If the guard fails, a fallback handle is called instead. All three method handles must have the same corresponding argument and return types, except that the return type of the test must be boolean, and the test is allowed to have fewer arguments than the other two method handles.

Here is pseudocode for the resulting adapter: <blockquote>

{@code
            boolean test(A...);
            T target(A...,B...);
            T fallback(A...,B...);
            T adapter(A... a,B... b) {
              if (test(a...))
                return target(a..., b...);
              else
                return fallback(a..., b...);
            }
            }

</blockquote> Note that the test arguments (a... in the pseudocode) cannot be modified by execution of the test, and so are passed unchanged from the caller to the target or fallback as appropriate.

Java documentation for java.lang.invoke.MethodHandles.guardWithTest(java.lang.invoke.MethodHandle, 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