jaroslav@1646: /* jaroslav@1646: * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved. jaroslav@1646: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jaroslav@1646: * jaroslav@1646: * This code is free software; you can redistribute it and/or modify it jaroslav@1646: * under the terms of the GNU General Public License version 2 only, as jaroslav@1646: * published by the Free Software Foundation. Oracle designates this jaroslav@1646: * particular file as subject to the "Classpath" exception as provided jaroslav@1646: * by Oracle in the LICENSE file that accompanied this code. jaroslav@1646: * jaroslav@1646: * This code is distributed in the hope that it will be useful, but WITHOUT jaroslav@1646: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jaroslav@1646: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jaroslav@1646: * version 2 for more details (a copy is included in the LICENSE file that jaroslav@1646: * accompanied this code). jaroslav@1646: * jaroslav@1646: * You should have received a copy of the GNU General Public License version jaroslav@1646: * 2 along with this work; if not, write to the Free Software Foundation, jaroslav@1646: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jaroslav@1646: * jaroslav@1646: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jaroslav@1646: * or visit www.oracle.com if you need additional information or have any jaroslav@1646: * questions. jaroslav@1646: */ jaroslav@1646: jaroslav@1646: package java.lang.invoke; jaroslav@1646: jaroslav@1646: import sun.invoke.empty.Empty; jaroslav@1646: import static java.lang.invoke.MethodHandleStatics.*; jaroslav@1646: import static java.lang.invoke.MethodHandles.Lookup.IMPL_LOOKUP; jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * A {@code CallSite} is a holder for a variable {@link MethodHandle}, jaroslav@1646: * which is called its {@code target}. jaroslav@1646: * An {@code invokedynamic} instruction linked to a {@code CallSite} delegates jaroslav@1646: * all calls to the site's current target. jaroslav@1646: * A {@code CallSite} may be associated with several {@code invokedynamic} jaroslav@1646: * instructions, or it may be "free floating", associated with none. jaroslav@1646: * In any case, it may be invoked through an associated method handle jaroslav@1646: * called its {@linkplain #dynamicInvoker dynamic invoker}. jaroslav@1646: *

jaroslav@1646: * {@code CallSite} is an abstract class which does not allow jaroslav@1646: * direct subclassing by users. It has three immediate, jaroslav@1646: * concrete subclasses that may be either instantiated or subclassed. jaroslav@1646: *

jaroslav@1646: *

