2009-05-05 22:40:09 -07:00
|
|
|
/*
|
2010-06-18 15:23:57 -07:00
|
|
|
* Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
|
2009-05-05 22:40:09 -07:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
2010-05-25 15:58:33 -07:00
|
|
|
* published by the Free Software Foundation. Oracle designates this
|
2009-05-05 22:40:09 -07:00
|
|
|
* particular file as subject to the "Classpath" exception as provided
|
2010-05-25 15:58:33 -07:00
|
|
|
* by Oracle in the LICENSE file that accompanied this code.
|
2009-05-05 22:40:09 -07:00
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
2010-05-25 15:58:33 -07:00
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
package java.dyn;
|
|
|
|
|
2010-09-08 18:40:11 -07:00
|
|
|
import sun.dyn.*;
|
|
|
|
import java.util.Collection;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/**
|
2010-09-08 18:40:11 -07:00
|
|
|
* A {@code CallSite} is a holder for a variable {@link MethodHandle},
|
|
|
|
* which is called its {@code target}.
|
|
|
|
* Every call to a {@code CallSite} is delegated to the site's current target.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
2010-09-08 18:40:11 -07:00
|
|
|
* A call site is initially created in an <em>unlinked</em> 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.
|
2010-04-30 23:48:23 -07:00
|
|
|
* <p>
|
2010-09-08 18:40:11 -07:00
|
|
|
* A call site may be <em>relinked</em> by changing its target.
|
|
|
|
* The new target must be non-null and must have the same
|
|
|
|
* {@linkplain MethodHandle#type() type}
|
|
|
|
* as the previous target.
|
|
|
|
* Thus, though a call site can be relinked to a series of
|
|
|
|
* successive targets, it cannot change its type.
|
2010-04-30 23:48:23 -07:00
|
|
|
* <p>
|
|
|
|
* Linkage happens once in the lifetime of any given {@code CallSite} object.
|
|
|
|
* Because of call site invalidation, this linkage can be repeated for
|
|
|
|
* a single {@code invokedynamic} instruction, with multiple {@code CallSite} objects.
|
|
|
|
* When a {@code CallSite} is unlinked from an {@code invokedynamic} instruction,
|
|
|
|
* the instruction is reset so that it is no longer associated with
|
|
|
|
* the {@code CallSite} object, but the {@code CallSite} does not change
|
|
|
|
* state.
|
|
|
|
* <p>
|
|
|
|
* Here is a sample use of call sites and bootstrap methods which links every
|
|
|
|
* dynamic call site to print its arguments:
|
|
|
|
<blockquote><pre><!-- see indy-demo/src/PrintArgsDemo.java -->
|
2010-09-08 18:40:11 -07:00
|
|
|
@BootstrapMethod(value=PrintArgsDemo.class, name="bootstrapDynamic")
|
|
|
|
static void test() throws Throwable {
|
|
|
|
InvokeDynamic.baz("baz arg", 2, 3.14);
|
|
|
|
}
|
2010-04-30 23:48:23 -07:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
</pre></blockquote>
|
2009-05-05 22:40:09 -07:00
|
|
|
* @author John Rose, JSR 292 EG
|
|
|
|
*/
|
2010-01-07 16:16:45 -08:00
|
|
|
public class CallSite
|
2010-09-08 18:40:11 -07:00
|
|
|
implements MethodHandleProvider
|
2010-01-07 16:16:45 -08:00
|
|
|
{
|
|
|
|
private static final Access IMPL_TOKEN = Access.getToken();
|
|
|
|
|
2009-05-05 22:40:09 -07:00
|
|
|
// Fields used only by the JVM. Do not use or change.
|
2010-04-30 23:48:23 -07:00
|
|
|
private MemberName vmmethod; // supplied by the JVM (ref. to calling method)
|
|
|
|
private int vmindex; // supplied by the JVM (BCI within calling method)
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
// The actual payload of this call site:
|
2010-01-07 16:16:45 -08:00
|
|
|
private MethodHandle target;
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
// Remove this field for PFD and delete deprecated methods:
|
|
|
|
private MemberName calleeNameRemoveForPFD;
|
2009-05-05 22:40:09 -07:00
|
|
|
|
2009-05-11 21:09:58 -07:00
|
|
|
/**
|
2010-04-30 23:48:23 -07:00
|
|
|
* Make a blank call site object.
|
|
|
|
* Before it is returned from a bootstrap method, this {@code CallSite} object
|
|
|
|
* must be provided with
|
|
|
|
* a target method via a call to {@link CallSite#setTarget(MethodHandle) setTarget},
|
|
|
|
* or by a subclass override of {@link CallSite#initialTarget(Class,String,MethodType) initialTarget}.
|
2009-05-11 21:09:58 -07:00
|
|
|
*/
|
2010-04-30 23:48:23 -07:00
|
|
|
public CallSite() {
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
/**
|
|
|
|
* 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 {@code CallSite} object
|
|
|
|
* must be provided with a target method via a call to {@link CallSite#setTarget},
|
|
|
|
* or by a subclass override of {@link CallSite#initialTarget}.
|
|
|
|
* @param target the method handle which will be the initial target of the call site, or null if there is none yet
|
|
|
|
*/
|
|
|
|
public CallSite(MethodHandle target) {
|
|
|
|
this.target = target;
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
2010-04-30 23:48:23 -07:00
|
|
|
|
|
|
|
/** @deprecated transitional form defined in EDR but removed in PFD */
|
|
|
|
public CallSite(Class<?> caller, String name, MethodType type) {
|
|
|
|
this.calleeNameRemoveForPFD = new MemberName(caller, name, type);
|
|
|
|
}
|
|
|
|
/** @deprecated transitional form defined in EDR but removed in PFD */
|
|
|
|
public Class<?> callerClass() {
|
|
|
|
MemberName callee = this.calleeNameRemoveForPFD;
|
|
|
|
return callee == null ? null : callee.getDeclaringClass();
|
|
|
|
}
|
|
|
|
/** @deprecated transitional form defined in EDR but removed in PFD */
|
|
|
|
public String name() {
|
|
|
|
MemberName callee = this.calleeNameRemoveForPFD;
|
|
|
|
return callee == null ? null : callee.getName();
|
|
|
|
}
|
|
|
|
/** @deprecated transitional form defined in EDR but removed in PFD */
|
|
|
|
public MethodType type() {
|
|
|
|
MemberName callee = this.calleeNameRemoveForPFD;
|
|
|
|
return callee == null ? (target == null ? null : target.type()) : callee.getMethodType();
|
|
|
|
}
|
|
|
|
/** @deprecated transitional form defined in EDR but removed in PFD */
|
|
|
|
protected MethodHandle initialTarget() {
|
|
|
|
return initialTarget(callerClass(), name(), type());
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Report if the JVM has linked this {@code CallSite} object to a dynamic call site instruction.
|
|
|
|
* Once it is linked, it is never unlinked.
|
|
|
|
*/
|
|
|
|
private boolean isLinked() {
|
|
|
|
return vmmethod != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Called from JVM (or low-level Java code) after the BSM returns the newly created CallSite.
|
|
|
|
* The parameters are JVM-specific.
|
|
|
|
*/
|
|
|
|
void initializeFromJVM(String name,
|
|
|
|
MethodType type,
|
|
|
|
MemberName callerMethod,
|
|
|
|
int callerBCI) {
|
|
|
|
if (this.isLinked()) {
|
|
|
|
throw new InvokeDynamicBootstrapError("call site has already been linked to an invokedynamic instruction");
|
|
|
|
}
|
|
|
|
MethodHandle target = this.target;
|
|
|
|
if (target == null) {
|
|
|
|
this.target = target = this.initialTarget(callerMethod.getDeclaringClass(), name, type);
|
|
|
|
}
|
|
|
|
if (!target.type().equals(type)) {
|
|
|
|
throw wrongTargetType(target, type);
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
2010-04-30 23:48:23 -07:00
|
|
|
this.vmindex = callerBCI;
|
|
|
|
this.vmmethod = callerMethod;
|
|
|
|
assert(this.isLinked());
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 {@code initialTarget} is called to produce an initial
|
|
|
|
* non-null target. (Live call sites must never have null targets.)
|
|
|
|
* <p>
|
2010-04-30 23:48:23 -07:00
|
|
|
* 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" {@code CallSite} object of an appropriate subclass.
|
|
|
|
* <p>
|
2009-05-05 22:40:09 -07:00
|
|
|
* If the bootstrap method itself does not initialize the call site,
|
|
|
|
* this method must be overridden, because it just raises an
|
2009-05-11 21:09:58 -07:00
|
|
|
* {@code InvokeDynamicBootstrapError}, which in turn causes the
|
|
|
|
* linkage of the {@code invokedynamic} instruction to terminate
|
|
|
|
* abnormally.
|
2010-09-08 18:40:11 -07:00
|
|
|
* @deprecated transitional form defined in EDR but removed in PFD
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2010-04-30 23:48:23 -07:00
|
|
|
protected MethodHandle initialTarget(Class<?> callerClass, String name, MethodType type) {
|
|
|
|
throw new InvokeDynamicBootstrapError("target must be initialized before call site is linked: "+name+type);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Report the current linkage state of the call site. (This is mutable.)
|
2010-01-07 16:16:45 -08:00
|
|
|
* The value may not be null after the {@code CallSite} object is returned
|
|
|
|
* from the bootstrap method of the {@code invokedynamic} instruction.
|
|
|
|
* When an {@code invokedynamic} instruction is executed, the target method
|
|
|
|
* of its associated {@code call site} object is invoked directly,
|
|
|
|
* as if via {@link MethodHandle}{@code .invoke}.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
|
|
|
* The interactions of {@code 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.
|
|
|
|
* <p>
|
|
|
|
* 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.
|
|
|
|
* @return the current linkage state of the call site
|
|
|
|
* @see #setTarget
|
|
|
|
*/
|
|
|
|
public MethodHandle getTarget() {
|
2010-04-30 23:48:23 -07:00
|
|
|
return target;
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2010-04-30 23:48:23 -07:00
|
|
|
* Set the target method of this call site.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
|
|
|
* The interactions of {@code 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.
|
|
|
|
* <p>
|
|
|
|
* 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.
|
2010-04-30 23:48:23 -07:00
|
|
|
* @param newTarget the new target
|
2009-05-11 21:09:58 -07:00
|
|
|
* @throws NullPointerException if the proposed new target is null
|
2010-04-30 23:48:23 -07:00
|
|
|
* @throws WrongMethodTypeException if the call site is linked and the proposed new target
|
|
|
|
* has a method type that differs from the previous target
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2010-04-30 23:48:23 -07:00
|
|
|
public void setTarget(MethodHandle newTarget) {
|
|
|
|
MethodType newType = newTarget.type(); // null check!
|
|
|
|
MethodHandle oldTarget = this.target;
|
|
|
|
if (oldTarget == null) {
|
|
|
|
// CallSite is not yet linked.
|
|
|
|
assert(!isLinked());
|
|
|
|
this.target = newTarget; // might be null!
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MethodType oldType = oldTarget.type();
|
|
|
|
if (!newTarget.type().equals(oldType))
|
|
|
|
throw wrongTargetType(newTarget, oldType);
|
|
|
|
if (oldTarget != newTarget)
|
|
|
|
CallSiteImpl.setCallSiteTarget(IMPL_TOKEN, this, newTarget);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
private static WrongMethodTypeException wrongTargetType(MethodHandle target, MethodType type) {
|
|
|
|
return new WrongMethodTypeException(String.valueOf(target)+target.type()+" should be of type "+type);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
/** Produce a printed representation that displays information about this call site
|
|
|
|
* that may be useful to the human reader.
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2010-09-08 18:40:11 -07:00
|
|
|
return "CallSite"+(target == null ? "" : target.type());
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
2010-09-08 18:40:11 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* <em>PROVISIONAL API, WORK IN PROGRESS:</em>
|
|
|
|
* Produce a method handle equivalent to an invokedynamic instruction
|
|
|
|
* which has been linked to this call site.
|
|
|
|
* <p>If this call site is a {@link ConstantCallSite}, this method
|
|
|
|
* simply returns the call site's target, since that will not change.
|
|
|
|
* <p>Otherwise, this method is equivalent to the following code:
|
|
|
|
* <p><blockquote><pre>
|
|
|
|
* MethodHandle getTarget, invoker, result;
|
|
|
|
* getTarget = MethodHandles.lookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
|
|
|
|
* invoker = MethodHandles.exactInvoker(this.type());
|
|
|
|
* result = MethodHandles.foldArguments(invoker, getTarget)
|
|
|
|
* </pre></blockquote>
|
|
|
|
* @return a method handle which always invokes this call site's current target
|
|
|
|
*/
|
|
|
|
public final MethodHandle dynamicInvoker() {
|
|
|
|
if (this instanceof ConstantCallSite)
|
|
|
|
return getTarget(); // will not change dynamically
|
2010-09-08 18:40:23 -07:00
|
|
|
MethodHandle getTarget = MethodHandleImpl.bindReceiver(IMPL_TOKEN, GET_TARGET, this);
|
2010-09-08 18:40:11 -07:00
|
|
|
MethodHandle invoker = MethodHandles.exactInvoker(this.type());
|
|
|
|
return MethodHandles.foldArguments(invoker, getTarget);
|
|
|
|
}
|
2010-09-08 18:40:23 -07:00
|
|
|
private static final MethodHandle GET_TARGET;
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
GET_TARGET = MethodHandles.Lookup.IMPL_LOOKUP.
|
|
|
|
findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class));
|
|
|
|
} catch (NoAccessException ignore) {
|
|
|
|
throw new InternalError();
|
|
|
|
}
|
|
|
|
}
|
2010-09-08 18:40:11 -07:00
|
|
|
|
|
|
|
/** Implementation of {@link MethodHandleProvider} which returns {@code this.dynamicInvoker()}. */
|
|
|
|
public final MethodHandle asMethodHandle() { return dynamicInvoker(); }
|
|
|
|
|
|
|
|
/** Implementation of {@link MethodHandleProvider}, which returns {@code this.dynamicInvoker().asType(type)}. */
|
|
|
|
public final MethodHandle asMethodHandle(MethodType type) { return dynamicInvoker().asType(type); }
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|