Class CallSite
java.lang.Object
java.lang.invoke.CallSite
- Direct Known Subclasses:
ConstantCallSite, MutableCallSite, VolatileCallSite
public abstract sealed class CallSite
extends Object
permits ConstantCallSite, MutableCallSite, VolatileCallSite
A
CallSite is a holder for a variable MethodHandle,
which is called its target.
An invokedynamic instruction linked to a CallSite delegates
all calls to the site's current target.
A CallSite may be associated with several invokedynamic
instructions, or it may be "free floating", associated with none.
In any case, it may be invoked through an associated method handle
called its dynamic invoker.
CallSite is an abstract sealed class which does not allow
direct subclassing by users. It has three immediate,
concrete non-sealed subclasses that may be either instantiated or subclassed.
- If a mutable target is not required, an
invokedynamicinstruction may be permanently bound by means of a constant call site. - If a mutable target is required which has volatile variable semantics, because updates to the target must be immediately and reliably witnessed by other threads, a volatile call site may be used.
- Otherwise, if a mutable target is required, a mutable call site may be used.
A non-constant call site may be relinked by changing its target. The new target must have the same type as the previous target. Thus, though a call site can be relinked to a series of successive targets, it cannot change its type.
Here is a sample use of call sites and bootstrap methods which links every dynamic call site to print its arguments:
static void test() throws Throwable { // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14); } private static void printArgs(Object... args) { System.out.println(java.util.Arrays.deepToString(args)); } private static final MethodHandle printArgs; static { MethodHandles.Lookup lookup = MethodHandles.lookup(); Class thisClass = lookup.lookupClass(); // (who am I?) printArgs = lookup.findStatic(thisClass, "printArgs", MethodType.methodType(void.class, Object[].class)); } private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) { // ignore caller and name, but match the type: return new ConstantCallSite(printArgs.asType(type)); }
- Since:
- 1.7
-
Method Summary
Modifier and TypeMethodDescriptionabstract MethodHandleProduces a method handle equivalent to an invokedynamic instruction which has been linked to this call site.abstract MethodHandleReturns the target method of the call site, according to the behavior defined by this call site's specific class.abstract voidsetTarget(MethodHandle newTarget) Updates the target method of this call site, according to the behavior defined by this call site's specific class.type()Returns the type of this call site's target.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
-
type
Returns the type of this call site's target. Although targets may change, any call site's type is permanent, and can never change to an unequal type. ThesetTargetmethod enforces this invariant by refusing any new target that does not have the previous target's type.- Returns:
- the type of the current target, which is also the type of any future target
-
getTarget
Returns the target method of the call site, according to the behavior defined by this call site's specific class. The immediate subclasses ofCallSitedocument the class-specific behaviors of this method.- Returns:
- the current linkage state of the call site, its target method handle
- See Also:
-
setTarget
Updates the target method of this call site, according to the behavior defined by this call site's specific class. The immediate subclasses ofCallSitedocument the class-specific behaviors of this method.The type of the new target must be equal to the type of the old target.
- Parameters:
newTarget- the new target- Throws:
NullPointerException- if the proposed new target is nullWrongMethodTypeException- if the proposed new target has a method type that differs from the previous target- See Also:
-
dynamicInvoker
Produces a method handle equivalent to an invokedynamic instruction which has been linked to this call site.This method is equivalent to the following code:
MethodHandle getTarget, invoker, result; getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class)); invoker = MethodHandles.exactInvoker(this.type()); result = MethodHandles.foldArguments(invoker, getTarget)- Returns:
- a method handle which always invokes this call site's current target
-