jaroslav@1646: * A non-constant call site may be relinked by changing its target. jaroslav@1646: * The new target must have the same {@linkplain MethodHandle#type() type} jaroslav@1646: * as the previous target. jaroslav@1646: * Thus, though a call site can be relinked to a series of jaroslav@1646: * successive targets, it cannot change its type. jaroslav@1646: *

jaroslav@1646: * Here is a sample use of call sites and bootstrap methods which links every jaroslav@1646: * dynamic call site to print its arguments: jaroslav@1646:

{@code
jaroslav@1646: static void test() throws Throwable {
jaroslav@1646:     // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION
jaroslav@1646:     InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14);
jaroslav@1646: }
jaroslav@1646: private static void printArgs(Object... args) {
jaroslav@1646:   System.out.println(java.util.Arrays.deepToString(args));
jaroslav@1646: }
jaroslav@1646: private static final MethodHandle printArgs;
jaroslav@1646: static {
jaroslav@1646:   MethodHandles.Lookup lookup = MethodHandles.lookup();
jaroslav@1646:   Class thisClass = lookup.lookupClass();  // (who am I?)
jaroslav@1646:   printArgs = lookup.findStatic(thisClass,
jaroslav@1646:       "printArgs", MethodType.methodType(void.class, Object[].class));
jaroslav@1646: }
jaroslav@1646: private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
jaroslav@1646:   // ignore caller and name, but match the type:
jaroslav@1646:   return new ConstantCallSite(printArgs.asType(type));
jaroslav@1646: }
jaroslav@1646: }
jaroslav@1646: * @author John Rose, JSR 292 EG jaroslav@1646: */ jaroslav@1646: abstract jaroslav@1646: public class CallSite { jaroslav@1646: static { MethodHandleImpl.initStatics(); } jaroslav@1646: jaroslav@1646: // The actual payload of this call site: jaroslav@1646: /*package-private*/ jaroslav@1646: MethodHandle target; // Note: This field is known to the JVM. Do not change. jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Make a blank call site object with the given method type. jaroslav@1646: * An initial target method is supplied which will throw jaroslav@1646: * an {@link IllegalStateException} if called. jaroslav@1646: *

jaroslav@1646: * Before this {@code CallSite} object is returned from a bootstrap method, jaroslav@1646: * it is usually provided with a more useful target method, jaroslav@1646: * via a call to {@link CallSite#setTarget(MethodHandle) setTarget}. jaroslav@1646: * @throws NullPointerException if the proposed type is null jaroslav@1646: */ jaroslav@1646: /*package-private*/ jaroslav@1646: CallSite(MethodType type) { jaroslav@1646: target = type.invokers().uninitializedCallSite(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Make a call site object equipped with an initial target method handle. jaroslav@1646: * @param target the method handle which will be the initial target of the call site jaroslav@1646: * @throws NullPointerException if the proposed target is null jaroslav@1646: */ jaroslav@1646: /*package-private*/ jaroslav@1646: CallSite(MethodHandle target) { jaroslav@1646: target.type(); // null check jaroslav@1646: this.target = target; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Make a call site object equipped with an initial target method handle. jaroslav@1646: * @param targetType the desired type of the call site jaroslav@1646: * @param createTargetHook a hook which will bind the call site to the target method handle jaroslav@1646: * @throws WrongMethodTypeException if the hook cannot be invoked on the required arguments, jaroslav@1646: * or if the target returned by the hook is not of the given {@code targetType} jaroslav@1646: * @throws NullPointerException if the hook returns a null value jaroslav@1646: * @throws ClassCastException if the hook returns something other than a {@code MethodHandle} jaroslav@1646: * @throws Throwable anything else thrown by the hook function jaroslav@1646: */ jaroslav@1646: /*package-private*/ jaroslav@1646: CallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable { jaroslav@1646: this(targetType); jaroslav@1646: ConstantCallSite selfCCS = (ConstantCallSite) this; jaroslav@1646: MethodHandle boundTarget = (MethodHandle) createTargetHook.invokeWithArguments(selfCCS); jaroslav@1646: checkTargetChange(this.target, boundTarget); jaroslav@1646: this.target = boundTarget; jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the type of this call site's target. jaroslav@1646: * Although targets may change, any call site's type is permanent, and can never change to an unequal type. jaroslav@1646: * The {@code setTarget} method enforces this invariant by refusing any new target that does jaroslav@1646: * not have the previous target's type. jaroslav@1646: * @return the type of the current target, which is also the type of any future target jaroslav@1646: */ jaroslav@1646: public MethodType type() { jaroslav@1646: // warning: do not call getTarget here, because CCS.getTarget can throw IllegalStateException jaroslav@1646: return target.type(); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Returns the target method of the call site, according to the jaroslav@1646: * behavior defined by this call site's specific class. jaroslav@1646: * The immediate subclasses of {@code CallSite} document the jaroslav@1646: * class-specific behaviors of this method. jaroslav@1646: * jaroslav@1646: * @return the current linkage state of the call site, its target method handle jaroslav@1646: * @see ConstantCallSite jaroslav@1646: * @see VolatileCallSite jaroslav@1646: * @see #setTarget jaroslav@1646: * @see ConstantCallSite#getTarget jaroslav@1646: * @see MutableCallSite#getTarget jaroslav@1646: * @see VolatileCallSite#getTarget jaroslav@1646: */ jaroslav@1646: public abstract MethodHandle getTarget(); jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Updates the target method of this call site, according to the jaroslav@1646: * behavior defined by this call site's specific class. jaroslav@1646: * The immediate subclasses of {@code CallSite} document the jaroslav@1646: * class-specific behaviors of this method. jaroslav@1646: *

jaroslav@1646: * The type of the new target must be {@linkplain MethodType#equals equal to} jaroslav@1646: * the type of the old target. jaroslav@1646: * jaroslav@1646: * @param newTarget the new target jaroslav@1646: * @throws NullPointerException if the proposed new target is null jaroslav@1646: * @throws WrongMethodTypeException if the proposed new target jaroslav@1646: * has a method type that differs from the previous target jaroslav@1646: * @see CallSite#getTarget jaroslav@1646: * @see ConstantCallSite#setTarget jaroslav@1646: * @see MutableCallSite#setTarget jaroslav@1646: * @see VolatileCallSite#setTarget jaroslav@1646: */ jaroslav@1646: public abstract void setTarget(MethodHandle newTarget); jaroslav@1646: jaroslav@1646: void checkTargetChange(MethodHandle oldTarget, MethodHandle newTarget) { jaroslav@1646: MethodType oldType = oldTarget.type(); jaroslav@1646: MethodType newType = newTarget.type(); // null check! jaroslav@1646: if (!newType.equals(oldType)) jaroslav@1646: throw wrongTargetType(newTarget, oldType); jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static WrongMethodTypeException wrongTargetType(MethodHandle target, MethodType type) { jaroslav@1646: return new WrongMethodTypeException(String.valueOf(target)+" should be of type "+type); jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** jaroslav@1646: * Produces a method handle equivalent to an invokedynamic instruction jaroslav@1646: * which has been linked to this call site. jaroslav@1646: *

jaroslav@1646: * This method is equivalent to the following code: jaroslav@1646: *

{@code
jaroslav@1646:      * MethodHandle getTarget, invoker, result;
jaroslav@1646:      * getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
jaroslav@1646:      * invoker = MethodHandles.exactInvoker(this.type());
jaroslav@1646:      * result = MethodHandles.foldArguments(invoker, getTarget)
jaroslav@1646:      * }
jaroslav@1646: * jaroslav@1646: * @return a method handle which always invokes this call site's current target jaroslav@1646: */ jaroslav@1646: public abstract MethodHandle dynamicInvoker(); jaroslav@1646: jaroslav@1646: /*non-public*/ MethodHandle makeDynamicInvoker() { jaroslav@1646: MethodHandle getTarget = GET_TARGET.bindReceiver(this); jaroslav@1646: MethodHandle invoker = MethodHandles.exactInvoker(this.type()); jaroslav@1646: return MethodHandles.foldArguments(invoker, getTarget); jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static final MethodHandle GET_TARGET; jaroslav@1646: static { jaroslav@1646: try { jaroslav@1646: GET_TARGET = IMPL_LOOKUP. jaroslav@1646: findVirtual(CallSite.class, "getTarget", MethodType.methodType(MethodHandle.class)); jaroslav@1646: } catch (ReflectiveOperationException e) { jaroslav@1646: throw newInternalError(e); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /** This guy is rolled into the default target if a MethodType is supplied to the constructor. */ jaroslav@1646: /*package-private*/ jaroslav@1646: static Empty uninitializedCallSite() { jaroslav@1646: throw new IllegalStateException("uninitialized call site"); jaroslav@1646: } jaroslav@1646: jaroslav@1646: // unsafe stuff: jaroslav@1646: private static final long TARGET_OFFSET; jaroslav@1646: static { jaroslav@1646: try { jaroslav@1646: TARGET_OFFSET = UNSAFE.objectFieldOffset(CallSite.class.getDeclaredField("target")); jaroslav@1646: } catch (Exception ex) { throw new Error(ex); } jaroslav@1646: } jaroslav@1646: jaroslav@1646: /*package-private*/ jaroslav@1646: void setTargetNormal(MethodHandle newTarget) { jaroslav@1646: MethodHandleNatives.setCallSiteTargetNormal(this, newTarget); jaroslav@1646: } jaroslav@1646: /*package-private*/ jaroslav@1646: MethodHandle getTargetVolatile() { jaroslav@1646: return (MethodHandle) UNSAFE.getObjectVolatile(this, TARGET_OFFSET); jaroslav@1646: } jaroslav@1646: /*package-private*/ jaroslav@1646: void setTargetVolatile(MethodHandle newTarget) { jaroslav@1646: MethodHandleNatives.setCallSiteTargetVolatile(this, newTarget); jaroslav@1646: } jaroslav@1646: jaroslav@1646: // this implements the upcall from the JVM, MethodHandleNatives.makeDynamicCallSite: jaroslav@1646: static CallSite makeSite(MethodHandle bootstrapMethod, jaroslav@1646: // Callee information: jaroslav@1646: String name, MethodType type, jaroslav@1646: // Extra arguments for BSM, if any: jaroslav@1646: Object info, jaroslav@1646: // Caller information: jaroslav@1646: Class callerClass) { jaroslav@1646: MethodHandles.Lookup caller = IMPL_LOOKUP.in(callerClass); jaroslav@1646: CallSite site; jaroslav@1646: try { jaroslav@1646: Object binding; jaroslav@1646: info = maybeReBox(info); jaroslav@1646: if (info == null) { jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type); jaroslav@1646: } else if (!info.getClass().isArray()) { jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type, info); jaroslav@1646: } else { jaroslav@1646: Object[] argv = (Object[]) info; jaroslav@1646: maybeReBoxElements(argv); jaroslav@1646: switch (argv.length) { jaroslav@1646: case 0: jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type); jaroslav@1646: break; jaroslav@1646: case 1: jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type, jaroslav@1646: argv[0]); jaroslav@1646: break; jaroslav@1646: case 2: jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type, jaroslav@1646: argv[0], argv[1]); jaroslav@1646: break; jaroslav@1646: case 3: jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type, jaroslav@1646: argv[0], argv[1], argv[2]); jaroslav@1646: break; jaroslav@1646: case 4: jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type, jaroslav@1646: argv[0], argv[1], argv[2], argv[3]); jaroslav@1646: break; jaroslav@1646: case 5: jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type, jaroslav@1646: argv[0], argv[1], argv[2], argv[3], argv[4]); jaroslav@1646: break; jaroslav@1646: case 6: jaroslav@1646: binding = bootstrapMethod.invoke(caller, name, type, jaroslav@1646: argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]); jaroslav@1646: break; jaroslav@1646: default: jaroslav@1646: final int NON_SPREAD_ARG_COUNT = 3; // (caller, name, type) jaroslav@1646: if (NON_SPREAD_ARG_COUNT + argv.length > MethodType.MAX_MH_ARITY) jaroslav@1646: throw new BootstrapMethodError("too many bootstrap method arguments"); jaroslav@1646: MethodType bsmType = bootstrapMethod.type(); jaroslav@1646: MethodType invocationType = MethodType.genericMethodType(NON_SPREAD_ARG_COUNT + argv.length); jaroslav@1646: MethodHandle typedBSM = bootstrapMethod.asType(invocationType); jaroslav@1646: MethodHandle spreader = invocationType.invokers().spreadInvoker(NON_SPREAD_ARG_COUNT); jaroslav@1646: binding = spreader.invokeExact(typedBSM, (Object)caller, (Object)name, (Object)type, argv); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: //System.out.println("BSM for "+name+type+" => "+binding); jaroslav@1646: if (binding instanceof CallSite) { jaroslav@1646: site = (CallSite) binding; jaroslav@1646: } else { jaroslav@1646: throw new ClassCastException("bootstrap method failed to produce a CallSite"); jaroslav@1646: } jaroslav@1646: if (!site.getTarget().type().equals(type)) jaroslav@1646: throw new WrongMethodTypeException("wrong type: "+site.getTarget()); jaroslav@1646: } catch (Throwable ex) { jaroslav@1646: BootstrapMethodError bex; jaroslav@1646: if (ex instanceof BootstrapMethodError) jaroslav@1646: bex = (BootstrapMethodError) ex; jaroslav@1646: else jaroslav@1646: bex = new BootstrapMethodError("call site initialization exception", ex); jaroslav@1646: throw bex; jaroslav@1646: } jaroslav@1646: return site; jaroslav@1646: } jaroslav@1646: jaroslav@1646: private static Object maybeReBox(Object x) { jaroslav@1646: if (x instanceof Integer) { jaroslav@1646: int xi = (int) x; jaroslav@1646: if (xi == (byte) xi) jaroslav@1646: x = xi; // must rebox; see JLS 5.1.7 jaroslav@1646: } jaroslav@1646: return x; jaroslav@1646: } jaroslav@1646: private static void maybeReBoxElements(Object[] xa) { jaroslav@1646: for (int i = 0; i < xa.length; i++) { jaroslav@1646: xa[i] = maybeReBox(xa[i]); jaroslav@1646: } jaroslav@1646: } jaroslav@1646: }