1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/rt/emul/compact/src/main/java/java/lang/invoke/CallSite.java Tue Jan 17 07:04:06 2017 +0100
1.3 @@ -0,0 +1,191 @@
1.4 +/*
1.5 + * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1.7 + *
1.8 + * This code is free software; you can redistribute it and/or modify it
1.9 + * under the terms of the GNU General Public License version 2 only, as
1.10 + * published by the Free Software Foundation. Oracle designates this
1.11 + * particular file as subject to the "Classpath" exception as provided
1.12 + * by Oracle in the LICENSE file that accompanied this code.
1.13 + *
1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1.17 + * version 2 for more details (a copy is included in the LICENSE file that
1.18 + * accompanied this code).
1.19 + *
1.20 + * You should have received a copy of the GNU General Public License version
1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1.23 + *
1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1.25 + * or visit www.oracle.com if you need additional information or have any
1.26 + * questions.
1.27 + */
1.28 +
1.29 +package java.lang.invoke;
1.30 +
1.31 +/**
1.32 + * A {@code CallSite} is a holder for a variable {@link MethodHandle},
1.33 + * which is called its {@code target}.
1.34 + * An {@code invokedynamic} instruction linked to a {@code CallSite} delegates
1.35 + * all calls to the site's current target.
1.36 + * A {@code CallSite} may be associated with several {@code invokedynamic}
1.37 + * instructions, or it may be "free floating", associated with none.
1.38 + * In any case, it may be invoked through an associated method handle
1.39 + * called its {@linkplain #dynamicInvoker dynamic invoker}.
1.40 + * <p>
1.41 + * {@code CallSite} is an abstract class which does not allow
1.42 + * direct subclassing by users. It has three immediate,
1.43 + * concrete subclasses that may be either instantiated or subclassed.
1.44 + * <ul>
1.45 + * <li>If a mutable target is not required, an {@code invokedynamic} instruction
1.46 + * may be permanently bound by means of a {@linkplain ConstantCallSite constant call site}.
1.47 + * <li>If a mutable target is required which has volatile variable semantics,
1.48 + * because updates to the target must be immediately and reliably witnessed by other threads,
1.49 + * a {@linkplain VolatileCallSite volatile call site} may be used.
1.50 + * <li>Otherwise, if a mutable target is required,
1.51 + * a {@linkplain MutableCallSite mutable call site} may be used.
1.52 + * </ul>
1.53 + * <p>
1.54 + * A non-constant call site may be <em>relinked</em> by changing its target.
1.55 + * The new target must have the same {@linkplain MethodHandle#type() type}
1.56 + * as the previous target.
1.57 + * Thus, though a call site can be relinked to a series of
1.58 + * successive targets, it cannot change its type.
1.59 + * <p>
1.60 + * Here is a sample use of call sites and bootstrap methods which links every
1.61 + * dynamic call site to print its arguments:
1.62 +<blockquote><pre>{@code
1.63 +static void test() throws Throwable {
1.64 + // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION
1.65 + InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14);
1.66 +}
1.67 +private static void printArgs(Object... args) {
1.68 + System.out.println(java.util.Arrays.deepToString(args));
1.69 +}
1.70 +private static final MethodHandle printArgs;
1.71 +static {
1.72 + MethodHandles.Lookup lookup = MethodHandles.lookup();
1.73 + Class thisClass = lookup.lookupClass(); // (who am I?)
1.74 + printArgs = lookup.findStatic(thisClass,
1.75 + "printArgs", MethodType.methodType(void.class, Object[].class));
1.76 +}
1.77 +private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
1.78 + // ignore caller and name, but match the type:
1.79 + return new ConstantCallSite(printArgs.asType(type));
1.80 +}
1.81 +}</pre></blockquote>
1.82 + * @author John Rose, JSR 292 EG
1.83 + */
1.84 +abstract
1.85 +public class CallSite {
1.86 + // The actual payload of this call site:
1.87 + /*package-private*/
1.88 + MethodHandle target; // Note: This field is known to the JVM. Do not change.
1.89 +
1.90 + /**
1.91 + * Make a blank call site object with the given method type.
1.92 + * An initial target method is supplied which will throw
1.93 + * an {@link IllegalStateException} if called.
1.94 + * <p>
1.95 + * Before this {@code CallSite} object is returned from a bootstrap method,
1.96 + * it is usually provided with a more useful target method,
1.97 + * via a call to {@link CallSite#setTarget(MethodHandle) setTarget}.
1.98 + * @throws NullPointerException if the proposed type is null
1.99 + */
1.100 + /*package-private*/
1.101 + CallSite(MethodType type) {
1.102 + throw new IllegalStateException();
1.103 + }
1.104 +
1.105 + /**
1.106 + * Make a call site object equipped with an initial target method handle.
1.107 + * @param target the method handle which will be the initial target of the call site
1.108 + * @throws NullPointerException if the proposed target is null
1.109 + */
1.110 + /*package-private*/
1.111 + CallSite(MethodHandle target) {
1.112 + target.type(); // null check
1.113 + this.target = target;
1.114 + }
1.115 +
1.116 + /**
1.117 + * Make a call site object equipped with an initial target method handle.
1.118 + * @param targetType the desired type of the call site
1.119 + * @param createTargetHook a hook which will bind the call site to the target method handle
1.120 + * @throws WrongMethodTypeException if the hook cannot be invoked on the required arguments,
1.121 + * or if the target returned by the hook is not of the given {@code targetType}
1.122 + * @throws NullPointerException if the hook returns a null value
1.123 + * @throws ClassCastException if the hook returns something other than a {@code MethodHandle}
1.124 + * @throws Throwable anything else thrown by the hook function
1.125 + */
1.126 + /*package-private*/
1.127 + CallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable {
1.128 + throw new IllegalStateException();
1.129 + }
1.130 +
1.131 + /**
1.132 + * Returns the type of this call site's target.
1.133 + * Although targets may change, any call site's type is permanent, and can never change to an unequal type.
1.134 + * The {@code setTarget} method enforces this invariant by refusing any new target that does
1.135 + * not have the previous target's type.
1.136 + * @return the type of the current target, which is also the type of any future target
1.137 + */
1.138 + public MethodType type() {
1.139 + // warning: do not call getTarget here, because CCS.getTarget can throw IllegalStateException
1.140 + return target.type();
1.141 + }
1.142 +
1.143 + /**
1.144 + * Returns the target method of the call site, according to the
1.145 + * behavior defined by this call site's specific class.
1.146 + * The immediate subclasses of {@code CallSite} document the
1.147 + * class-specific behaviors of this method.
1.148 + *
1.149 + * @return the current linkage state of the call site, its target method handle
1.150 + * @see ConstantCallSite
1.151 + * @see VolatileCallSite
1.152 + * @see #setTarget
1.153 + * @see ConstantCallSite#getTarget
1.154 + * @see MutableCallSite#getTarget
1.155 + * @see VolatileCallSite#getTarget
1.156 + */
1.157 + public abstract MethodHandle getTarget();
1.158 +
1.159 + /**
1.160 + * Updates the target method of this call site, according to the
1.161 + * behavior defined by this call site's specific class.
1.162 + * The immediate subclasses of {@code CallSite} document the
1.163 + * class-specific behaviors of this method.
1.164 + * <p>
1.165 + * The type of the new target must be {@linkplain MethodType#equals equal to}
1.166 + * the type of the old target.
1.167 + *
1.168 + * @param newTarget the new target
1.169 + * @throws NullPointerException if the proposed new target is null
1.170 + * @throws WrongMethodTypeException if the proposed new target
1.171 + * has a method type that differs from the previous target
1.172 + * @see CallSite#getTarget
1.173 + * @see ConstantCallSite#setTarget
1.174 + * @see MutableCallSite#setTarget
1.175 + * @see VolatileCallSite#setTarget
1.176 + */
1.177 + public abstract void setTarget(MethodHandle newTarget);
1.178 +
1.179 + /**
1.180 + * Produces a method handle equivalent to an invokedynamic instruction
1.181 + * which has been linked to this call site.
1.182 + * <p>
1.183 + * This method is equivalent to the following code:
1.184 + * <blockquote><pre>{@code
1.185 + * MethodHandle getTarget, invoker, result;
1.186 + * getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
1.187 + * invoker = MethodHandles.exactInvoker(this.type());
1.188 + * result = MethodHandles.foldArguments(invoker, getTarget)
1.189 + * }</pre></blockquote>
1.190 + *
1.191 + * @return a method handle which always invokes this call site's current target
1.192 + */
1.193 + public abstract MethodHandle dynamicInvoker();
1.194 +}