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

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

jaroslav@1692: *

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

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

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

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

jaroslav@1692: * The type of the new target must be {@linkplain MethodType#equals equal to} jaroslav@1692: * the type of the old target. jaroslav@1692: * jaroslav@1692: * @param newTarget the new target jaroslav@1692: * @throws NullPointerException if the proposed new target is null jaroslav@1692: * @throws WrongMethodTypeException if the proposed new target jaroslav@1692: * has a method type that differs from the previous target jaroslav@1692: * @see CallSite#getTarget jaroslav@1692: * @see ConstantCallSite#setTarget jaroslav@1692: * @see MutableCallSite#setTarget jaroslav@1692: * @see VolatileCallSite#setTarget jaroslav@1692: */ jaroslav@1692: public abstract void setTarget(MethodHandle newTarget); jaroslav@1692: jaroslav@1692: /** jaroslav@1692: * Produces a method handle equivalent to an invokedynamic instruction jaroslav@1692: * which has been linked to this call site. jaroslav@1692: *

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

{@code
jaroslav@1692:      * MethodHandle getTarget, invoker, result;
jaroslav@1692:      * getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
jaroslav@1692:      * invoker = MethodHandles.exactInvoker(this.type());
jaroslav@1692:      * result = MethodHandles.foldArguments(invoker, getTarget)
jaroslav@1692:      * }
jaroslav@1692: * jaroslav@1692: * @return a method handle which always invokes this call site's current target jaroslav@1692: */ jaroslav@1692: public abstract MethodHandle dynamicInvoker(); jaroslav@1692: }