rt/emul/compact/src/main/java/java/lang/invoke/CallSite.java
branchjdk8
changeset 1675 cd50c1894ce5
parent 1674 eca8e9c3ec3e
child 1678 35daab73e225
     1.1 --- a/rt/emul/compact/src/main/java/java/lang/invoke/CallSite.java	Sun Aug 17 20:09:05 2014 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,339 +0,0 @@
     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 -import sun.invoke.empty.Empty;
    1.32 -import static java.lang.invoke.MethodHandleStatics.*;
    1.33 -import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP;
    1.34 -
    1.35 -/**
    1.36 - * A {@code CallSite} is a holder for a variable {@link MethodHandle},
    1.37 - * which is called its {@code target}.
    1.38 - * An {@code invokedynamic} instruction linked to a {@code CallSite} delegates
    1.39 - * all calls to the site's current target.
    1.40 - * A {@code CallSite} may be associated with several {@code invokedynamic}
    1.41 - * instructions, or it may be "free floating", associated with none.
    1.42 - * In any case, it may be invoked through an associated method handle
    1.43 - * called its {@linkplain #dynamicInvoker dynamic invoker}.
    1.44 - * <p>
    1.45 - * {@code CallSite} is an abstract class which does not allow
    1.46 - * direct subclassing by users.  It has three immediate,
    1.47 - * concrete subclasses that may be either instantiated or subclassed.
    1.48 - * <ul>
    1.49 - * <li>If a mutable target is not required, an {@code invokedynamic} instruction
    1.50 - * may be permanently bound by means of a {@linkplain ConstantCallSite constant call site}.
    1.51 - * <li>If a mutable target is required which has volatile variable semantics,
    1.52 - * because updates to the target must be immediately and reliably witnessed by other threads,
    1.53 - * a {@linkplain VolatileCallSite volatile call site} may be used.
    1.54 - * <li>Otherwise, if a mutable target is required,
    1.55 - * a {@linkplain MutableCallSite mutable call site} may be used.
    1.56 - * </ul>
    1.57 - * <p>
    1.58 - * A non-constant call site may be <em>relinked</em> by changing its target.
    1.59 - * The new target must have the same {@linkplain MethodHandle#type() type}
    1.60 - * as the previous target.
    1.61 - * Thus, though a call site can be relinked to a series of
    1.62 - * successive targets, it cannot change its type.
    1.63 - * <p>
    1.64 - * Here is a sample use of call sites and bootstrap methods which links every
    1.65 - * dynamic call site to print its arguments:
    1.66 -<blockquote><pre>{@code
    1.67 -static void test() throws Throwable {
    1.68 -    // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION
    1.69 -    InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14);
    1.70 -}
    1.71 -private static void printArgs(Object... args) {
    1.72 -  System.out.println(java.util.Arrays.deepToString(args));
    1.73 -}
    1.74 -private static final MethodHandle printArgs;
    1.75 -static {
    1.76 -  MethodHandles.Lookup lookup = MethodHandles.lookup();
    1.77 -  Class thisClass = lookup.lookupClass();  // (who am I?)
    1.78 -  printArgs = lookup.findStatic(thisClass,
    1.79 -      "printArgs", MethodType.methodType(void.class, Object[].class));
    1.80 -}
    1.81 -private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
    1.82 -  // ignore caller and name, but match the type:
    1.83 -  return new ConstantCallSite(printArgs.asType(type));
    1.84 -}
    1.85 -}</pre></blockquote>
    1.86 - * @author John Rose, JSR 292 EG
    1.87 - */
    1.88 -abstract
    1.89 -public class CallSite {
    1.90 -    static { MethodHandleImpl.initStatics(); }
    1.91 -
    1.92 -    // The actual payload of this call site:
    1.93 -    /*package-private*/
    1.94 -    MethodHandle target;    // Note: This field is known to the JVM.  Do not change.
    1.95 -
    1.96 -    /**
    1.97 -     * Make a blank call site object with the given method type.
    1.98 -     * An initial target method is supplied which will throw
    1.99 -     * an {@link IllegalStateException} if called.
   1.100 -     * <p>
   1.101 -     * Before this {@code CallSite} object is returned from a bootstrap method,
   1.102 -     * it is usually provided with a more useful target method,
   1.103 -     * via a call to {@link CallSite#setTarget(MethodHandle) setTarget}.
   1.104 -     * @throws NullPointerException if the proposed type is null
   1.105 -     */
   1.106 -    /*package-private*/
   1.107 -    CallSite(MethodType type) {
   1.108 -        target = type.invokers().uninitializedCallSite();
   1.109 -    }
   1.110 -
   1.111 -    /**
   1.112 -     * Make a call site object equipped with an initial target method handle.
   1.113 -     * @param target the method handle which will be the initial target of the call site
   1.114 -     * @throws NullPointerException if the proposed target is null
   1.115 -     */
   1.116 -    /*package-private*/
   1.117 -    CallSite(MethodHandle target) {
   1.118 -        target.type();  // null check
   1.119 -        this.target = target;
   1.120 -    }
   1.121 -
   1.122 -    /**
   1.123 -     * Make a call site object equipped with an initial target method handle.
   1.124 -     * @param targetType the desired type of the call site
   1.125 -     * @param createTargetHook a hook which will bind the call site to the target method handle
   1.126 -     * @throws WrongMethodTypeException if the hook cannot be invoked on the required arguments,
   1.127 -     *         or if the target returned by the hook is not of the given {@code targetType}
   1.128 -     * @throws NullPointerException if the hook returns a null value
   1.129 -     * @throws ClassCastException if the hook returns something other than a {@code MethodHandle}
   1.130 -     * @throws Throwable anything else thrown by the hook function
   1.131 -     */
   1.132 -    /*package-private*/
   1.133 -    CallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable {
   1.134 -        this(targetType);
   1.135 -        ConstantCallSite selfCCS = (ConstantCallSite) this;
   1.136 -        MethodHandle boundTarget = (MethodHandle) createTargetHook.invokeWithArguments(selfCCS);
   1.137 -        checkTargetChange(this.target, boundTarget);
   1.138 -        this.target = boundTarget;
   1.139 -    }
   1.140 -
   1.141 -    /**
   1.142 -     * Returns the type of this call site's target.
   1.143 -     * Although targets may change, any call site's type is permanent, and can never change to an unequal type.
   1.144 -     * The {@code setTarget} method enforces this invariant by refusing any new target that does
   1.145 -     * not have the previous target's type.
   1.146 -     * @return the type of the current target, which is also the type of any future target
   1.147 -     */
   1.148 -    public MethodType type() {
   1.149 -        // warning:  do not call getTarget here, because CCS.getTarget can throw IllegalStateException
   1.150 -        return target.type();
   1.151 -    }
   1.152 -
   1.153 -    /**
   1.154 -     * Returns the target method of the call site, according to the
   1.155 -     * behavior defined by this call site's specific class.
   1.156 -     * The immediate subclasses of {@code CallSite} document the
   1.157 -     * class-specific behaviors of this method.
   1.158 -     *
   1.159 -     * @return the current linkage state of the call site, its target method handle
   1.160 -     * @see ConstantCallSite
   1.161 -     * @see VolatileCallSite
   1.162 -     * @see #setTarget
   1.163 -     * @see ConstantCallSite#getTarget
   1.164 -     * @see MutableCallSite#getTarget
   1.165 -     * @see VolatileCallSite#getTarget
   1.166 -     */
   1.167 -    public abstract MethodHandle getTarget();
   1.168 -
   1.169 -    /**
   1.170 -     * Updates the target method of this call site, according to the
   1.171 -     * behavior defined by this call site's specific class.
   1.172 -     * The immediate subclasses of {@code CallSite} document the
   1.173 -     * class-specific behaviors of this method.
   1.174 -     * <p>
   1.175 -     * The type of the new target must be {@linkplain MethodType#equals equal to}
   1.176 -     * the type of the old target.
   1.177 -     *
   1.178 -     * @param newTarget the new target
   1.179 -     * @throws NullPointerException if the proposed new target is null
   1.180 -     * @throws WrongMethodTypeException if the proposed new target
   1.181 -     *         has a method type that differs from the previous target
   1.182 -     * @see CallSite#getTarget
   1.183 -     * @see ConstantCallSite#setTarget
   1.184 -     * @see MutableCallSite#setTarget
   1.185 -     * @see VolatileCallSite#setTarget
   1.186 -     */
   1.187 -    public abstract void setTarget(MethodHandle newTarget);
   1.188 -
   1.189 -    void checkTargetChange(MethodHandle oldTarget, MethodHandle newTarget) {
   1.190 -        MethodType oldType = oldTarget.type();
   1.191 -        MethodType newType = newTarget.type();  // null check!
   1.192 -        if (!newType.equals(oldType))
   1.193 -            throw wrongTargetType(newTarget, oldType);
   1.194 -    }
   1.195 -
   1.196 -    private static WrongMethodTypeException wrongTargetType(MethodHandle target, MethodType type) {
   1.197 -        return new WrongMethodTypeException(String.valueOf(target)+" should be of type "+type);
   1.198 -    }
   1.199 -
   1.200 -    /**
   1.201 -     * Produces a method handle equivalent to an invokedynamic instruction
   1.202 -     * which has been linked to this call site.
   1.203 -     * <p>
   1.204 -     * This method is equivalent to the following code:
   1.205 -     * <blockquote><pre>{@code
   1.206 -     * MethodHandle getTarget, invoker, result;
   1.207 -     * getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
   1.208 -     * invoker = MethodHandles.exactInvoker(this.type());
   1.209 -     * result = MethodHandles.foldArguments(invoker, getTarget)
   1.210 -     * }</pre></blockquote>
   1.211 -     *
   1.212 -     * @return a method handle which always invokes this call site's current target
   1.213 -     */
   1.214 -    public abstract MethodHandle dynamicInvoker();
   1.215 -
   1.216 -    /*non-public*/ MethodHandle makeDynamicInvoker() {
   1.217 -        MethodHandle getTarget = GET_TARGET.bindReceiver(this);
   1.218 -        MethodHandle invoker = MethodHandles.exactInvoker(this.type());
   1.219 -        return MethodHandles.foldArguments(invoker, getTarget);
   1.220 -    }
   1.221 -
   1.222 -    private static final MethodHandle GET_TARGET;
   1.223 -    static {
   1.224 -        try {
   1.225 -            GET_TARGET = IMPL_LOOKUP.
   1.226 -                findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class));
   1.227 -        } catch (ReflectiveOperationException e) {
   1.228 -            throw newInternalError(e);
   1.229 -        }
   1.230 -    }
   1.231 -
   1.232 -    /** This guy is rolled into the default target if a MethodType is supplied to the constructor. */
   1.233 -    /*package-private*/
   1.234 -    static Empty uninitializedCallSite() {
   1.235 -        throw new IllegalStateException("uninitialized call site");
   1.236 -    }
   1.237 -
   1.238 -    /*package-private*/
   1.239 -    void setTargetNormal(MethodHandle newTarget) {
   1.240 -        MethodHandleNatives.setCallSiteTargetNormal(this, newTarget);
   1.241 -    }
   1.242 -    /*package-private*/
   1.243 -    MethodHandle getTargetVolatile() {
   1.244 -        return target;
   1.245 -    }
   1.246 -    /*package-private*/
   1.247 -    void setTargetVolatile(MethodHandle newTarget) {
   1.248 -        MethodHandleNatives.setCallSiteTargetVolatile(this, newTarget);
   1.249 -    }
   1.250 -
   1.251 -    // this implements the upcall from the JVM, MethodHandleNatives.makeDynamicCallSite:
   1.252 -    static CallSite makeSite(MethodHandle bootstrapMethod,
   1.253 -                             // Callee information:
   1.254 -                             String name, MethodType type,
   1.255 -                             // Extra arguments for BSM, if any:
   1.256 -                             Object info,
   1.257 -                             // Caller information:
   1.258 -                             Class<?> callerClass) {
   1.259 -        MethodHandles.Lookup caller = IMPL_LOOKUP.in(callerClass);
   1.260 -        CallSite site;
   1.261 -        try {
   1.262 -            Object binding;
   1.263 -            info = maybeReBox(info);
   1.264 -            if (info == null) {
   1.265 -                binding = bootstrapMethod.invoke(caller, name, type);
   1.266 -            } else if (!info.getClass().isArray()) {
   1.267 -                binding = bootstrapMethod.invoke(caller, name, type, info);
   1.268 -            } else {
   1.269 -                Object[] argv = (Object[]) info;
   1.270 -                maybeReBoxElements(argv);
   1.271 -                switch (argv.length) {
   1.272 -                case 0:
   1.273 -                    binding = bootstrapMethod.invoke(caller, name, type);
   1.274 -                    break;
   1.275 -                case 1:
   1.276 -                    binding = bootstrapMethod.invoke(caller, name, type,
   1.277 -                                                     argv[0]);
   1.278 -                    break;
   1.279 -                case 2:
   1.280 -                    binding = bootstrapMethod.invoke(caller, name, type,
   1.281 -                                                     argv[0], argv[1]);
   1.282 -                    break;
   1.283 -                case 3:
   1.284 -                    binding = bootstrapMethod.invoke(caller, name, type,
   1.285 -                                                     argv[0], argv[1], argv[2]);
   1.286 -                    break;
   1.287 -                case 4:
   1.288 -                    binding = bootstrapMethod.invoke(caller, name, type,
   1.289 -                                                     argv[0], argv[1], argv[2], argv[3]);
   1.290 -                    break;
   1.291 -                case 5:
   1.292 -                    binding = bootstrapMethod.invoke(caller, name, type,
   1.293 -                                                     argv[0], argv[1], argv[2], argv[3], argv[4]);
   1.294 -                    break;
   1.295 -                case 6:
   1.296 -                    binding = bootstrapMethod.invoke(caller, name, type,
   1.297 -                                                     argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
   1.298 -                    break;
   1.299 -                default:
   1.300 -                    final int NON_SPREAD_ARG_COUNT = 3;  // (caller, name, type)
   1.301 -                    if (NON_SPREAD_ARG_COUNT + argv.length > MethodType.MAX_MH_ARITY)
   1.302 -                        throw new BootstrapMethodError("too many bootstrap method arguments");
   1.303 -                    MethodType bsmType = bootstrapMethod.type();
   1.304 -                    MethodType invocationType = MethodType.genericMethodType(NON_SPREAD_ARG_COUNT + argv.length);
   1.305 -                    MethodHandle typedBSM = bootstrapMethod.asType(invocationType);
   1.306 -                    MethodHandle spreader = invocationType.invokers().spreadInvoker(NON_SPREAD_ARG_COUNT);
   1.307 -                    binding = spreader.invokeExact(typedBSM, (Object)caller, (Object)name, (Object)type, argv);
   1.308 -                }
   1.309 -            }
   1.310 -            //System.out.println("BSM for "+name+type+" => "+binding);
   1.311 -            if (binding instanceof CallSite) {
   1.312 -                site = (CallSite) binding;
   1.313 -            }  else {
   1.314 -                throw new ClassCastException("bootstrap method failed to produce a CallSite");
   1.315 -            }
   1.316 -            if (!site.getTarget().type().equals(type))
   1.317 -                throw new WrongMethodTypeException("wrong type: "+site.getTarget());
   1.318 -        } catch (Throwable ex) {
   1.319 -            BootstrapMethodError bex;
   1.320 -            if (ex instanceof BootstrapMethodError)
   1.321 -                bex = (BootstrapMethodError) ex;
   1.322 -            else
   1.323 -                bex = new BootstrapMethodError("call site initialization exception", ex);
   1.324 -            throw bex;
   1.325 -        }
   1.326 -        return site;
   1.327 -    }
   1.328 -
   1.329 -    private static Object maybeReBox(Object x) {
   1.330 -        if (x instanceof Integer) {
   1.331 -            int xi = (int) x;
   1.332 -            if (xi == (byte) xi)
   1.333 -                x = xi;  // must rebox; see JLS 5.1.7
   1.334 -        }
   1.335 -        return x;
   1.336 -    }
   1.337 -    private static void maybeReBoxElements(Object[] xa) {
   1.338 -        for (int i = 0; i < xa.length; i++) {
   1.339 -            xa[i] = maybeReBox(xa[i]);
   1.340 -        }
   1.341 -    }
   1.342 -}