java.lang.Object
jdk.dynalink.linker.GuardedInvocation
Represents a conditionally valid method handle. Usually produced as a return
value of
GuardingDynamicLinker.getGuardedInvocation(LinkRequest, LinkerServices)
and
GuardingTypeConverterFactory.convertToType(Class, Class, Supplier)
.
It is an immutable tuple of an invocation method handle, a guard method
handle that defines the applicability of the invocation handle, zero or more
switch points that can be used for external invalidation of the invocation
handle, and an exception type that if thrown during an invocation of the
method handle also invalidates it. The invocation handle is suitable for
invocation if the guard handle returns true for its arguments, and as long
as any of the switch points are not invalidated, and as long as it does not
throw an exception of the designated type. The guard, the switch points, and
the exception type are all optional (a guarded invocation having none of them
is unconditionally valid).-
Constructor Summary
ConstructorDescriptionGuardedInvocation
(MethodHandle invocation) Creates a new unconditional guarded invocation.GuardedInvocation
(MethodHandle invocation, MethodHandle guard) Creates a new guarded invocation, with a guard method handle.GuardedInvocation
(MethodHandle invocation, MethodHandle guard, SwitchPoint switchPoint) Creates a new guarded invocation, with both a guard method handle and a switch point that can be used to invalidate it.GuardedInvocation
(MethodHandle invocation, MethodHandle guard, SwitchPoint[] switchPoints, Class<? extends Throwable> exception) Creates a new guarded invocation, with a guard method handle, any number of switch points that can be used to invalidate it, and an exception that if thrown when invoked also invalidates it.GuardedInvocation
(MethodHandle invocation, MethodHandle guard, SwitchPoint switchPoint, Class<? extends Throwable> exception) Creates a new guarded invocation, with a guard method handle, a switch point that can be used to invalidate it, and an exception that if thrown when invoked also invalidates it.GuardedInvocation
(MethodHandle invocation, SwitchPoint switchPoint) Creates a new guarded invocation that can be invalidated by a switch point. -
Method Summary
Modifier and TypeMethodDescriptionaddSwitchPoint
(SwitchPoint newSwitchPoint) Create a new guarded invocation with an added switch point.asType
(MethodType newType) Changes the type of the invocation, as ifMethodHandle.asType(MethodType)
was applied to its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for the guard).asType
(CallSiteDescriptor desc) Changes the type of the invocation, as ifMethodHandle.asType(MethodType)
was applied to its invocation and its guard, if it has one (with return type changed to boolean for guard).asType
(LinkerServices linkerServices, MethodType newType) Changes the type of the invocation, as ifLinkerServices.asType(MethodHandle, MethodType)
was applied to its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for the guard).asTypeSafeReturn
(LinkerServices linkerServices, MethodType newType) Changes the type of the invocation, as ifLinkerServices.asTypeLosslessReturn(MethodHandle, MethodType)
was applied to its invocation andLinkerServices.asType(MethodHandle, MethodType)
applied to its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for the guard).compose
(MethodHandle fallback) Composes the invocation, guard, switch points, and the exception into a composite method handle that knows how to fall back when the guard fails or the invocation is invalidated.compose
(MethodHandle guardFallback, MethodHandle switchpointFallback, MethodHandle catchFallback) Composes the invocation, guard, switch points, and the exception into a composite method handle that knows how to fall back when the guard fails or the invocation is invalidated.dropArguments
(int pos, Class<?>... valueTypes) Makes an invocation that drops arguments in both the invocation and the guard (if it exists and has at leastpos
parameters) withMethodHandles.dropArguments(MethodHandle, int, Class...)
.dropArguments
(int pos, List<Class<?>> valueTypes) Makes an invocation that drops arguments in both the invocation and the guard (if it exists and has at leastpos
parameters) withMethodHandles.dropArguments(MethodHandle, int, List)
.filterArguments
(int pos, MethodHandle... filters) Applies argument filters to both the invocation and the guard (if it exists and has at leastpos + 1
parameters) withMethodHandles.filterArguments(MethodHandle, int, MethodHandle...)
.Returns the exception type that if thrown by the invocation should invalidate the linkage of this guarded invocation.getGuard()
Returns the guard method handle.Returns the invocation method handle.Returns the switch points that can be used to invalidate the linkage of this invocation handle.boolean
Returns true if and only if this guarded invocation has at least one invalidated switch point.replaceMethods
(MethodHandle newInvocation, MethodHandle newGuard) Creates a new guarded invocation with different methods, preserving the switch point.
-
Constructor Details
-
GuardedInvocation
Creates a new unconditional guarded invocation. It is unconditional as it has no invalidations.- Parameters:
invocation
- the method handle representing the invocation. Must not be null.- Throws:
NullPointerException
- if invocation is null.
-
GuardedInvocation
Creates a new guarded invocation, with a guard method handle.- Parameters:
invocation
- the method handle representing the invocation. Must not be null.guard
- the method handle representing the guard. Must have be compatible with theinvocation
handle as perMethodHandles.guardWithTest(MethodHandle, MethodHandle, MethodHandle)
. For some useful guards, check out theGuards
class. It can be null to represent an unconditional invocation.- Throws:
NullPointerException
- if invocation is null.
-
GuardedInvocation
Creates a new guarded invocation that can be invalidated by a switch point.- Parameters:
invocation
- the method handle representing the invocation. Must not be null.switchPoint
- the optional switch point that can be used to invalidate this linkage. It can be null. If it is null, this represents an unconditional invocation.- Throws:
NullPointerException
- if invocation is null.
-
GuardedInvocation
Creates a new guarded invocation, with both a guard method handle and a switch point that can be used to invalidate it.- Parameters:
invocation
- the method handle representing the invocation. Must not be null.guard
- the method handle representing the guard. Must have be compatible with theinvocation
handle as perMethodHandles.guardWithTest(MethodHandle, MethodHandle, MethodHandle)
. For some useful guards, check out theGuards
class. It can be null. If both it and the switch point are null, this represents an unconditional invocation.switchPoint
- the optional switch point that can be used to invalidate this linkage.- Throws:
NullPointerException
- if invocation is null.
-
GuardedInvocation
public GuardedInvocation(MethodHandle invocation, MethodHandle guard, SwitchPoint switchPoint, Class<? extends Throwable> exception) Creates a new guarded invocation, with a guard method handle, a switch point that can be used to invalidate it, and an exception that if thrown when invoked also invalidates it.- Parameters:
invocation
- the method handle representing the invocation. Must not be null.guard
- the method handle representing the guard. Must have be compatible with theinvocation
handle as perMethodHandles.guardWithTest(MethodHandle, MethodHandle, MethodHandle)
. For some useful guards, check out theGuards
class. It can be null. If it and the switch point and the exception are all null, this represents an unconditional invocation.switchPoint
- the optional switch point that can be used to invalidate this linkage.exception
- the optional exception type that is when thrown by the invocation also invalidates it.- Throws:
NullPointerException
- if invocation is null.
-
GuardedInvocation
public GuardedInvocation(MethodHandle invocation, MethodHandle guard, SwitchPoint[] switchPoints, Class<? extends Throwable> exception) Creates a new guarded invocation, with a guard method handle, any number of switch points that can be used to invalidate it, and an exception that if thrown when invoked also invalidates it.- Parameters:
invocation
- the method handle representing the invocation. Must not be null.guard
- the method handle representing the guard. Must have be compatible with theinvocation
handle as perMethodHandles.guardWithTest(MethodHandle, MethodHandle, MethodHandle)
. For some useful guards, check out theGuards
class. It can be null. If it and the exception are both null, and no switch points were specified, this represents an unconditional invocation.switchPoints
- optional switch points that can be used to invalidate this linkage.exception
- the optional exception type that is when thrown by the invocation also invalidates it.- Throws:
NullPointerException
- if invocation is null.
-
-
Method Details
-
getInvocation
Returns the invocation method handle.- Returns:
- the invocation method handle. It will never be null.
-
getGuard
Returns the guard method handle.- Returns:
- the guard method handle. Can be null.
-
getSwitchPoints
Returns the switch points that can be used to invalidate the linkage of this invocation handle.- Returns:
- the switch points that can be used to invalidate the linkage of this invocation handle. Can be null.
-
getException
Returns the exception type that if thrown by the invocation should invalidate the linkage of this guarded invocation.- Returns:
- the exception type that if thrown should be used to invalidate the linkage. Can be null.
-
hasBeenInvalidated
public boolean hasBeenInvalidated()Returns true if and only if this guarded invocation has at least one invalidated switch point.- Returns:
- true if and only if this guarded invocation has at least one invalidated switch point.
-
replaceMethods
Creates a new guarded invocation with different methods, preserving the switch point.- Parameters:
newInvocation
- the new invocationnewGuard
- the new guard- Returns:
- a new guarded invocation with the replaced methods and the same switch point as this invocation.
-
addSwitchPoint
Create a new guarded invocation with an added switch point.- Parameters:
newSwitchPoint
- new switch point. Can be null in which case this method return the current guarded invocation with no changes.- Returns:
- a guarded invocation with the added switch point.
-
asType
Changes the type of the invocation, as ifMethodHandle.asType(MethodType)
was applied to its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for the guard). If the invocation already is of the required type, returns this object.- Parameters:
newType
- the new type of the invocation.- Returns:
- a guarded invocation with the new type applied to it.
-
asType
Changes the type of the invocation, as ifLinkerServices.asType(MethodHandle, MethodType)
was applied to its invocation and its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for the guard). If the invocation already is of the required type, returns this object.- Parameters:
linkerServices
- the linker services to use for the conversionnewType
- the new type of the invocation.- Returns:
- a guarded invocation with the new type applied to it.
-
asTypeSafeReturn
Changes the type of the invocation, as ifLinkerServices.asTypeLosslessReturn(MethodHandle, MethodType)
was applied to its invocation andLinkerServices.asType(MethodHandle, MethodType)
applied to its guard, if it has one (with return type changed to boolean, and parameter count potentially truncated for the guard). If the invocation doesn't change its type, returns this object.- Parameters:
linkerServices
- the linker services to use for the conversionnewType
- the new type of the invocation.- Returns:
- a guarded invocation with the new type applied to it.
-
asType
Changes the type of the invocation, as ifMethodHandle.asType(MethodType)
was applied to its invocation and its guard, if it has one (with return type changed to boolean for guard). If the invocation already is of the required type, returns this object.- Parameters:
desc
- a call descriptor whose method type is adapted.- Returns:
- a guarded invocation with the new type applied to it.
-
filterArguments
Applies argument filters to both the invocation and the guard (if it exists and has at leastpos + 1
parameters) withMethodHandles.filterArguments(MethodHandle, int, MethodHandle...)
.- Parameters:
pos
- the position of the first argument being filteredfilters
- the argument filters- Returns:
- a filtered invocation
-
dropArguments
Makes an invocation that drops arguments in both the invocation and the guard (if it exists and has at leastpos
parameters) withMethodHandles.dropArguments(MethodHandle, int, List)
.- Parameters:
pos
- the position of the first argument being droppedvalueTypes
- the types of the values being dropped- Returns:
- an invocation that drops arguments
-
dropArguments
Makes an invocation that drops arguments in both the invocation and the guard (if it exists and has at leastpos
parameters) withMethodHandles.dropArguments(MethodHandle, int, Class...)
.- Parameters:
pos
- the position of the first argument being droppedvalueTypes
- the types of the values being dropped- Returns:
- an invocation that drops arguments
-
compose
Composes the invocation, guard, switch points, and the exception into a composite method handle that knows how to fall back when the guard fails or the invocation is invalidated.- Parameters:
fallback
- the fallback method handle for when a switch point is invalidated, a guard returns false, or invalidating exception is thrown.- Returns:
- a composite method handle.
-
compose
public MethodHandle compose(MethodHandle guardFallback, MethodHandle switchpointFallback, MethodHandle catchFallback) Composes the invocation, guard, switch points, and the exception into a composite method handle that knows how to fall back when the guard fails or the invocation is invalidated.- Parameters:
guardFallback
- the fallback method handle in case guard returns false.switchpointFallback
- the fallback method handle in case a switch point is invalidated.catchFallback
- the fallback method in case the exception handler triggers.- Returns:
- a composite method handle.
-