Spec-Zone .ru
спецификации, руководства, описания, API
Please note that the specifications and other information contained herein are not final and are subject to change. The information is being made available to you solely for purpose of evaluation.

Java™ Platform
Standard Ed. 7

DRAFT ea-b118

java.dyn
Class CallSite

java.lang.Object
  extended by java.dyn.CallSite
All Implemented Interfaces:
MethodHandleProvider
Direct Known Subclasses:
ConstantCallSite

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 Summary
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.
 
Method Summary
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

CallSite

public CallSite()
Make a blank call site object. Before it is returned from a bootstrap method, this CallSite object must be provided with a target method via a call to setTarget, or by a subclass override of initialTarget.


CallSite

public CallSite(MethodHandle target)
Make a blank call site object, possibly equipped with an initial target method handle. The initial target reference may be null, in which case the CallSite object must be provided with a target method via a call to setTarget(java.dyn.MethodHandle), or by a subclass override of initialTarget().

Parameters:
target - the method handle which will be the initial target of the call site, or null if there is none yet

CallSite

public CallSite(Class<?> caller,
                String name,
                MethodType type)
Deprecated. transitional form defined in EDR but removed in PFD

Method Detail

callerClass

public Class<?> callerClass()
Deprecated. transitional form defined in EDR but removed in PFD


name

public String name()
Deprecated. transitional form defined in EDR but removed in PFD


type

public MethodType type()
Deprecated. transitional form defined in EDR but removed in PFD


initialTarget

protected MethodHandle initialTarget()
Deprecated. transitional form defined in EDR but removed in PFD


initialTarget

protected MethodHandle initialTarget(Class<?> callerClass,
                                     String name,
                                     MethodType type)
Deprecated. transitional form defined in EDR but removed in PFD

Just after a call site is created by a bootstrap method handle, if the target has not been initialized by the factory method itself, the method 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.


getTarget

public MethodHandle getTarget()
Report the current linkage state of the call site. (This is mutable.) The value may not be null after the 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.

Returns:
the current linkage state of the call site
See Also:
setTarget(java.dyn.MethodHandle)

setTarget

public void setTarget(MethodHandle newTarget)
Set the target method of this call site.

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.

Parameters:
newTarget - the new target
Throws:
NullPointerException - if the proposed new target is null
WrongMethodTypeException - if the call site is linked and the proposed new target has a method type that differs from the previous target

toString

public String toString()
Produce a printed representation that displays information about this call site that may be useful to the human reader.

Overrides:
toString in class Object
Returns:
a string representation of the object.

dynamicInvoker

public final MethodHandle dynamicInvoker()
PROVISIONAL API, WORK IN PROGRESS: Produce a method handle equivalent to an invokedynamic instruction which has been linked to this call site.

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)
 

Returns:
a method handle which always invokes this call site's current target

asMethodHandle

public final MethodHandle asMethodHandle()
Implementation of MethodHandleProvider which returns this.dynamicInvoker().

Specified by:
asMethodHandle in interface MethodHandleProvider

asMethodHandle

public final MethodHandle asMethodHandle(MethodType type)
Implementation of MethodHandleProvider, which returns this.dynamicInvoker().asType(type).

Specified by:
asMethodHandle in interface MethodHandleProvider

Java™ Platform
Standard Ed. 7

DRAFT ea-b118

Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.

Copyright © 1993, 2010, Oracle Corporation. All rights reserved.
DRAFT ea-b118

Scripting on this page tracks web page traffic, but does not change the content in any way.