rt/emul/compact/src/main/java/java/lang/invoke/CallSite.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 14 Sep 2014 19:27:44 +0200
changeset 1692 2f800fdc371e
permissions -rw-r--r--
Adding necessary fake classes to allow Javac to compile lamda expressions against our emulation library.
     1 /*
     2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    25 
    26 package java.lang.invoke;
    27 
    28 /**
    29  * A {@code CallSite} is a holder for a variable {@link MethodHandle},
    30  * which is called its {@code target}.
    31  * An {@code invokedynamic} instruction linked to a {@code CallSite} delegates
    32  * all calls to the site's current target.
    33  * A {@code CallSite} may be associated with several {@code invokedynamic}
    34  * instructions, or it may be "free floating", associated with none.
    35  * In any case, it may be invoked through an associated method handle
    36  * called its {@linkplain #dynamicInvoker dynamic invoker}.
    37  * <p>
    38  * {@code CallSite} is an abstract class which does not allow
    39  * direct subclassing by users.  It has three immediate,
    40  * concrete subclasses that may be either instantiated or subclassed.
    41  * <ul>
    42  * <li>If a mutable target is not required, an {@code invokedynamic} instruction
    43  * may be permanently bound by means of a {@linkplain ConstantCallSite constant call site}.
    44  * <li>If a mutable target is required which has volatile variable semantics,
    45  * because updates to the target must be immediately and reliably witnessed by other threads,
    46  * a {@linkplain VolatileCallSite volatile call site} may be used.
    47  * <li>Otherwise, if a mutable target is required,
    48  * a {@linkplain MutableCallSite mutable call site} may be used.
    49  * </ul>
    50  * <p>
    51  * A non-constant call site may be <em>relinked</em> by changing its target.
    52  * The new target must have the same {@linkplain MethodHandle#type() type}
    53  * as the previous target.
    54  * Thus, though a call site can be relinked to a series of
    55  * successive targets, it cannot change its type.
    56  * <p>
    57  * Here is a sample use of call sites and bootstrap methods which links every
    58  * dynamic call site to print its arguments:
    59 <blockquote><pre>{@code
    60 static void test() throws Throwable {
    61     // THE FOLLOWING LINE IS PSEUDOCODE FOR A JVM INSTRUCTION
    62     InvokeDynamic[#bootstrapDynamic].baz("baz arg", 2, 3.14);
    63 }
    64 private static void printArgs(Object... args) {
    65   System.out.println(java.util.Arrays.deepToString(args));
    66 }
    67 private static final MethodHandle printArgs;
    68 static {
    69   MethodHandles.Lookup lookup = MethodHandles.lookup();
    70   Class thisClass = lookup.lookupClass();  // (who am I?)
    71   printArgs = lookup.findStatic(thisClass,
    72       "printArgs", MethodType.methodType(void.class, Object[].class));
    73 }
    74 private static CallSite bootstrapDynamic(MethodHandles.Lookup caller, String name, MethodType type) {
    75   // ignore caller and name, but match the type:
    76   return new ConstantCallSite(printArgs.asType(type));
    77 }
    78 }</pre></blockquote>
    79  * @author John Rose, JSR 292 EG
    80  */
    81 abstract
    82 public class CallSite {
    83     // The actual payload of this call site:
    84     /*package-private*/
    85     MethodHandle target;    // Note: This field is known to the JVM.  Do not change.
    86 
    87     /**
    88      * Make a blank call site object with the given method type.
    89      * An initial target method is supplied which will throw
    90      * an {@link IllegalStateException} if called.
    91      * <p>
    92      * Before this {@code CallSite} object is returned from a bootstrap method,
    93      * it is usually provided with a more useful target method,
    94      * via a call to {@link CallSite#setTarget(MethodHandle) setTarget}.
    95      * @throws NullPointerException if the proposed type is null
    96      */
    97     /*package-private*/
    98     CallSite(MethodType type) {
    99         throw new IllegalStateException();
   100     }
   101 
   102     /**
   103      * Make a call site object equipped with an initial target method handle.
   104      * @param target the method handle which will be the initial target of the call site
   105      * @throws NullPointerException if the proposed target is null
   106      */
   107     /*package-private*/
   108     CallSite(MethodHandle target) {
   109         target.type();  // null check
   110         this.target = target;
   111     }
   112 
   113     /**
   114      * Make a call site object equipped with an initial target method handle.
   115      * @param targetType the desired type of the call site
   116      * @param createTargetHook a hook which will bind the call site to the target method handle
   117      * @throws WrongMethodTypeException if the hook cannot be invoked on the required arguments,
   118      *         or if the target returned by the hook is not of the given {@code targetType}
   119      * @throws NullPointerException if the hook returns a null value
   120      * @throws ClassCastException if the hook returns something other than a {@code MethodHandle}
   121      * @throws Throwable anything else thrown by the hook function
   122      */
   123     /*package-private*/
   124     CallSite(MethodType targetType, MethodHandle createTargetHook) throws Throwable {
   125         throw new IllegalStateException();
   126     }
   127 
   128     /**
   129      * Returns the type of this call site's target.
   130      * Although targets may change, any call site's type is permanent, and can never change to an unequal type.
   131      * The {@code setTarget} method enforces this invariant by refusing any new target that does
   132      * not have the previous target's type.
   133      * @return the type of the current target, which is also the type of any future target
   134      */
   135     public MethodType type() {
   136         // warning:  do not call getTarget here, because CCS.getTarget can throw IllegalStateException
   137         return target.type();
   138     }
   139 
   140     /**
   141      * Returns the target method of the call site, according to the
   142      * behavior defined by this call site's specific class.
   143      * The immediate subclasses of {@code CallSite} document the
   144      * class-specific behaviors of this method.
   145      *
   146      * @return the current linkage state of the call site, its target method handle
   147      * @see ConstantCallSite
   148      * @see VolatileCallSite
   149      * @see #setTarget
   150      * @see ConstantCallSite#getTarget
   151      * @see MutableCallSite#getTarget
   152      * @see VolatileCallSite#getTarget
   153      */
   154     public abstract MethodHandle getTarget();
   155 
   156     /**
   157      * Updates the target method of this call site, according to the
   158      * behavior defined by this call site's specific class.
   159      * The immediate subclasses of {@code CallSite} document the
   160      * class-specific behaviors of this method.
   161      * <p>
   162      * The type of the new target must be {@linkplain MethodType#equals equal to}
   163      * the type of the old target.
   164      *
   165      * @param newTarget the new target
   166      * @throws NullPointerException if the proposed new target is null
   167      * @throws WrongMethodTypeException if the proposed new target
   168      *         has a method type that differs from the previous target
   169      * @see CallSite#getTarget
   170      * @see ConstantCallSite#setTarget
   171      * @see MutableCallSite#setTarget
   172      * @see VolatileCallSite#setTarget
   173      */
   174     public abstract void setTarget(MethodHandle newTarget);
   175 
   176     /**
   177      * Produces a method handle equivalent to an invokedynamic instruction
   178      * which has been linked to this call site.
   179      * <p>
   180      * This method is equivalent to the following code:
   181      * <blockquote><pre>{@code
   182      * MethodHandle getTarget, invoker, result;
   183      * getTarget = MethodHandles.publicLookup().bind(this, "getTarget", MethodType.methodType(MethodHandle.class));
   184      * invoker = MethodHandles.exactInvoker(this.type());
   185      * result = MethodHandles.foldArguments(invoker, getTarget)
   186      * }</pre></blockquote>
   187      *
   188      * @return a method handle which always invokes this call site's current target
   189      */
   190     public abstract MethodHandle dynamicInvoker();
   191 }