Spec-Zone .ru
спецификации, руководства, описания, API
|
|
Java™ Platform Standard Ed. 7 DRAFT ea-b118 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object java.dyn.CallSite
public class CallSite extends Object implements MethodHandleProvider
A CallSite
is a holder for a variable MethodHandle
,
which is called its target
.
Every call to a CallSite
is delegated to the site's current target.
A call site is initially created in an unlinked state, which is distinguished by a null target variable. Before the call site may be invoked (and before certain other operations are attempted), the call site must be linked to a non-null target.
A call site may be relinked by changing its target. The new target must be non-null and 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.
Linkage happens once in the lifetime of any given CallSite
object.
Because of call site invalidation, this linkage can be repeated for
a single invokedynamic
instruction, with multiple CallSite
objects.
When a CallSite
is unlinked from an invokedynamic
instruction,
the instruction is reset so that it is no longer associated with
the CallSite
object, but the CallSite
does not change
state.
Here is a sample use of call sites and bootstrap methods which links every dynamic call site to print its arguments:
@BootstrapMethod(value=PrintArgsDemo.class, name="bootstrapDynamic") static void test() throws Throwable { InvokeDynamic.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(Class caller, String name, MethodType type) { // ignore caller and name, but match the type: return new CallSite(MethodHandles.collectArguments(printArgs, type)); }
Constructor and Description |
---|
CallSite()
Make a blank call site object. |
CallSite(Class<?> caller,
String name,
MethodType type)
Deprecated. transitional form defined in EDR but removed in PFD |
CallSite(MethodHandle target)
Make a blank call site object, possibly equipped with an initial target method handle. |
Modifier and Type | Method and Description |
---|---|
MethodHandle |
asMethodHandle()
Implementation of MethodHandleProvider which returns this.dynamicInvoker() . |
MethodHandle |
asMethodHandle(MethodType type)
Implementation of MethodHandleProvider , which returns this.dynamicInvoker().asType(type) . |
Class<?> |
callerClass()
Deprecated. transitional form defined in EDR but removed in PFD |
MethodHandle |
dynamicInvoker()
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle equivalent to an invokedynamic instruction which has been linked to this call site. |
MethodHandle |
getTarget()
Report the current linkage state of the call site. |
protected MethodHandle |
initialTarget()
Deprecated. transitional form defined in EDR but removed in PFD |
protected MethodHandle |
initialTarget(Class<?> callerClass,
String name,
MethodType type)
Deprecated. transitional form defined in EDR but removed in PFD |
String |
name()
Deprecated. transitional form defined in EDR but removed in PFD |
void |
setTarget(MethodHandle newTarget)
Set the target method of this call site. |
String |
toString()
Produce a printed representation that displays information about this call site that may be useful to the human reader. |
MethodType |
type()
Deprecated. transitional form defined in EDR but removed in PFD |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
---|
public CallSite()
CallSite
object
must be provided with
a target method via a call to setTarget
,
or by a subclass override of initialTarget
.
public CallSite(MethodHandle target)
CallSite
object
must be provided with a target method via a call to setTarget(java.dyn.MethodHandle)
,
or by a subclass override of initialTarget()
.
target
- the method handle which will be the initial target of the call site, or null if there is none yetpublic CallSite(Class<?> caller, String name, MethodType type)
Method Detail |
---|
public Class<?> callerClass()
public String name()
public MethodType type()
protected MethodHandle initialTarget()
protected MethodHandle initialTarget(Class<?> callerClass, String name, MethodType type)
initialTarget
is called to produce an initial
non-null target. (Live call sites must never have null targets.)
The arguments are the same as those passed to the bootstrap method.
Thus, a bootstrap method is free to ignore the arguments and simply
create a "blank" CallSite
object of an appropriate subclass.
If the bootstrap method itself does not initialize the call site,
this method must be overridden, because it just raises an
InvokeDynamicBootstrapError
, which in turn causes the
linkage of the invokedynamic
instruction to terminate
abnormally.
public MethodHandle getTarget()
CallSite
object is returned
from the bootstrap method of the invokedynamic
instruction.
When an invokedynamic
instruction is executed, the target method
of its associated call site
object is invoked directly,
as if via MethodHandle
.invoke
.
The interactions of getTarget
with memory are the same
as of a read from an ordinary variable, such as an array element or a
non-volatile, non-final field.
In particular, the current thread may choose to reuse the result of a previous read of the target from memory, and may fail to see a recent update to the target by another thread.
setTarget(java.dyn.MethodHandle)
public void setTarget(MethodHandle newTarget)
The interactions of setTarget
with memory are the same
as of a write to an ordinary variable, such as an array element or a
non-volatile, non-final field.
In particular, unrelated threads may fail to see the updated target until they perform a read from memory. Stronger guarantees can be created by putting appropriate operations into the bootstrap method and/or the target methods used at any given call site.
newTarget
- the new targetNullPointerException
- if the proposed new target is nullWrongMethodTypeException
- if the call site is linked and the proposed new target
has a method type that differs from the previous targetpublic String toString()
toString
in class Object
public final MethodHandle dynamicInvoker()
If this call site is a ConstantCallSite
, this method
simply returns the call site's target, since that will not change.
Otherwise, this method is equivalent to the following code:
MethodHandle getTarget, invoker, result; getTarget = MethodHandles.lookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class)); invoker = MethodHandles.exactInvoker(this.type()); result = MethodHandles.foldArguments(invoker, getTarget)
public final MethodHandle asMethodHandle()
MethodHandleProvider
which returns this.dynamicInvoker()
.
asMethodHandle
in interface MethodHandleProvider
public final MethodHandle asMethodHandle(MethodType type)
MethodHandleProvider
, which returns this.dynamicInvoker().asType(type)
.
asMethodHandle
in interface MethodHandleProvider
|
Java™ Platform Standard Ed. 7 DRAFT ea-b118 |
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
Copyright © 1993, 2010, Oracle Corporation. All rights reserved.