2009-05-05 22:40:09 -07:00
|
|
|
/*
|
2011-02-11 01:26:28 -08:00
|
|
|
* Copyright (c) 2008, 2011, 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.*;
|
2010-10-30 21:08:23 -07:00
|
|
|
import sun.dyn.empty.Empty;
|
|
|
|
import sun.misc.Unsafe;
|
2010-09-08 18:40:11 -07:00
|
|
|
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}.
|
2010-10-30 21:08:23 -07:00
|
|
|
* An {@code invokedynamic} instruction linked to a {@code CallSite} delegates
|
|
|
|
* all calls to the site's current target.
|
2010-12-16 15:59:27 -08:00
|
|
|
* A {@code CallSite} may be associated with several {@code 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 {@linkplain #dynamicInvoker dynamic invoker}.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
2010-12-16 15:59:27 -08:00
|
|
|
* {@code CallSite} is an abstract class which does not allow
|
|
|
|
* direct subclassing by users. It has three immediate,
|
|
|
|
* concrete subclasses that may be either instantiated or subclassed.
|
|
|
|
* <ul>
|
|
|
|
* <li>If a mutable target is not required, an {@code invokedynamic} instruction
|
|
|
|
* may be permanently bound by means of a {@linkplain ConstantCallSite constant call site}.
|
|
|
|
* <li>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 {@linkplain VolatileCallSite volatile call site} may be used.
|
|
|
|
* <li>Otherwise, if a mutable target is required,
|
|
|
|
* a {@linkplain MutableCallSite mutable call site} may be used.
|
|
|
|
* </ul>
|
2010-04-30 23:48:23 -07:00
|
|
|
* <p>
|
2010-12-16 15:59:27 -08:00
|
|
|
* A non-constant call site may be <em>relinked</em> by changing its target.
|
2010-10-30 21:08:23 -07:00
|
|
|
* The new target must have the same {@linkplain MethodHandle#type() type}
|
2010-09-08 18:40:11 -07:00
|
|
|
* 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>
|
|
|
|
* 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
|
|
|
static void test() throws Throwable {
|
2010-10-30 21:08:23 -07:00
|
|
|
// THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION
|
|
|
|
InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14);
|
2010-09-08 18:40:11 -07:00
|
|
|
}
|
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));
|
|
|
|
}
|
2010-10-30 21:08:23 -07:00
|
|
|
private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
|
2010-04-30 23:48:23 -07:00
|
|
|
// ignore caller and name, but match the type:
|
2011-02-11 01:26:28 -08:00
|
|
|
return new ConstantCallSite(printArgs.asType(type));
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
</pre></blockquote>
|
2009-05-05 22:40:09 -07:00
|
|
|
* @author John Rose, JSR 292 EG
|
|
|
|
*/
|
2010-12-16 15:59:27 -08:00
|
|
|
abstract
|
2010-10-30 21:08:23 -07:00
|
|
|
public class CallSite {
|
2010-01-07 16:16:45 -08:00
|
|
|
private static final Access IMPL_TOKEN = Access.getToken();
|
2011-02-11 01:26:28 -08:00
|
|
|
static { MethodHandleImpl.initStatics(); }
|
2010-01-07 16:16:45 -08:00
|
|
|
|
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-10-30 21:08:23 -07:00
|
|
|
/*package-private*/
|
|
|
|
MethodHandle target;
|
2010-01-07 16:16:45 -08:00
|
|
|
|
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-10-30 21:08:23 -07:00
|
|
|
* Make a blank call site object with the given method type.
|
|
|
|
* An initial target method is supplied which will throw
|
|
|
|
* an {@link IllegalStateException} if called.
|
|
|
|
* <p>
|
|
|
|
* Before this {@code CallSite} object is returned from a bootstrap method,
|
|
|
|
* it is usually provided with a more useful target method,
|
|
|
|
* via a call to {@link CallSite#setTarget(MethodHandle) setTarget}.
|
2010-12-16 15:59:27 -08:00
|
|
|
* @throws NullPointerException if the proposed type is null
|
2009-05-11 21:09:58 -07:00
|
|
|
*/
|
2010-12-16 15:59:27 -08:00
|
|
|
/*package-private*/
|
|
|
|
CallSite(MethodType type) {
|
2010-10-30 21:08:23 -07:00
|
|
|
target = MethodHandles.invokers(type).uninitializedCallSite();
|
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.
|
2010-10-30 21:08:23 -07:00
|
|
|
* @param target the method handle which will be the initial target of the call site
|
2010-12-16 15:59:27 -08:00
|
|
|
* @throws NullPointerException if the proposed target is null
|
2010-04-30 23:48:23 -07:00
|
|
|
*/
|
2010-12-16 15:59:27 -08:00
|
|
|
/*package-private*/
|
|
|
|
CallSite(MethodHandle target) {
|
2010-10-30 21:08:23 -07:00
|
|
|
target.type(); // null check
|
2010-04-30 23:48:23 -07:00
|
|
|
this.target = target;
|
2010-01-07 16:16:45 -08:00
|
|
|
}
|
2010-04-30 23:48:23 -07:00
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* 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.
|
2010-10-30 21:08:23 -07:00
|
|
|
* The {@code setTarget} method enforces this invariant by refusing any new target that does
|
|
|
|
* not have the previous target's type.
|
|
|
|
* @return the type of the current target, which is also the type of any future target
|
2010-04-30 23:48:23 -07:00
|
|
|
*/
|
2010-10-30 21:08:23 -07:00
|
|
|
public MethodType type() {
|
|
|
|
return target.type();
|
2010-04-30 23:48:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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) {
|
2010-10-30 21:08:23 -07:00
|
|
|
if (this.vmmethod != null) {
|
|
|
|
// FIXME
|
2010-04-30 23:48:23 -07:00
|
|
|
throw new InvokeDynamicBootstrapError("call site has already been linked to an invokedynamic instruction");
|
|
|
|
}
|
2010-10-30 21:08:23 -07:00
|
|
|
if (!this.type().equals(type)) {
|
2010-04-30 23:48:23 -07:00
|
|
|
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;
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Returns the target method of the call site, according to the
|
|
|
|
* behavior defined by this call site's specific class.
|
|
|
|
* The immediate subclasses of {@code CallSite} document the
|
|
|
|
* class-specific behaviors of this method.
|
|
|
|
*
|
2010-10-30 21:08:23 -07:00
|
|
|
* @return the current linkage state of the call site, its target method handle
|
|
|
|
* @see ConstantCallSite
|
|
|
|
* @see VolatileCallSite
|
2009-05-05 22:40:09 -07:00
|
|
|
* @see #setTarget
|
2011-02-11 01:26:28 -08:00
|
|
|
* @see ConstantCallSite#getTarget
|
|
|
|
* @see MutableCallSite#getTarget
|
|
|
|
* @see VolatileCallSite#getTarget
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:28 -08:00
|
|
|
public abstract MethodHandle getTarget();
|
2009-05-05 22:40:09 -07:00
|
|
|
|
|
|
|
/**
|
2011-02-11 01:26:28 -08:00
|
|
|
* Updates the target method of this call site, according to the
|
|
|
|
* behavior defined by this call site's specific class.
|
|
|
|
* The immediate subclasses of {@code CallSite} document the
|
|
|
|
* class-specific behaviors of this method.
|
2009-05-05 22:40:09 -07:00
|
|
|
* <p>
|
2011-02-11 01:26:28 -08:00
|
|
|
* The type of the new target must be {@linkplain MethodType#equals equal to}
|
|
|
|
* the type of the old target.
|
|
|
|
*
|
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-10-30 21:08:23 -07:00
|
|
|
* @throws WrongMethodTypeException if the proposed new target
|
2010-04-30 23:48:23 -07:00
|
|
|
* has a method type that differs from the previous target
|
2011-02-11 01:26:28 -08:00
|
|
|
* @see CallSite#getTarget
|
|
|
|
* @see ConstantCallSite#setTarget
|
|
|
|
* @see MutableCallSite#setTarget
|
|
|
|
* @see VolatileCallSite#setTarget
|
2009-05-05 22:40:09 -07:00
|
|
|
*/
|
2011-02-11 01:26:28 -08:00
|
|
|
public abstract void setTarget(MethodHandle newTarget);
|
2010-10-30 21:08:23 -07:00
|
|
|
|
|
|
|
void checkTargetChange(MethodHandle oldTarget, MethodHandle newTarget) {
|
2010-04-30 23:48:23 -07:00
|
|
|
MethodType oldType = oldTarget.type();
|
2010-10-30 21:08:23 -07:00
|
|
|
MethodType newType = newTarget.type(); // null check!
|
|
|
|
if (!newType.equals(oldType))
|
2010-04-30 23:48:23 -07:00
|
|
|
throw wrongTargetType(newTarget, oldType);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-04-30 23:48:23 -07:00
|
|
|
private static WrongMethodTypeException wrongTargetType(MethodHandle target, MethodType type) {
|
2010-10-30 21:08:23 -07:00
|
|
|
return new WrongMethodTypeException(String.valueOf(target)+" should be of type "+type);
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|
|
|
|
|
2010-09-08 18:40:11 -07:00
|
|
|
/**
|
|
|
|
* Produce a method handle equivalent to an invokedynamic instruction
|
|
|
|
* which has been linked to this call site.
|
2011-02-11 01:26:28 -08:00
|
|
|
* <p>
|
|
|
|
* This method is equivalent to the following code:
|
|
|
|
* <blockquote><pre>
|
2010-09-08 18:40:11 -07:00
|
|
|
* MethodHandle getTarget, invoker, result;
|
2011-02-11 01:26:28 -08:00
|
|
|
* getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
|
2010-09-08 18:40:11 -07:00
|
|
|
* invoker = MethodHandles.exactInvoker(this.type());
|
|
|
|
* result = MethodHandles.foldArguments(invoker, getTarget)
|
|
|
|
* </pre></blockquote>
|
2011-02-11 01:26:28 -08:00
|
|
|
*
|
2010-09-08 18:40:11 -07:00
|
|
|
* @return a method handle which always invokes this call site's current target
|
|
|
|
*/
|
2011-02-11 01:26:28 -08:00
|
|
|
public abstract MethodHandle dynamicInvoker();
|
|
|
|
|
|
|
|
/*non-public*/ MethodHandle makeDynamicInvoker() {
|
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);
|
|
|
|
}
|
2011-02-11 01:26:28 -08:00
|
|
|
|
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
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
/** This guy is rolled into the default target if a MethodType is supplied to the constructor. */
|
|
|
|
/*package-private*/
|
|
|
|
static Empty uninitializedCallSite() {
|
|
|
|
throw new IllegalStateException("uninitialized call site");
|
|
|
|
}
|
|
|
|
|
|
|
|
// unsafe stuff:
|
|
|
|
private static final Unsafe unsafe = Unsafe.getUnsafe();
|
|
|
|
private static final long TARGET_OFFSET;
|
|
|
|
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
TARGET_OFFSET = unsafe.objectFieldOffset(CallSite.class.getDeclaredField("target"));
|
|
|
|
} catch (Exception ex) { throw new Error(ex); }
|
|
|
|
}
|
2010-09-08 18:40:11 -07:00
|
|
|
|
2010-10-30 21:08:23 -07:00
|
|
|
/*package-private*/
|
|
|
|
void setTargetNormal(MethodHandle newTarget) {
|
|
|
|
target = newTarget;
|
|
|
|
//CallSiteImpl.setCallSiteTarget(IMPL_TOKEN, this, newTarget);
|
|
|
|
}
|
|
|
|
/*package-private*/
|
|
|
|
MethodHandle getTargetVolatile() {
|
|
|
|
return (MethodHandle) unsafe.getObjectVolatile(this, TARGET_OFFSET);
|
|
|
|
}
|
|
|
|
/*package-private*/
|
|
|
|
void setTargetVolatile(MethodHandle newTarget) {
|
|
|
|
unsafe.putObjectVolatile(this, TARGET_OFFSET, newTarget);
|
|
|
|
//CallSiteImpl.setCallSiteTarget(IMPL_TOKEN, this, newTarget);
|
|
|
|
}
|
2009-05-05 22:40:09 -07:00
|
|
|
}
|