ConstantCallSite
, MutableCallSite
, VolatileCallSite
public abstract sealed class CallSite extends Object permits ConstantCallSite, MutableCallSite, VolatileCallSite
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.
invokedynamic
instruction may be permanently bound by means of a constant call site. 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)); }
Modifier and Type | Method | Description |
---|---|---|
abstract MethodHandle |
dynamicInvoker() |
Produces a method handle equivalent to an invokedynamic instruction which has been linked to this call site. |
abstract MethodHandle |
getTarget() |
Returns the target method of the call site, according to the behavior defined by this call site's specific class. |
abstract void |
setTarget |
Updates the target method of this call site, according to the behavior defined by this call site's specific class. |
MethodType |
type() |
Returns the type of this call site's target. |
public MethodType type()
setTarget
method enforces this invariant by refusing any new target that does not have the previous target's type.public abstract MethodHandle getTarget()
CallSite
document the class-specific behaviors of this method.public abstract void setTarget(MethodHandle newTarget)
CallSite
document the class-specific behaviors of this method. The type of the new target must be equal to the type of the old target.
newTarget
- the new targetNullPointerException
- if the proposed new target is nullWrongMethodTypeException
- if the proposed new target has a method type that differs from the previous targetpublic abstract MethodHandle dynamicInvoker()
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)
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/invoke/CallSite.html