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