Guards (Java SE 26 & JDK 26)
Description: declaration: module: jdk.dynalink, package: jdk.dynalink.linker.support, class: Guards
Keywords: jdk.dynalink.linker.support.Guards class

# Class Guards

java.lang.Object
jdk.dynalink.linker.support.Guards

public final class Guards extends Object
Utility methods for creating typical guards for `MethodHandles.guardWithTest(MethodHandle, MethodHandle, MethodHandle)` and for adjusting their method types.

Since:
  9

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static MethodHandle`
  `asType(MethodHandle test, MethodType type)`
  Takes a method handle intended to be used as a guard, and adapts it to the requested type, but returning a boolean.
  `static MethodHandle`
  `asType(LinkerServices linkerServices, MethodHandle test, MethodType type)`
  Takes a method handle intended to be used as a guard, and adapts it to the requested type, but returning a boolean.
  `static MethodHandle`
  `getClassGuard(Class<?> clazz)`
  Creates a guard method that tests its only argument for being of an exact particular class.
  `static MethodHandle`
  `getIdentityGuard(Object obj)`
  Creates a guard method that tests its only argument for being referentially identical to another object
  `static MethodHandle`
  `getInstanceOfGuard(Class<?> clazz)`
  Creates a guard method that tests its only argument for being an instance of a particular class.
  `static MethodHandle`
  `isArray(int pos, MethodType type)`
  Creates a method handle that returns true if the argument in the specified position is a Java array.
  `static MethodHandle`
  `isInstance(Class<?> clazz, int pos, MethodType type)`
  Creates a method handle with arguments of a specified type, but with boolean return value.
  `static MethodHandle`
  `isInstance(Class<?> clazz, MethodType type)`
  Creates a method handle with arguments of a specified type, but with boolean return value.
  `static MethodHandle`
  `isNotNull()`
  Returns a guard that tests whether the first argument is not null.
  `static MethodHandle`
  `isNull()`
  Returns a guard that tests whether the first argument is null.
  `static MethodHandle`
  `isOfClass(Class<?> clazz, MethodType type)`
  Creates a guard method handle with arguments of a specified type, but with boolean return value.

  ### Methods declared in class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`
  Modifier and Type
  Method
  Description
  `protected Object`
  `clone()`
  Creates and returns a copy of this object.
  `boolean`
  `equals(Object obj)`
  Indicates whether some other object is "equal to" this one.
  `protected void`
  `finalize()`
  Deprecated, for removal: This API element is subject to removal in a future version.
  Finalization is deprecated and subject to removal in a future release.
  `final Class<?>`
  `getClass()`
  Returns the runtime class of this `Object`.
  `int`
  `hashCode()`
  Returns a hash code value for this object.
  `final void`
  `notify()`
  Wakes up a single thread that is waiting on this object's monitor.
  `final void`
  `notifyAll()`
  Wakes up all threads that are waiting on this object's monitor.
  `String`
  `toString()`
  Returns a string representation of the object.
  `final void`
  `wait()`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted.
  `final void`
  `wait(long timeoutMillis)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.
  `final void`
  `wait(long timeoutMillis, int nanos)`
  Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed.

- ## Method Details

  - ### isOfClass

    public static MethodHandle isOfClass(Class<?> clazz, MethodType type)
    Creates a guard method handle with arguments of a specified type, but with boolean return value. When invoked, it returns true if the first argument is of the specified class (exactly of it, not a subclass). The rest of the arguments will be ignored.
    Parameters:
      `clazz` - the class of the first argument to test for
      `type` - the method type
    Returns:
      a method handle testing whether its first argument is of the specified class.

  - ### isInstance

    public static MethodHandle isInstance(Class<?> clazz, MethodType type)
    Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it returns true if the first argument is instance of the specified class or its subclass). The rest of the arguments will be ignored.
    Parameters:
      `clazz` - the class of the first argument to test for
      `type` - the method type
    Returns:
      a method handle testing whether its first argument is of the specified class or subclass.

  - ### isInstance

    public static MethodHandle isInstance(Class<?> clazz, int pos, MethodType type)
    Creates a method handle with arguments of a specified type, but with boolean return value. When invoked, it returns true if the n'th argument is instance of the specified class or its subclass). The rest of the arguments will be ignored.
    Parameters:
      `clazz` - the class of the first argument to test for
      `pos` - the position on the argument list to test
      `type` - the method type
    Returns:
      a method handle testing whether its first argument is of the specified class or subclass.

  - ### isArray

    public static MethodHandle isArray(int pos, MethodType type)
    Creates a method handle that returns true if the argument in the specified position is a Java array.
    Parameters:
      `pos` - the position in the argument lit
      `type` - the method type of the handle
    Returns:
      a method handle that returns true if the argument in the specified position is a Java array; the rest of the arguments are ignored.

  - ### asType

    public static MethodHandle asType(MethodHandle test, MethodType type)
    Takes a method handle intended to be used as a guard, and adapts it to the requested type, but returning a boolean. Applies `MethodHandle.asType(MethodType)` to convert types and uses `MethodHandles.dropArguments(MethodHandle, int, Class...)` to match the requested type arity.
    Parameters:
      `test` - the test method handle
      `type` - the type to adapt the method handle to
    Returns:
      the adapted method handle

  - ### asType

    public static MethodHandle asType(LinkerServices linkerServices, MethodHandle test, MethodType type)
    Takes a method handle intended to be used as a guard, and adapts it to the requested type, but returning a boolean. Applies `LinkerServices.asType(MethodHandle, MethodType)` to convert types and uses `MethodHandles.dropArguments(MethodHandle, int, Class...)` to match the requested type arity.
    Parameters:
      `linkerServices` - the linker services to use for type conversions
      `test` - the test method handle
      `type` - the type to adapt the method handle to
    Returns:
      the adapted method handle

  - ### getClassGuard

    public static MethodHandle getClassGuard(Class<?> clazz)
    Creates a guard method that tests its only argument for being of an exact particular class.
    Parameters:
      `clazz` - the class to test for.
    Returns:
      the desired guard method.

  - ### getInstanceOfGuard

    public static MethodHandle getInstanceOfGuard(Class<?> clazz)
    Creates a guard method that tests its only argument for being an instance of a particular class.
    Parameters:
      `clazz` - the class to test for.
    Returns:
      the desired guard method.

  - ### getIdentityGuard

    public static MethodHandle getIdentityGuard(Object obj)
    Creates a guard method that tests its only argument for being referentially identical to another object
    Parameters:
      `obj` - the object used as referential identity test
    Returns:
      the desired guard method.

  - ### isNull

    public static MethodHandle isNull()
    Returns a guard that tests whether the first argument is null.
    Returns:
      a guard that tests whether the first argument is null.

  - ### isNotNull

    public static MethodHandle isNotNull()
    Returns a guard that tests whether the first argument is not null.
    Returns:
      a guard that tests whether the first argument is not null.