diff --git a/jdk/src/share/classes/java/dyn/BootstrapMethod.java b/jdk/src/share/classes/java/dyn/BootstrapMethod.java new file mode 100644 index 00000000000..a5c41b4854b --- /dev/null +++ b/jdk/src/share/classes/java/dyn/BootstrapMethod.java @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + * 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 + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + * + * 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. + */ + +package java.dyn; + +import java.lang.annotation.*; + +/** + * Annotation on InvokeDynamic method calls which requests the JVM to use a specific + * bootstrap method + * to link the call. This annotation is not retained as such in the class file, + * but is transformed into a constant-pool entry for the invokedynamic instruction which + * specifies the desired bootstrap method. + *
+ * If only the value is given, it must name a subclass of {@link CallSite}
+ * with a constructor which accepts a class, string, and method type.
+ * If the value and name are both given, there must be
+ * a static method in the given class of the given name which accepts a class, string,
+ * and method type, and returns a reference coercible to {@link CallSite}.
+ *
+ * This annotation can be placed either on the return type of a single {@link InvokeDynamic} + * call (see examples) or else it can be placed on an enclosing class or method, where it + * determines a default bootstrap method for any {@link InvokeDynamic} calls which are not + * specifically annotated with a bootstrap method. + * Every {@link InvokeDynamic} call must be given a bootstrap method. + *
+ * Examples: +
+@BootstrapMethod(value=MyLanguageRuntime.class, name="bootstrapDynamic")
+String x = (String) InvokeDynamic.greet();
+//BSM => MyLanguageRuntime.bootstrapDynamic(Here.class, "greet", methodType(String.class))
+@BootstrapMethod(MyCallSite.class)
+void example() throws Throwable {
+ InvokeDynamic.greet();
+ //BSM => new MyCallSite(Here.class, "greet", methodType(void.class))
+}
+
+ * + */ +@Target({ElementType.TYPE_USE, + // For defaulting every indy site within a class or method; cf. @SuppressWarnings: + ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR + }) +@Retention(RetentionPolicy.SOURCE) +public @interface BootstrapMethod { + /** The class containing the bootstrap method. */ + Class> value(); + + /** The name of the bootstrap method. + * If this is the empty string, an instance of the bootstrap class is created, + * and a constructor is invoked. + * Otherwise, there must be a static method of the required name. + */ + String name() default ""; // empty string denotes a constructor with 'new' + + /** The argument types of the bootstrap method, as passed out by the JVM. + * There is usually no reason to override the default. + */ + Class>[] arguments() default {Class.class, String.class, MethodType.class}; +} diff --git a/jdk/src/share/classes/java/dyn/CallSite.java b/jdk/src/share/classes/java/dyn/CallSite.java index b8335774851..7c4ed52ea68 100644 --- a/jdk/src/share/classes/java/dyn/CallSite.java +++ b/jdk/src/share/classes/java/dyn/CallSite.java @@ -25,56 +25,26 @@ package java.dyn; -import sun.dyn.Access; -import sun.dyn.MemberName; -import sun.dyn.CallSiteImpl; +import sun.dyn.*; +import java.util.Collection; /** - * A {@code CallSite} reifies an {@code invokedynamic} instruction from bytecode, - * and controls its linkage. - * Every linked {@code CallSite} object corresponds to a distinct instance - * of the {@code invokedynamic} instruction, and vice versa. + * 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. *
- * Every linked {@code CallSite} object has one state variable, - * a {@link MethodHandle} reference called the {@code target}. - * This reference is never null. Though it can change its value - * successive values must always have exactly the {@link MethodType method type} - * called for by the bytecodes of the associated {@code invokedynamic} instruction + * 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. *
- * It is the responsibility of each class's - * {@link Linkage#registerBootstrapMethod(Class, MethodHandle) bootstrap method} - * to produce call sites which have been pre-linked to an initial target method. - * The required {@link MethodType type} for the target method is a parameter - * to each bootstrap method call. - *
- * The bootstrap method may elect to produce call sites of a - * language-specific subclass of {@code CallSite}. In such a case, - * the subclass may claim responsibility for initializing its target to - * a non-null value, by overriding {@link #initialTarget}. - *
- * An {@code invokedynamic} instruction which has not yet been executed - * is said to be unlinked. When an unlinked call site is executed, - * the containing class's bootstrap method is called to manufacture a call site, - * for the instruction. If the bootstrap method does not assign a non-null - * value to the new call site's target variable, the method {@link #initialTarget} - * is called to produce the new call site's first target method. - *
- * A freshly-created {@code CallSite} object is not yet in a linked state. - * An unlinked {@code CallSite} object reports null for its {@code callerClass}. - * When the JVM receives a {@code CallSite} object from a bootstrap method, - * it first ensures that its target is non-null and of the correct type. - * The JVM then links the {@code CallSite} object to the call site instruction, - * enabling the {@code callerClass} to return the class in which the instruction occurs. - *
- * Next, the JVM links the instruction to the {@code CallSite}, at which point - * any further execution of the {@code invokedynamic} instruction implicitly - * invokes the current target of the {@code CallSite} object. - * After this two-way linkage, both the instruction and the {@code CallSite} - * object are said to be linked. - *
- * This state of linkage continues until the method containing the - * dynamic call site is garbage collected, or the dynamic call site - * is invalidated by an explicit request. + * A call site may be relinked 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. *
* Linkage happens once in the lifetime of any given {@code CallSite} object. * Because of call site invalidation, this linkage can be repeated for @@ -87,6 +57,10 @@ import sun.dyn.CallSiteImpl; * 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));
}
@@ -96,17 +70,16 @@ static {
Class thisClass = lookup.lookupClass(); // (who am I?)
printArgs = lookup.findStatic(thisClass,
"printArgs", MethodType.methodType(void.class, Object[].class));
- Linkage.registerBootstrapMethod("bootstrapDynamic");
}
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));
}
- * @see Linkage#registerBootstrapMethod(java.lang.Class, java.dyn.MethodHandle)
* @author John Rose, JSR 292 EG
*/
public class CallSite
+ implements MethodHandleProvider
{
private static final Access IMPL_TOKEN = Access.getToken();
@@ -209,6 +182,7 @@ public class CallSite
* {@code InvokeDynamicBootstrapError}, which in turn causes the
* linkage of the {@code invokedynamic} instruction to terminate
* abnormally.
+ * @deprecated transitional form defined in EDR but removed in PFD
*/
protected MethodHandle initialTarget(Class> callerClass, String name, MethodType type) {
throw new InvokeDynamicBootstrapError("target must be initialized before call site is linked: "+name+type);
@@ -278,16 +252,44 @@ public class CallSite
*/
@Override
public String toString() {
- StringBuilder buf = new StringBuilder("CallSite#");
- buf.append(hashCode());
- if (!isLinked())
- buf.append("[unlinked]");
- else
- buf.append("[")
- .append("from ").append(vmmethod.getDeclaringClass().getName())
- .append(" : ").append(getTarget().type())
- .append(" => ").append(getTarget())
- .append("]");
- return buf.toString();
+ return "CallSite"+(target == null ? "" : target.type());
}
+
+ /**
+ * 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 {@link ConstantCallSite}, this method + * simply returns the call site's target, since that will not change. + *
Otherwise, this method is equivalent to the following code: + *
+ * @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 + MethodHandle getTarget = MethodHandleImpl.bindReceiver(IMPL_TOKEN, GET_TARGET, this); + MethodHandle invoker = MethodHandles.exactInvoker(this.type()); + return MethodHandles.foldArguments(invoker, getTarget); + } + 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(); + } + } + + /** 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); } } diff --git a/jdk/src/share/classes/java/dyn/ClassValue.java b/jdk/src/share/classes/java/dyn/ClassValue.java new file mode 100644 index 00000000000..325d55e0742 --- /dev/null +++ b/jdk/src/share/classes/java/dyn/ClassValue.java @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. + * 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 + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * 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. + * + * 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. + */ + +package java.dyn; + +import java.util.WeakHashMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicReference; + +/** + * Lazily associate a computed value with (potentially) every class. + * @author John Rose, JSR 292 EG + */ +public abstract class ClassValue+ * MethodHandle getTarget, invoker, result; + * getTarget = MethodHandles.lookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class)); + * invoker = MethodHandles.exactInvoker(this.type()); + * result = MethodHandles.foldArguments(invoker, getTarget) + *
+ * This method will be invoked within the first thread that accesses + * the value with the {@link #get}. + *
+ * Normally, this method is invoked at most once per class, + * but it may be invoked again in case of subsequent invocations + * of {@link #remove} followed by {@link #get}. + * + * @return the computed value for this thread-local + */ + protected abstract T computeValue(Class> type); + + /** + * Creates a new class value. + */ + protected ClassValue() { + } + + /** + * Returns the value for the given class. + * If no value has yet been computed, it is obtained by + * by an invocation of the {@link #computeValue} method. + *
+ * The actual installation of the value on the class
+ * is performed while the class's synchronization lock
+ * is held. At that point, if racing threads have
+ * computed values, one is chosen, and returned to
+ * all the racing threads.
+ *
+ * @return the current thread's value of this thread-local
+ */
+ public T get(Class> type) {
+ ClassValueMap map = getMap(type);
+ if (map != null) {
+ Object x = map.get(this);
+ if (x != null) {
+ return (T) map.unmaskNull(x);
+ }
+ }
+ return setComputedValue(type);
+ }
+
+ /**
+ * Removes the associated value for the given class.
+ * If this value is subsequently {@linkplain #get read} for the same class,
+ * its value will be reinitialized by invoking its {@link #computeValue} method.
+ * This may result in an additional invocation of the
+ * {@code computeValue} method for the given class.
+ */
+ public void remove(Class> type) {
+ ClassValueMap map = getMap(type);
+ if (map != null) {
+ synchronized (map) {
+ map.remove(this);
+ }
+ }
+ }
+
+ /// Implementation...
+
+ /** The hash code for this type is based on the identity of the object,
+ * and is well-dispersed for power-of-two tables.
+ */
+ public final int hashCode() { return hashCode; }
+ private final int hashCode = HASH_CODES.getAndAdd(0x61c88647);
+ private static final AtomicInteger HASH_CODES = new AtomicInteger();
+
+ private static final AtomicInteger STORE_BARRIER = new AtomicInteger();
+
+ /** Slow path for {@link #get}. */
+ private T setComputedValue(Class> type) {
+ ClassValueMap map = getMap(type);
+ if (map == null) {
+ map = initializeMap(type);
+ }
+ T value = computeValue(type);
+ STORE_BARRIER.lazySet(0);
+ // All stores pending from computeValue are completed.
+ synchronized (map) {
+ // Warm up the table with a null entry.
+ map.preInitializeEntry(this);
+ }
+ // All stores pending from table expansion are completed.
+ synchronized (map) {
+ value = (T) map.initializeEntry(this, value);
+ // One might fear a possible race condition here
+ // if the code for map.put has flushed the write
+ // to map.table[*] before the writes to the Map.Entry
+ // are done. This is not possible, since we have
+ // warmed up the table with an empty entry.
+ }
+ return value;
+ }
+
+ // Replace this map by a per-class slot.
+ private static final WeakHashMap
* The type {@code InvokeDynamic} has no particular meaning as a
@@ -45,22 +45,31 @@ package java.dyn;
* It may be imported for ease of use.
*
* Here are some examples:
- *
* The argument types are taken directly from the actual arguments,
- * while the return type is taken from the type parameter.
- * (This type parameter may be a primtive, and it defaults to {@code Object}.)
+ * while the return type corresponds to the target of the assignment.
+ * (Currently, the return type must be given as a false type parameter.
+ * This type parameter is an irregular use of the generic type syntax,
+ * and is likely to change in favor of a convention based on target typing.)
+ *
* The final example uses a special syntax for uttering non-Java names.
* Any name legal to the JVM may be given between the double quotes.
+ *
* None of these calls is complete without a bootstrap method,
- * which must be registered by the static initializer of the enclosing class.
+ * which must be declared for the enclosing class or method.
* @author John Rose, JSR 292 EG
*/
@MethodHandle.PolymorphicSignature
diff --git a/jdk/src/share/classes/java/dyn/InvokeDynamicBootstrapError.java b/jdk/src/share/classes/java/dyn/InvokeDynamicBootstrapError.java
index a301dfa42bc..3820e94c774 100644
--- a/jdk/src/share/classes/java/dyn/InvokeDynamicBootstrapError.java
+++ b/jdk/src/share/classes/java/dyn/InvokeDynamicBootstrapError.java
@@ -28,15 +28,11 @@ package java.dyn;
/**
* Thrown to indicate that an {@code invokedynamic} instruction has
* failed to find its
- * {@linkplain Linkage#registerBootstrapMethod(Class, MethodHandle) bootstrap method},
+ * {@linkplain BootstrapMethod bootstrap method},
* or the bootstrap method has
* failed to provide a
* {@linkplain CallSite} call site with a non-null {@linkplain MethodHandle target}
* of the correct {@linkplain MethodType method type}.
- *
- * The bootstrap method must have been declared during a class's initialization
- * by a call to one of the overloadings of
- * {@link Linkage#registerBootstrapMethod registerBootstrapMethod}.
*
* @author John Rose, JSR 292 EG
* @since 1.7
diff --git a/jdk/src/share/classes/java/dyn/Linkage.java b/jdk/src/share/classes/java/dyn/Linkage.java
index d65ae41c31c..98eedbbabfb 100644
--- a/jdk/src/share/classes/java/dyn/Linkage.java
+++ b/jdk/src/share/classes/java/dyn/Linkage.java
@@ -25,7 +25,6 @@
package java.dyn;
-import java.lang.annotation.Annotation;
import java.dyn.MethodHandles.Lookup;
import java.util.WeakHashMap;
import sun.dyn.Access;
@@ -56,11 +55,7 @@ public class Linkage {
*
@@ -86,6 +81,7 @@ public class Linkage {
* or is already running in another thread
* @exception SecurityException if there is a security manager installed,
* and a {@link LinkagePermission} check fails for "registerBootstrapMethod"
+ * @deprecated Use @{@link BootstrapMethod} annotations instead
*/
public static
void registerBootstrapMethod(Class callerClass, MethodHandle bootstrapMethod) {
@@ -97,14 +93,9 @@ public class Linkage {
static private void checkBSM(MethodHandle mh) {
if (mh == null) throw newIllegalArgumentException("null bootstrap method");
- if (mh.type() == BOOTSTRAP_METHOD_TYPE_2)
- // For now, always pass an empty array for the Annotations argument
- mh = MethodHandles.insertArguments(mh, BOOTSTRAP_METHOD_TYPE_2.parameterCount()-1,
- (Object)NO_ANNOTATIONS);
if (mh.type() == BOOTSTRAP_METHOD_TYPE) return;
throw new WrongMethodTypeException(mh.toString());
}
- static private final Annotation[] NO_ANNOTATIONS = { };
/**
* PROVISIONAL API, WORK IN PROGRESS:
@@ -115,6 +106,7 @@ public class Linkage {
* @throws NoSuchMethodException if there is no such method
* @throws IllegalStateException if the caller class's static initializer
* has already run, or is already running in another thread
+ * @deprecated Use @{@link BootstrapMethod} annotations instead
*/
public static
void registerBootstrapMethod(Class> runtime, String name) {
@@ -131,6 +123,7 @@ public class Linkage {
* @throws IllegalArgumentException if there is no such method
* @throws IllegalStateException if the caller class's static initializer
* has already run, or is already running in another thread
+ * @deprecated Use @{@link BootstrapMethod} annotations instead
*/
public static
void registerBootstrapMethod(String name) {
@@ -142,18 +135,10 @@ public class Linkage {
void registerBootstrapMethodLookup(Class> callerClass, Class> runtime, String name) {
Lookup lookup = new Lookup(IMPL_TOKEN, callerClass);
MethodHandle bootstrapMethod;
- // Try both types. TBD
try {
- bootstrapMethod = lookup.findStatic(runtime, name, BOOTSTRAP_METHOD_TYPE_2);
+ bootstrapMethod = lookup.findStatic(runtime, name, BOOTSTRAP_METHOD_TYPE);
} catch (NoAccessException ex) {
- bootstrapMethod = null;
- }
- if (bootstrapMethod == null) {
- try {
- bootstrapMethod = lookup.findStatic(runtime, name, BOOTSTRAP_METHOD_TYPE);
- } catch (NoAccessException ex) {
- throw new IllegalArgumentException("no such bootstrap method in "+runtime+": "+name, ex);
- }
+ throw new IllegalArgumentException("no such bootstrap method in "+runtime+": "+name, ex);
}
checkBSM(bootstrapMethod);
MethodHandleImpl.registerBootstrap(IMPL_TOKEN, callerClass, bootstrapMethod);
@@ -172,6 +157,7 @@ public class Linkage {
* and the immediate caller of this method is not in the same
* package as the caller class
* and a {@link LinkagePermission} check fails for "getBootstrapMethod"
+ * @deprecated
*/
public static
MethodHandle getBootstrapMethod(Class callerClass) {
@@ -188,10 +174,6 @@ public class Linkage {
public static final MethodType BOOTSTRAP_METHOD_TYPE
= MethodType.methodType(CallSite.class,
Class.class, String.class, MethodType.class);
- static final MethodType BOOTSTRAP_METHOD_TYPE_2
- = MethodType.methodType(CallSite.class,
- Class.class, String.class, MethodType.class,
- Annotation[].class);
/**
* PROVISIONAL API, WORK IN PROGRESS:
diff --git a/jdk/src/share/classes/java/dyn/LinkagePermission.java b/jdk/src/share/classes/java/dyn/LinkagePermission.java
index 6f6dc5004eb..9861843918d 100644
--- a/jdk/src/share/classes/java/dyn/LinkagePermission.java
+++ b/jdk/src/share/classes/java/dyn/LinkagePermission.java
@@ -31,6 +31,7 @@ import java.util.Hashtable;
import java.util.StringTokenizer;
/**
+ * PROVISIONAL API, WORK IN PROGRESS:
* This class is for managing runtime permission checking for
* operations performed by methods in the {@link Linkage} class.
* Like a {@link RuntimePermission}, on which it is modeled,
@@ -52,13 +53,6 @@ import java.util.StringTokenizer;
*
*
* ISSUE: Is this still needed?
*
- * @see java.security.RuntimePermission
+ * @see java.lang.RuntimePermission
* @see java.lang.SecurityManager
*
* @author John Rose, JSR 292 EG
@@ -86,7 +81,7 @@ public final class LinkagePermission extends BasicPermission {
/**
* Create a new LinkagePermission with the given name.
* The name is the symbolic name of the LinkagePermission, such as
- * "registerBootstrapMethod", "invalidateCallerClass.*", etc. An asterisk
+ * "invalidateCallerClass.*", etc. An asterisk
* may appear at the end of the name, following a ".", or by itself, to
* signify a wildcard match.
*
diff --git a/jdk/src/share/classes/java/dyn/MethodHandle.java b/jdk/src/share/classes/java/dyn/MethodHandle.java
index 20387ca6859..4904e975306 100644
--- a/jdk/src/share/classes/java/dyn/MethodHandle.java
+++ b/jdk/src/share/classes/java/dyn/MethodHandle.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -36,11 +36,13 @@ import static sun.dyn.MemberName.newIllegalArgumentException; // utility
/**
* A method handle is a typed, directly executable reference to a method,
* constructor, field, or similar low-level operation, with optional
- * conversion or substitution of arguments or return values.
+ * transformations of arguments or return values.
+ * (These transformations include conversion, insertion, deletion,
+ * substitution. See the methods of this class and of {@link MethodHandles}.)
*
* Method handles are strongly typed according to signature.
* They are not distinguished by method name or enclosing class.
- * A method handle must be invoked under a signature which exactly matches
+ * A method handle must be invoked under a signature which matches
* the method handle's own {@link MethodType method type}.
*
* Every method handle confesses its type via the {@code type} accessor.
@@ -174,9 +176,10 @@ assert(i == 3);
* merely a documentation convention. These type parameters do
* not play a role in type-checking method handle invocations.
*
- * Note: Like classes and strings, method handles that correspond directly
- * to fields and methods can be represented directly as constants to be
- * loaded by {@code ldc} bytecodes.
+ * Like classes and strings, method handles that correspond to accessible
+ * fields, methods, and constructors can be represented directly
+ * in a class file's constant pool as constants to be loaded by {@code ldc} bytecodes.
+ * Loading such a constant causes the component classes of its type to be loaded as necessary.
*
* @see MethodType
* @see MethodHandles
@@ -186,6 +189,7 @@ public abstract class MethodHandle
// Note: This is an implementation inheritance hack, and will be removed
// with a JVM change which moves the required hidden state onto this class.
extends MethodHandleImpl
+ implements MethodHandleProvider
{
private static Access IMPL_TOKEN = Access.getToken();
@@ -197,7 +201,7 @@ public abstract class MethodHandle
* those methods which are signature polymorphic.
*/
@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD,java.lang.annotation.ElementType.TYPE})
- @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.CLASS)
+ @java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)
@interface PolymorphicSignature { }
private MethodType type;
@@ -274,10 +278,14 @@ public abstract class MethodHandle
* and performing simple conversions for arguments and return types.
* The signature at the call site of {@code invokeGeneric} must
* have the same arity as this method handle's {@code type}.
- * The same conversions are allowed on arguments or return values as are supported by
- * by {@link MethodHandles#convertArguments}.
+ *
* If the call site signature exactly matches this method handle's {@code type},
* the call proceeds as if by {@link #invokeExact}.
+ *
+ * Otherwise, the call proceeds as if this method handle were first
+ * adjusted by calling {@link #asType} to adjust this method handle
+ * to the required type, and then the call proceeds as if by
+ * {@link #invokeExact} on the adjusted method handle.
*/
public final native @PolymorphicSignature
+ * The type {@link MethodHandle} is a concrete class whose implementation
+ * hierarchy (if any) may be tightly coupled to the underlying JVM implementation.
+ * It cannot also serve as a base type for user-defined functional APIs.
+ * For this reason, {@code MethodHandle} cannot be subclassed to add new
+ * behavior to method handles. But this interface can be used to provide
+ * a link between a user-defined function and the {@code invokedynamic}
+ * instruction and the method handle API.
+ */
+public interface MethodHandleProvider {
+ /** Produce a method handle which will serve as a behavioral proxy for the current object.
+ * The type and invocation behavior of the proxy method handle are user-defined,
+ * and should have some relation to the intended meaning of the original object itself.
+ *
+ * The current object may have a changeable behavior.
+ * For example, {@link CallSite} has a {@code setTarget} method which changes its invocation.
+ * In such a case, it is incorrect for {@code asMethodHandle} to return
+ * a method handle whose behavior may diverge from that of the current object.
+ * Rather, the returned method handle must stably and permanently access
+ * the behavior of the current object, even if that behavior is changeable.
+ *
+ * The reference identity of the proxy method handle is not guaranteed to
+ * have any particular relation to the reference identity of the object.
+ * In particular, several objects with the same intended meaning could
+ * share a common method handle, or the same object could return different
+ * method handles at different times. In the latter case, the different
+ * method handles should have the same type and invocation behavior,
+ * and be usable from any thread at any time.
+ * In particular, if a MethodHandleProvider is bound to an
+ * The type {@link MethodHandle} itself implements {@code MethodHandleProvider}, and
+ * for this method simply returns {@code this}.
+ */
+ public MethodHandle asMethodHandle();
+
+ /** Produce a method handle of a given type which will serve as a behavioral proxy for the current object.
+ * As for the no-argument version {@link #asMethodHandle()}, the invocation behavior of the
+ * proxy method handle is user-defined. But the type must be the given type,
+ * or else a {@link WrongMethodTypeException} must be thrown.
+ *
+ * If the current object somehow represents a variadic or overloaded behavior,
+ * the method handle returned for a given type might represent only a subset of
+ * the current object's repertoire of behaviors, which correspond to that type.
+ */
+ public MethodHandle asMethodHandle(MethodType type) throws WrongMethodTypeException;
+}
diff --git a/jdk/src/share/classes/java/dyn/MethodHandles.java b/jdk/src/share/classes/java/dyn/MethodHandles.java
index ac35f88e143..b6e83338340 100644
--- a/jdk/src/share/classes/java/dyn/MethodHandles.java
+++ b/jdk/src/share/classes/java/dyn/MethodHandles.java
@@ -25,15 +25,12 @@
package java.dyn;
-import java.lang.reflect.Constructor;
+import java.lang.reflect.*;
import sun.dyn.Access;
import sun.dyn.MemberName;
import sun.dyn.MethodHandleImpl;
import sun.dyn.util.VerifyAccess;
import sun.dyn.util.Wrapper;
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.lang.reflect.Modifier;
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
@@ -81,6 +78,14 @@ public class MethodHandles {
* Return a {@link Lookup lookup object} which is trusted minimally.
* It can only be used to create method handles to
* publicly accessible fields and methods.
+ *
+ * As a matter of pure convention, the {@linkplain Lookup#lookupClass lookup class}
+ * of this lookup object will be {@link java.lang.Object}.
+ *
+ * The lookup class can be changed to any other class {@code C} using an expression of the form
+ * {@linkplain Lookup#in
* In general, the conditions under which a method handle may be
* created for a method {@code M} are exactly as restrictive as the conditions
* under which the lookup class could have compiled a call to {@code M}.
- * At least some of these error conditions are likely to be
- * represented by checked exceptions in the final version of this API.
+ * This rule is applied even if the Java compiler might have created
+ * an wrapper method to access a private method of another class
+ * in the same top-level declaration.
+ * For example, a lookup object created for a nested class {@code C.D}
+ * can access private members within other related classes such as
+ * {@code C}, {@code C.D.E}, or {@code C.B}.
*/
public static final
class Lookup {
/** The class on behalf of whom the lookup is being performed. */
private final Class> lookupClass;
- /** The allowed sorts of members which may be looked up (public, etc.), with STRICT for package. */
+ /** The allowed sorts of members which may be looked up (public, etc.), with STATIC for package. */
private final int allowedModes;
private static final int
PUBLIC = Modifier.PUBLIC,
- PACKAGE = Modifier.STRICT,
+ PACKAGE = Modifier.STATIC,
PROTECTED = Modifier.PROTECTED,
PRIVATE = Modifier.PRIVATE,
ALL_MODES = (PUBLIC | PACKAGE | PROTECTED | PRIVATE),
@@ -155,8 +166,10 @@ public class MethodHandles {
/** Which class is performing the lookup? It is this class against
* which checks are performed for visibility and access permissions.
*
- * This value is null if and only if this lookup was produced
- * by {@link MethodHandles#publicLookup}.
+ * The class implies a maximum level of access permission,
+ * but the permissions may be additionally limited by the bitmask
+ * {@link #lookupModes}, which controls whether non-public members
+ * can be accessed.
*/
public Class> lookupClass() {
return lookupClass;
@@ -168,10 +181,15 @@ public class MethodHandles {
}
/** Which types of members can this lookup object produce?
- * The result is a bit-mask of the modifier bits PUBLIC, PROTECTED, PRIVATE, and STRICT.
- * The modifier bit STRICT stands in for the (non-existent) package protection mode.
+ * The result is a bit-mask of the {@link Modifier} bits
+ * {@linkplain Modifier#PUBLIC PUBLIC (0x01)},
+ * {@linkplain Modifier#PROTECTED PROTECTED (0x02)},
+ * {@linkplain Modifier#PRIVATE PRIVATE (0x04)},
+ * and {@linkplain Modifier#STATIC STATIC (0x08)}.
+ * The modifier bit {@code STATIC} stands in for the package protection mode,
+ * which does not have an explicit modifier bit.
*/
- int lookupModes() {
+ public int lookupModes() {
return allowedModes & ALL_MODES;
}
@@ -621,32 +639,32 @@ public class MethodHandles {
/// Helper methods, all package-private.
- MemberName resolveOrFail(Class> refc, String name, Class> type, boolean isStatic) {
+ MemberName resolveOrFail(Class> refc, String name, Class> type, boolean isStatic) throws NoAccessException {
checkSymbolicClass(refc); // do this before attempting to resolve
int mods = (isStatic ? Modifier.STATIC : 0);
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), true, lookupClassOrNull());
}
- MemberName resolveOrFail(Class> refc, String name, MethodType type, boolean isStatic) {
+ MemberName resolveOrFail(Class> refc, String name, MethodType type, boolean isStatic) throws NoAccessException {
checkSymbolicClass(refc); // do this before attempting to resolve
int mods = (isStatic ? Modifier.STATIC : 0);
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), true, lookupClassOrNull());
}
MemberName resolveOrFail(Class> refc, String name, MethodType type, boolean isStatic,
- boolean searchSupers, Class> specialCaller) {
+ boolean searchSupers, Class> specialCaller) throws NoAccessException {
checkSymbolicClass(refc); // do this before attempting to resolve
int mods = (isStatic ? Modifier.STATIC : 0);
return IMPL_NAMES.resolveOrFail(new MemberName(refc, name, type, mods), searchSupers, specialCaller);
}
- void checkSymbolicClass(Class> refc) {
+ void checkSymbolicClass(Class> refc) throws NoAccessException {
Class> caller = lookupClassOrNull();
if (caller != null && !VerifyAccess.isClassAccessible(refc, caller))
throw newNoAccessException("symbolic reference class is not public", new MemberName(refc), caller);
}
- void checkMethod(Class> refc, MemberName m, boolean wantStatic) {
+ void checkMethod(Class> refc, MemberName m, boolean wantStatic) throws NoAccessException {
String message;
if (m.isConstructor())
message = "expected a method, not a constructor";
@@ -659,7 +677,7 @@ public class MethodHandles {
throw newNoAccessException(message, m, lookupClass());
}
- void checkAccess(Class> refc, MemberName m) {
+ void checkAccess(Class> refc, MemberName m) throws NoAccessException {
int allowedModes = this.allowedModes;
if (allowedModes == TRUSTED) return;
int mods = m.getModifiers();
@@ -695,14 +713,14 @@ public class MethodHandles {
return "member is private to package";
}
- void checkSpecialCaller(Class> specialCaller) {
+ void checkSpecialCaller(Class> specialCaller) throws NoAccessException {
if (allowedModes == TRUSTED) return;
if (!VerifyAccess.isSamePackageMember(specialCaller, lookupClass()))
throw newNoAccessException("no private access for invokespecial",
new MemberName(specialCaller), lookupClass());
}
- MethodHandle restrictProtectedReceiver(MemberName method, MethodHandle mh) {
+ MethodHandle restrictProtectedReceiver(MemberName method, MethodHandle mh) throws NoAccessException {
// The accessing class only has the right to use a protected member
// on itself or a subclass. Enforce that restriction, from JVMS 5.4.4, etc.
if (!method.isProtected() || method.isStatic()
@@ -712,7 +730,7 @@ public class MethodHandles {
else
return restrictReceiver(method, mh, lookupClass());
}
- MethodHandle restrictReceiver(MemberName method, MethodHandle mh, Class> caller) {
+ MethodHandle restrictReceiver(MemberName method, MethodHandle mh, Class> caller) throws NoAccessException {
assert(!method.isStatic());
Class> defc = method.getDeclaringClass(); // receiver type of mh is too wide
if (defc.isInterface() || !defc.isAssignableFrom(caller)) {
@@ -898,11 +916,16 @@ public class MethodHandles {
* @return a method handle which always invokes the call site's target
*/
public static
- MethodHandle dynamicInvoker(CallSite site) {
+ MethodHandle dynamicInvoker(CallSite site) throws NoAccessException {
MethodHandle getCSTarget = GET_TARGET;
- if (getCSTarget == null)
- GET_TARGET = getCSTarget = Lookup.IMPL_LOOKUP.
- findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class));
+ if (getCSTarget == null) {
+ try {
+ GET_TARGET = getCSTarget = Lookup.IMPL_LOOKUP.
+ findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class));
+ } catch (NoAccessException ex) {
+ throw new InternalError();
+ }
+ }
MethodHandle getTarget = MethodHandleImpl.bindReceiver(IMPL_TOKEN, getCSTarget, site);
MethodHandle invoker = exactInvoker(site.type());
return foldArguments(invoker, getTarget);
@@ -1260,17 +1283,20 @@ public class MethodHandles {
*
* Example:
*
+ * The resulting instance of the required SAM type will respond to
+ * invocation of the SAM type's single abstract method by calling
+ * the given {@code target} on the incoming arguments,
+ * and returning or throwing whatever the {@code target}
+ * returns or throws. The invocation will be as if by
+ * {@code target.invokeExact}.
+ *
+ * The method handle may throw an undeclared exception,
+ * which means any checked exception (or other checked throwable)
+ * not declared by the SAM type's single abstract method.
+ * If this happens, the throwable will be wrapped in an instance
+ * of {@link UndeclaredThrowableException} and thrown in that
+ * wrapped form.
+ *
+ * The wrapper instance is guaranteed to be of a non-public
+ * implementation class C in a package containing no classes
+ * or methods except system-defined classes and methods.
+ * The implementation class C will have no public supertypes
+ * or public methods beyond the following:
+ *
+ * No stable mapping is promised between the SAM type and
+ * the implementation class C. Over time, several implementation
+ * classes might be used for the same SAM type.
+ *
+ * This method is not guaranteed to return a distinct
+ * wrapper object for each separate call. If the JVM is able
+ * to prove that a wrapper has already been created for a given
+ * method handle, or for another method handle with the
+ * same behavior, the JVM may return that wrapper in place of
+ * a new wrapper.
+ * @param target the method handle to invoke from the wrapper
+ * @param samType the desired type of the wrapper, a SAM type
+ * @return a correctly-typed wrapper for the given {@code target}
+ * @throws IllegalArgumentException if the {@code target} throws
+ * an undeclared exception
+ */
+ // ISSUE: Should we delegate equals/hashCode to the targets?
+ // Not useful unless there is a stable equals/hashCode behavior
+ // for MethodHandle, and for MethodHandleProvider.asMethodHandle.
+ public static
+
* The structure is a return type accompanied by any number of parameter types.
- * The types (primitive, void, and reference) are represented by Class objects.
+ * The types (primitive, {@code void}, and reference) are represented by {@link Class} objects.
+ * (For ease of exposition, we treat {@code void} as if it were a type.
+ * In fact, it denotes the absence of a return type.)
*
- * All instances of
* This type can be created only by factory methods.
* All factory methods may cache values, though caching is not guaranteed.
*
- * Note: Like classes and strings, method types can be represented directly
- * as constants to be loaded by {@code ldc} bytecodes.
+ * {@code MethodType} objects are sometimes derived from bytecode instructions
+ * such as {@code invokedynamic}, specifically from the type descriptor strings associated
+ * with the instructions in a class file's constant pool.
+ * When this occurs, any classes named in the descriptor strings must be loaded.
+ * (But they need not be initialized.)
+ * This loading may occur at any time before the {@code MethodType} object is first derived.
+ *
+ * Like classes and strings, method types can be represented directly
+ * in a class file's constant pool as constants to be loaded by {@code ldc} bytecodes.
+ * Loading such a constant causes its component classes to be loaded as necessary.
* @author John Rose, JSR 292 EG
*/
public final
-class MethodType {
+class MethodType implements java.lang.reflect.Type {
private final Class> rtype;
private final Class>[] ptypes;
private MethodTypeForm form; // erased form, plus cached data about primitives
@@ -636,11 +649,11 @@ class MethodType {
/** Convenience method for {@link #methodType(java.lang.Class, java.lang.Class[])}.
* Find or create an instance of the given method type.
- * Any class or interface name embedded in the signature string
+ * Any class or interface name embedded in the descriptor string
* will be resolved by calling {@link ClassLoader#loadClass(java.lang.String)}
* on the given loader (or if it is null, on the system class loader).
*
- * Note that it is possible to build method types which cannot be
+ * Note that it is possible to encounter method types which cannot be
* constructed by this method, because their component types are
* not all reachable from a common class loader.
*
@@ -662,8 +675,11 @@ class MethodType {
}
/**
- * Create a bytecode signature representation of the type.
- * Note that this is not a strict inverse of
+ * Create a bytecode descriptor representation of the method type.
+ *
+ * Note that this is not a strict inverse of {@link #fromMethodDescriptorString}.
+ * Two distinct classes which share a common name but have different class loaders
+ * will appear identical when viewed within descriptor strings.
*
* This method is included for the benfit of applications that must
* generate bytecodes that process method handles and invokedynamic.
diff --git a/jdk/src/share/classes/java/dyn/NoAccessException.java b/jdk/src/share/classes/java/dyn/NoAccessException.java
index 6b2fbcebcae..89c1a52af2a 100644
--- a/jdk/src/share/classes/java/dyn/NoAccessException.java
+++ b/jdk/src/share/classes/java/dyn/NoAccessException.java
@@ -37,7 +37,7 @@ package java.dyn;
* @author John Rose, JSR 292 EG
* @since 1.7
*/
-public class NoAccessException extends RuntimeException {
+public class NoAccessException extends ReflectiveOperationException {
private static final long serialVersionUID = 292L;
/**
diff --git a/jdk/src/share/classes/java/dyn/package-info.java b/jdk/src/share/classes/java/dyn/package-info.java
index 7a285b75d22..37555be14b4 100644
--- a/jdk/src/share/classes/java/dyn/package-info.java
+++ b/jdk/src/share/classes/java/dyn/package-info.java
@@ -40,20 +40,18 @@
* The JVM links any such call (regardless of signature) to a dynamically
* typed method handle invocation. In the case of {@code invokeGeneric},
* argument and return value conversions are applied.
+ *
+ * Each instance of an {@code invokedynamic} instruction is called a dynamic call site.
+ * Multiple instances of an {@code invokedynamic} instruction can share a single
+ * {@code CONSTANT_InvokeDynamic} entry.
+ * In any case, distinct call sites always have distinct linkage state.
+ *
+ * Moreover, for the purpose of distinguishing dynamic call sites,
+ * the JVM is allowed (but not required) to make internal copies
+ * of {@code invokedynamic} instructions, each one
+ * constituting a separate dynamic call site with its own linkage state.
+ * Such copying, if it occurs, cannot be observed except indirectly via
+ * execution of bootstrap methods and target methods.
+ *
+ * A dynamic call site is originally in an unlinked state. In this state, there is
+ * no target method for the call site to invoke.
+ * A dynamic call site is linked by means of a bootstrap method,
+ * as described below.
+ *
+ * (Historic Note: Some older JVMs may allow the index of a {@code CONSTANT_NameAndType}
+ * instead of a {@code CONSTANT_InvokeDynamic}. In earlier, obsolete versions of this API, the
+ * bootstrap method was specified dynamically, in a per-class basis, during class initialization.)
+ *
+ *
+ * The JVM will ensure that on first
+ * execution of an {@code ldc} instruction for this entry, a {@link java.dyn.MethodType MethodType}
* will be created which represents the signature.
* Any classes mentioned in the {@code MethodType} will be loaded if necessary,
* but not initialized.
@@ -86,12 +116,15 @@
*
* The JVM will ensure that on first execution of an {@code ldc} instruction
- * for this entry, a {@link java.dyn.MethodHandle} will be created which represents
+ * for this entry, a {@link java.dyn.MethodHandle MethodHandle} will be created which represents
* the field or method reference, according to the specific mode implied by the subtag.
*
* As with {@code CONSTANT_Class} and {@code CONSTANT_MethodType} constants,
@@ -126,6 +159,129 @@
* Method handles for subtags {@code REF_getStatic}, {@code REF_putStatic}, and {@code REF_invokeStatic}
* may force class initialization on their first invocation, just like the corresponding bytecodes.
*
+ *
+ * Each {@code invokedynamic} instruction statically specifies its own
+ * bootstrap method as a constant pool reference.
+ * The constant pool reference also specifies the call site's name and type signature,
+ * just like {@code invokevirtual} and the other invoke instructions.
+ *
+ * Linking starts with resolving the constant pool entry for the
+ * bootstrap method, and resolving a {@link java.dyn.MethodType MethodType} object for
+ * the type signature of the dynamic call site.
+ * This resolution process may trigger class loading.
+ * It may therefore throw an error if a class fails to load.
+ * This error becomes the abnormal termination of the dynamic
+ * call site execution.
+ * Linkage does not trigger class initialization.
+ *
+ * Next, the bootstrap method call is started, with four values being stacked:
+ *
+ * After resolution, the linkage process may fail in a variety of ways.
+ * All failures are reported by an {@link java.dyn.InvokeDynamicBootstrapError InvokeDynamicBootstrapError},
+ * which is thrown as the abnormal termination of the dynamic call
+ * site execution.
+ * The following circumstances will cause this:
+ *
+ * If there are several such threads, the JVM picks one thread
+ * and runs the bootstrap method while the others wait for the
+ * invocation to terminate normally or abnormally.
+ *
+ * After a bootstrap method is called and a method handle target
+ * successfully extracted, the JVM attempts to link the instruction
+ * being executed to the target method handle.
+ * This may fail if there has been intervening linkage
+ * or invalidation event for the same instruction.
+ * If such a failure occurs, the dynamic call site must be
+ * re-executed from the beginning, either re-linking it
+ * (if it has been invalidated) or invoking the target
+ * (if it the instruction has been linked by some other means).
+ *
+ * If the instruction is linked successfully, the target method
+ * handle is invoked to complete the instruction execution.
+ * The state of linkage continues until the method containing the
+ * dynamic call site is garbage collected, or the dynamic call site
+ * is invalidated by an explicit request,
+ * such as {@link java.dyn.Linkage#invalidateCallerClass Linkage.invalidateCallerClass}.
+ *
+ * In an application which requires dynamic call sites with individually
+ * mutable behaviors, their bootstrap methods should produce distinct
+ * {@link java.dyn.CallSite CallSite} objects, one for each linkage request.
+ *
+ * If a class containing {@code invokedynamic} instructions
+ * is {@linkplain java.dyn.Linkage#invalidateCallerClass(Class) invalidated},
+ * subsequent execution of those {@code invokedynamic} instructions
+ * will require linking.
+ * It is as if they had never been executed in the first place.
+ * (However, invalidation does not cause constant pool entries to be
+ * resolved a second time.)
+ *
+ * Invalidation events and bootstrap method calls for a particular
+ * dynamic call site are globally ordered relative to each other.
+ * When an invokedynamic instruction is invalidated, if there is
+ * simultaneously a bootstrap method invocation in process
+ * (in the same thread or a different thread), the result
+ * eventually returned must not be used to link the call site.
+ * Put another way, when a call site is invalidated, its
+ * subsequent linkage (if any) must be performed by a bootstrap method
+ * call initiated after the invalidation occurred.
+ *
+ * If several threads simultaneously execute a bootstrap method for a single dynamic
+ * call site, the JVM must choose one target object and installs it visibly to
+ * all threads. Any other bootstrap method calls are allowed to complete, but their
+ * results are ignored, and their dynamic call site invocations proceed with the originally
+ * chosen target object.
+ *
+ * The JVM is free to duplicate dynamic call sites.
+ * This means that, even if a class contains just one {@code invokedynamic}
+ * instruction, its bootstrap method may be executed several times,
+ * once for each duplicate. Thus, bootstrap method code should not
+ * assume an exclusive one-to-one correspondence between particular occurrences
+ * of {@code invokedynamic} bytecodes in class files and linkage events.
+ *
+ * In principle, each individual execution of an {@code invokedynamic}
+ * instruction could be deemed (by a conforming implementation) to be a separate
+ * duplicate, requiring its own execution of the bootstrap method.
+ * However, implementations are expected to perform code duplication
+ * (if at all) in order to improve performance, not make it worse.
+ *
* @author John Rose, JSR 292 EG
*/
diff --git a/jdk/src/share/classes/sun/dyn/BoundMethodHandle.java b/jdk/src/share/classes/sun/dyn/BoundMethodHandle.java
index 5152187229e..f2ae32efbc9 100644
--- a/jdk/src/share/classes/sun/dyn/BoundMethodHandle.java
+++ b/jdk/src/share/classes/sun/dyn/BoundMethodHandle.java
@@ -48,8 +48,6 @@ public class BoundMethodHandle extends MethodHandle {
private static final MemberName.Factory IMPL_NAMES = MemberName.getFactory(IMPL_TOKEN);
// Constructors in this class *must* be package scoped or private.
- // Exception: JavaMethodHandle constructors are protected.
- // (The link between JMH and BMH is temporary.)
/** Bind a direct MH to its receiver (or first ref. argument).
* The JVM will pre-dispatch the MH if it is not already static.
@@ -122,55 +120,6 @@ public class BoundMethodHandle extends MethodHandle {
assert(this instanceof JavaMethodHandle);
}
- /** Initialize the current object as a Java method handle.
- */
- protected BoundMethodHandle(String entryPointName, MethodType type, boolean matchArity) {
- super(Access.TOKEN, null);
- MethodHandle entryPoint
- = findJavaMethodHandleEntryPoint(this.getClass(),
- entryPointName, type, matchArity);
- MethodHandleImpl.initType(this, entryPoint.type().dropParameterTypes(0, 1));
- this.argument = this; // kludge; get rid of
- this.vmargslot = this.type().parameterSlotDepth(0);
- initTarget(entryPoint, 0);
- assert(this instanceof JavaMethodHandle);
- }
-
- private static
- MethodHandle findJavaMethodHandleEntryPoint(Class> caller,
- String name,
- MethodType type,
- boolean matchArity) {
- if (matchArity) type.getClass(); // elicit NPE
- List
diff --git a/jdk/src/share/classes/sun/dyn/CallSiteImpl.java b/jdk/src/share/classes/sun/dyn/CallSiteImpl.java
index 2e02e74ebd0..f7c2d7099a7 100644
--- a/jdk/src/share/classes/sun/dyn/CallSiteImpl.java
+++ b/jdk/src/share/classes/sun/dyn/CallSiteImpl.java
@@ -26,6 +26,7 @@
package sun.dyn;
import java.dyn.*;
+import static sun.dyn.MemberName.uncaughtException;
/**
* Parts of CallSite known to the JVM.
@@ -49,18 +50,21 @@ public class CallSiteImpl {
}
CallSite site;
try {
- if (bootstrapMethod.type().parameterCount() == 3)
- site = bootstrapMethod.
- * The method is specified by name and type, as if via this expression:
- * {@code MethodHandles.lookup().findVirtual(this.getClass(), name, type)}.
- * The class defining the method might be an anonymous inner class.
- *
- * The method handle type of {@code this} (i.e, the fully constructed object)
- * will be the given method handle type.
- * A call to {@code this} will invoke the selected method.
- * The receiver argument will be bound to {@code this} on every method
- * handle invocation.
- *
- * Rationale:
- * Although this constructor may seem to be a mere luxury,
- * it is not subsumed by the more general constructor which
- * takes any {@code MethodHandle} as the entry point argument.
- * In order to convert an entry point name to a method handle,
- * the self-class of the object is required (in order to do
- * the lookup). The self-class, in turn, is generally not
- * available at the time of the constructor invocation,
- * due to the rules of Java and the JVM verifier.
- * One cannot call {@code this.getClass()}, because
- * the value of {@code this} is inaccessible at the point
- * of the constructor call. (Changing this would require
- * change to the Java language, verifiers, and compilers.)
- * In particular, this constructor allows {@code JavaMethodHandle}s
- * to be created in combination with the anonymous inner class syntax.
- * @param entryPointName the name of the entry point method
- * @param type (optional) the desired type of the method handle
- */
- protected JavaMethodHandle(String entryPointName, MethodType type) {
- super(entryPointName, type, true);
-
- }
-
- /**
- * Create a method handle whose entry point is a non-static method
- * visible in the exact (most specific) class of
- * the newly constructed object.
- *
- * The method is specified only by name.
- * There must be exactly one method of that name visible in the object class,
- * either inherited or locally declared.
- * (That is, the method must not be overloaded.)
- *
- * The method handle type of {@code this} (i.e, the fully constructed object)
- * will be the same as the type of the selected non-static method.
- * The receiver argument will be bound to {@code this} on every method
- * handle invocation.
- * ISSUE: This signature wildcarding feature does not correspond to
- * any MethodHandles.Lookup API element. Can we eliminate it?
- * Alternatively, it is useful for naming non-overloaded methods.
- * Shall we make type arguments optional in the Lookup methods,
- * throwing an error in cases of ambiguity?
- *
- * For this method's rationale, see the documentation
- * for {@link #JavaMethodHandle(String,MethodType)}.
- * @param entryPointName the name of the entry point method
- */
- protected JavaMethodHandle(String entryPointName) {
- super(entryPointName, (MethodType) null, false);
- }
}
diff --git a/jdk/src/share/classes/sun/dyn/MemberName.java b/jdk/src/share/classes/sun/dyn/MemberName.java
index db21f2b4121..f45d1603ebf 100644
--- a/jdk/src/share/classes/sun/dyn/MemberName.java
+++ b/jdk/src/share/classes/sun/dyn/MemberName.java
@@ -521,6 +521,11 @@ public final class MemberName implements Member, Cloneable {
if (lookupClass != null) message += ", from " + lookupClass.getName();
return new NoAccessException(message);
}
+ public static Error uncaughtException(Exception ex) {
+ Error err = new InternalError("uncaught exception");
+ err.initCause(ex);
+ return err;
+ }
/** Actually making a query requires an access check. */
public static Factory getFactory(Access token) {
@@ -641,7 +646,7 @@ public final class MemberName implements Member, Cloneable {
* If lookup fails or access is not permitted, a {@linkplain NoAccessException} is thrown.
* Otherwise a fresh copy of the given member is returned, with modifier bits filled in.
*/
- public MemberName resolveOrFail(MemberName m, boolean searchSupers, Class> lookupClass) {
+ public MemberName resolveOrFail(MemberName m, boolean searchSupers, Class> lookupClass) throws NoAccessException {
MemberName result = resolveOrNull(m, searchSupers, lookupClass);
if (result != null)
return result;
diff --git a/jdk/src/share/classes/sun/dyn/MethodHandleImpl.java b/jdk/src/share/classes/sun/dyn/MethodHandleImpl.java
index 25dc470d061..caa96b90d69 100644
--- a/jdk/src/share/classes/sun/dyn/MethodHandleImpl.java
+++ b/jdk/src/share/classes/sun/dyn/MethodHandleImpl.java
@@ -25,11 +25,8 @@
package sun.dyn;
-import java.dyn.JavaMethodHandle;
-import java.dyn.MethodHandle;
-import java.dyn.MethodHandles;
+import java.dyn.*;
import java.dyn.MethodHandles.Lookup;
-import java.dyn.MethodType;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.dyn.util.VerifyType;
@@ -46,6 +43,7 @@ import sun.dyn.util.Wrapper;
import sun.misc.Unsafe;
import static sun.dyn.MemberName.newIllegalArgumentException;
import static sun.dyn.MemberName.newNoAccessException;
+import static sun.dyn.MemberName.uncaughtException;
/**
* Base class for method handles, containing JVM-specific fields and logic.
@@ -173,7 +171,7 @@ public abstract class MethodHandleImpl {
*/
public static
MethodHandle findMethod(Access token, MemberName method,
- boolean doDispatch, Class> lookupClass) {
+ boolean doDispatch, Class> lookupClass) throws NoAccessException {
Access.check(token); // only trusted calls
MethodType mtype = method.getMethodType();
if (!method.isStatic()) {
@@ -320,7 +318,7 @@ public abstract class MethodHandleImpl {
try {
VARARGS_INVOKE = IMPL_LOOKUP.findVirtual(AllocateObject.class, "invoke_V", MethodType.genericMethodType(0, true));
} catch (NoAccessException ex) {
- throw new InternalError("");
+ throw uncaughtException(ex);
}
}
// Corresponding generic constructor types:
@@ -416,9 +414,7 @@ public abstract class MethodHandleImpl {
f = c.getDeclaredField(field.getName());
return unsafe.staticFieldBase(f);
} catch (Exception ee) {
- Error e = new InternalError();
- e.initCause(ee);
- throw e;
+ throw uncaughtException(ee);
}
}
@@ -473,10 +469,8 @@ public abstract class MethodHandleImpl {
MethodHandle mh;
try {
mh = IMPL_LOOKUP.findVirtual(FieldAccessor.class, name, type);
- } catch (NoAccessException ee) {
- Error e = new InternalError("name,type="+name+type);
- e.initCause(ee);
- throw e;
+ } catch (NoAccessException ex) {
+ throw uncaughtException(ex);
}
if (evclass != vclass || (!isStatic && ecclass != cclass)) {
MethodType strongType = FieldAccessor.ftype(cclass, vclass, isSetter, isStatic);
@@ -543,10 +537,8 @@ public abstract class MethodHandleImpl {
MethodHandle mh;
try {
mh = IMPL_LOOKUP.findStatic(FieldAccessor.class, name, type);
- } catch (NoAccessException ee) {
- Error e = new InternalError("name,type="+name+type);
- e.initCause(ee);
- throw e;
+ } catch (NoAccessException ex) {
+ throw uncaughtException(ex);
}
if (caclass != null) {
MethodType strongType = FieldAccessor.atype(caclass, isSetter);
@@ -1031,7 +1023,7 @@ public abstract class MethodHandleImpl {
try {
VARARGS_INVOKE = IMPL_LOOKUP.findVirtual(GuardWithTest.class, "invoke_V", MethodType.genericMethodType(0, true));
} catch (NoAccessException ex) {
- throw new InternalError("");
+ throw uncaughtException(ex);
}
}
}
@@ -1167,7 +1159,7 @@ public abstract class MethodHandleImpl {
try {
VARARGS_INVOKE = IMPL_LOOKUP.findVirtual(GuardWithCatch.class, "invoke_V", MethodType.genericMethodType(0, true));
} catch (NoAccessException ex) {
- throw new InternalError("");
+ throw uncaughtException(ex);
}
}
}
@@ -1207,9 +1199,16 @@ public abstract class MethodHandleImpl {
return AdapterMethodHandle.makeRetypeRaw(token, type, THROW_EXCEPTION);
}
- static final MethodHandle THROW_EXCEPTION
+ static final MethodHandle THROW_EXCEPTION;
+ static {
+ try {
+ THROW_EXCEPTION
= IMPL_LOOKUP.findStatic(MethodHandleImpl.class, "throwException",
MethodType.methodType(Empty.class, Throwable.class));
+ } catch (NoAccessException ex) {
+ throw new RuntimeException(ex);
+ }
+ }
static
+
- * Object x; String s; int i;
- * x = InvokeDynamic.greet("world"); // greet(Ljava/lang/String;)Ljava/lang/Object;
- * s = InvokeDynamic.<String>hail(x); // hail(Ljava/lang/Object;)Ljava/lang/String;
- * InvokeDynamic.<void>cogito(); // cogito()V
- * i = InvokeDynamic.<int>#"op:+"(2, 3); // "op:+"(II)I
- *
* Each of the above calls generates a single invokedynamic instruction
* with the name-and-type descriptors indicated in the comments.
+ *
+@BootstrapMethod(value=Here.class, name="bootstrapDynamic")
+static void example() throws Throwable {
+ Object x; String s; int i;
+ x = InvokeDynamic.greet("world"); // greet(Ljava/lang/String;)Ljava/lang/Object;
+ s = (String) InvokeDynamic.hail(x); // hail(Ljava/lang/Object;)Ljava/lang/String;
+ InvokeDynamic.cogito(); // cogito()V
+ i = (int) InvokeDynamic.#"op:+"(2, 3); // "op:+"(II)I
+}
+static MethodHandle bootstrapDynamic(Class caller, String name, MethodType type) { ... }
+
- *
- *
- * registerBootstrapMethod.{class name}
- * Specifying a bootstrap method for {@code invokedynamic} instructions within a class of the given name
- * An attacker could attempt to attach a bootstrap method to a class which
- * has just been loaded, thus gaining control of its {@code invokedynamic} calls.
- *
*
*
+ * invalidateAll
* Force the relinking of invokedynamic call sites everywhere.
* This could allow an attacker to slow down the system,
@@ -73,8 +67,9 @@ import java.util.StringTokenizer;
* See {@code invalidateAll}.
* invokedynamic
+ * call site, the proxy method handle extracted at the time of binding
+ * will be used for an unlimited time, until the call site is rebound.
+ * publicLookup().in(C.class)}.
+ * Since all classes have equal access to public names,
+ * such a change would confer no new access rights.
*/
public static Lookup publicLookup() {
return Lookup.PUBLIC_LOOKUP;
@@ -90,9 +95,10 @@ public class MethodHandles {
* A lookup object is a factory for creating method handles,
* when the creation requires access checking.
* Method handles do not perform
- * access checks when they are called; this is a major difference
+ * access checks when they are called, but rather when they are created.
+ * (This is a major difference
* from reflective {@link Method}, which performs access checking
- * against every caller, on every call.
+ * against every caller, on every call.)
* Therefore, method handle access
* restrictions must be enforced when a method handle is created.
* The caller class against which those restrictions are enforced
@@ -107,7 +113,7 @@ public class MethodHandles {
* It may then use this factory to create method handles on
* all of its methods, including private ones.
* It may also delegate the lookup (e.g., to a metaobject protocol)
- * by passing the {@code Lookup} object to other code.
+ * by passing the lookup object to other code.
* If this other code creates method handles, they will be access
* checked against the original lookup class, and not with any higher
* privileges.
@@ -125,23 +131,28 @@ public class MethodHandles {
* It can also fail if a security manager is installed and refuses
* access. In any of these cases, an exception will be
* thrown from the attempted lookup.
+ *
* @param target the method handle to invoke after the argument is dropped
* @param valueTypes the type(s) of the argument to drop
@@ -1562,4 +1588,107 @@ public class MethodHandles {
MethodHandle throwException(Class> returnType, Class extends Throwable> exType) {
return MethodHandleImpl.throwException(IMPL_TOKEN, MethodType.methodType(returnType, exType));
}
+
+ /**
+ * Produce a wrapper instance of the given "SAM" type which redirects its calls to the given method handle.
+ * A SAM type is a type which declares a single abstract method.
+ * Additionally, it must have either no constructor (as an interface)
+ * or have a public or protected constructor of zero arguments (as a class).
+ *
- * MethodHandle cat = MethodHandles.lookup().
- * findVirtual(String.class, "concat", String.class, String.class);
- * System.out.println(cat.<String>invokeExact("x", "y")); // xy
+ * import static java.dyn.MethodHandles.*;
+ * import static java.dyn.MethodType.*;
+ * ...
+ * MethodHandle cat = lookup().findVirtual(String.class,
+ * "concat", methodType(String.class, String.class));
+ * System.out.println((String) cat.invokeExact("x", "y")); // xy
* MethodHandle d0 = dropArguments(cat, 0, String.class);
- * System.out.println(d0.<String>invokeExact("x", "y", "z")); // xy
+ * System.out.println((String) d0.invokeExact("x", "y", "z")); // yz
* MethodHandle d1 = dropArguments(cat, 1, String.class);
- * System.out.println(d1.<String>invokeExact("x", "y", "z")); // xz
+ * System.out.println((String) d1.invokeExact("x", "y", "z")); // xz
* MethodHandle d2 = dropArguments(cat, 2, String.class);
- * System.out.println(d2.<String>invokeExact("x", "y", "z")); // yz
- * MethodHandle d12 = dropArguments(cat, 1, String.class, String.class);
- * System.out.println(d12.<String>invokeExact("w", "x", "y", "z")); // wz
+ * System.out.println((String) d2.invokeExact("x", "y", "z")); // xy
+ * MethodHandle d12 = dropArguments(cat, 1, int.class, boolean.class);
+ * System.out.println((String) d12.invokeExact("x", 12, true, "z")); // xz
*
+ *
+ * MethodType are immutable.
+ * All instances of {@code MethodType} are immutable.
* Two instances are completely interchangeable if they compare equal.
* Equality depends on pairwise correspondence of the return and parameter types and on nothing else.
* Corresponding JVM bytecode format changes
@@ -65,18 +63,50 @@
* The first byte is the opcode 186 (hexadecimal {@code BA}).
* The next two bytes are a constant pool index (in the same format as for the other {@code invoke} instructions).
* The final two bytes are reserved for future use and required to be zero.
- * The constant pool reference is to a entry with tag {@code CONSTANT_NameAndType}
- * (decimal 12). It is thus not a method reference of any sort, but merely
- * the method name, argument types, and return type of the dynamic call site.
- * (TBD: The EG is discussing the possibility of a special constant pool entry type,
- * so that other information may be added, such as a per-instruction bootstrap
- * method and/or annotations.)
+ * The constant pool reference of an {@code invokedynamic} instruction is to a entry
+ * with tag {@code CONSTANT_InvokeDynamic} (decimal 17). See below for its format.
+ * The entry specifies the bootstrap method (a {@link java.dyn.MethodHandle MethodHandle} constant),
+ * the dynamic invocation name, and the argument types and return type of the call.
+ * constant pool entries for {@code invokedynamic} instructions
+ * If a constant pool entry has the tag {@code CONSTANT_InvokeDynamic} (decimal 17),
+ * it must contain exactly four more bytes.
+ * The first two bytes after the tag must be an index to a {@code CONSTANT_MethodHandle}
+ * entry, and the second two bytes must be an index to a {@code CONSTANT_NameAndType}.
+ * The first index specifies a bootstrap method used by the associated dynamic call sites.
+ * The second index specifies the method name, argument types, and return type of the dynamic call site.
+ * The structure of such an entry is therefore analogous to a {@code CONSTANT_Methodref},
+ * except that the {@code CONSTANT_Class} reference in a {@code CONSTANT_Methodref} entry
+ * is replaced by a bootstrap method reference.
*
* constant pool entries for {@code MethodType}s
* If a constant pool entry has the tag {@code CONSTANT_MethodType} (decimal 16),
- * it must contain exactly two more bytes, which are an index to a {@code CONSTANT_Utf8}
- * entry which represents a method type signature. The JVM will ensure that on first
- * execution of an {@code ldc} instruction for this entry, a {@link java.dyn.MethodType}
+ * it must contain exactly two more bytes, which must be an index to a {@code CONSTANT_Utf8}
+ * entry which represents a method type signature.
+ * constant pool entries for {@code MethodHandle}s
* If a constant pool entry has the tag {@code CONSTANT_MethodHandle} (decimal 15),
* it must contain exactly three more bytes. The first byte after the tag is a subtag
- * value in the range 1 through 9, and the last two are an index to a
+ * value which must be in the range 1 through 9, and the last two must be an index to a
* {@code CONSTANT_Fieldref}, {@code CONSTANT_Methodref}, or
* {@code CONSTANT_InterfaceMethodref} entry which represents a field or method
* for which a method handle is to be created.
+ * Furthermore, the subtag value and the type of the constant index value
+ * must agree according to the table below.
+ * Bootstrap Methods
+ * Before the JVM can execute a dynamic call site (an {@code invokedynamic} instruction),
+ * the call site must first be linked.
+ * Linking is accomplished by calling a bootstrap method
+ * which is given the static information content of the call site,
+ * and which must produce a {@link java.dyn.MethodHandle method handle}
+ * that gives the behavior of the call site.
+ *
+ *
+ * The method handle is then applied to the other values as if by
+ * {@linkplain java.dyn.MethodHandle#invokeGeneric the invokeGeneric method}.
+ * The returned result must be a {@link java.dyn.CallSite CallSite}, a {@link java.dyn.MethodHandle MethodHandle},
+ * or another {@link java.dyn.MethodHandleProvider MethodHandleProvider} value.
+ * The method {@linkplain java.dyn.MethodHandleProvider#asMethodHandle asMethodHandle}
+ * is then called on the returned value. The result of that second
+ * call is the {@code MethodHandle} which becomes the
+ * permanent binding for the dynamic call site.
+ * That method handle's type must be exactly equal to the type
+ * derived from the dynamic call site signature and passed to
+ * the bootstrap method.
+ *
+ *
+ * timing of linkage
+ * A dynamic call site is linked just before its first execution.
+ * The bootstrap method call implementing the linkage occurs within
+ * a thread that is attempting a first execution.
+ *