Class Guards
java.lang.Object
jdk.dynalink.linker.support.Guards
Utility methods for creating typical guards for
MethodHandles.guardWithTest(MethodHandle, MethodHandle, MethodHandle)
and for adjusting their method types.- Since:
- 9
-
Method Summary
Modifier and TypeMethodDescriptionstatic MethodHandleasType(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 MethodHandleasType(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 MethodHandlegetClassGuard(Class<?> clazz) Creates a guard method that tests its only argument for being of an exact particular class.static MethodHandlegetIdentityGuard(Object obj) Creates a guard method that tests its only argument for being referentially identical to another objectstatic MethodHandlegetInstanceOfGuard(Class<?> clazz) Creates a guard method that tests its only argument for being an instance of a particular class.static MethodHandleisArray(int pos, MethodType type) Creates a method handle that returns true if the argument in the specified position is a Java array.static MethodHandleisInstance(Class<?> clazz, int pos, MethodType type) Creates a method handle with arguments of a specified type, but with boolean return value.static MethodHandleisInstance(Class<?> clazz, MethodType type) Creates a method handle with arguments of a specified type, but with boolean return value.static MethodHandleReturns a guard that tests whether the first argument is not null.static MethodHandleisNull()Returns a guard that tests whether the first argument is null.static MethodHandleisOfClass(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, waitModifier and TypeMethodDescriptionprotected Objectclone()Answers a new instance of the same class as the receiver, whose slots have been filled in with the values in the slots of the receiver.booleanCompares the argument to the receiver, and answers true if they represent the same object using a class specific comparison.protected voidfinalize()Deprecated, for removal: This API element is subject to removal in a future version.May cause performance issues, deadlocks and hangs.getClass()Answers the unique instance of java.lang.Class which represents the class of the receiver.inthashCode()Answers an integer hash code for the receiver.final voidnotify()Causes one thread which iswaiting on the receiver to be made ready to run.final voidCauses all threads which arewaiting on the receiver to be made ready to run.toString()Answers a string containing a concise, human-readable description of the receiver.final voidwait()Causes the thread which sent this message to be made not ready to run pending some change in the receiver (as indicated bynotifyornotifyAll).final voidwait(long time) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.final voidwait(long time, int frac) Causes the thread which sent this message to be made not ready to run either pending some change in the receiver (as indicated bynotifyornotifyAll) or the expiration of the timeout.
-
Method Details
-
isOfClass
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 fortype- the method type- Returns:
- a method handle testing whether its first argument is of the specified class.
-
isInstance
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 fortype- the method type- Returns:
- a method handle testing whether its first argument is of the specified class or subclass.
-
isInstance
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 forpos- the position on the argument list to testtype- the method type- Returns:
- a method handle testing whether its first argument is of the specified class or subclass.
-
isArray
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 littype- 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
Takes a method handle intended to be used as a guard, and adapts it to the requested type, but returning a boolean. AppliesMethodHandle.asType(MethodType)to convert types and usesMethodHandles.dropArguments(MethodHandle, int, Class...)to match the requested type arity.- Parameters:
test- the test method handletype- 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. AppliesLinkerServices.asType(MethodHandle, MethodType)to convert types and usesMethodHandles.dropArguments(MethodHandle, int, Class...)to match the requested type arity.- Parameters:
linkerServices- the linker services to use for type conversionstest- the test method handletype- the type to adapt the method handle to- Returns:
- the adapted method handle
-
getClassGuard
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
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
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
Returns a guard that tests whether the first argument is null.- Returns:
- a guard that tests whether the first argument is null.
-
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.
-