1 /*
   2  * Copyright (c) 2008, 2019, 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 /**
  27  * The {@code java.lang.invoke} package provides low-level primitives for interacting
  28  * with the Java Virtual Machine.
  29  *
  30  * <p>
  31  * As described in the Java Virtual Machine Specification, certain types in this package
  32  * are given special treatment by the virtual machine:
  33  * <ul>
  34  * <li>The classes {@link java.lang.invoke.MethodHandle MethodHandle}
  35  * {@link java.lang.invoke.VarHandle VarHandle} contain
  36  * <a href="MethodHandle.html#sigpoly">signature polymorphic methods</a>
  37  * which can be linked regardless of their type descriptor.
  38  * Normally, method linkage requires exact matching of type descriptors.
  39  * </li>
  40  *
  41  * <li>The JVM bytecode format supports immediate constants of
  42  * the classes {@link java.lang.invoke.MethodHandle MethodHandle} and
  43  * {@link java.lang.invoke.MethodType MethodType}.
  44  * </li>
  45  *
  46  * <li>The {@code invokedynamic} instruction makes use of bootstrap {@code MethodHandle}
  47  * constants to dynamically resolve {@code CallSite} objects for custom method invocation
  48  * behavior.
  49  * </li>
  50  *
  51  * <li>The {@code ldc} instruction makes use of bootstrap {@code MethodHandle} constants
  52  * to dynamically resolve custom constant values.
  53  * </li>
  54  * </ul>
  55  *
  56  * <h2><a id="jvm_mods"></a>Dynamic resolution of call sites and constants</h2>
  57  * The following low-level information summarizes relevant parts of the
  58  * Java Virtual Machine specification.  For full details, please see the
  59  * current version of that specification.
  60  *
  61  * <h3><a id="indyinsn"></a>Dynamically-computed call sites</h3>
  62  * An {@code invokedynamic} instruction is originally in an unlinked state.
  63  * In this state, there is no target method for the instruction to invoke.
  64  * <p>
  65  * Before the JVM can execute an {@code invokedynamic} instruction,
  66  * the instruction must first be <em>linked</em>.
  67  * Linking is accomplished by calling a <em>bootstrap method</em>
  68  * which is given the static information content of the call,
  69  * and which must produce a {@link java.lang.invoke.CallSite}
  70  * that gives the behavior of the invocation.
  71  * <p>
  72  * Each {@code invokedynamic} instruction statically specifies its own
  73  * bootstrap method as a constant pool reference.
  74  * The constant pool reference also specifies the invocation's name and method type descriptor,
  75  * just like {@code invokestatic} and the other invoke instructions.
  76  *
  77  * <h3><a id="condycon"></a>Dynamically-computed constants</h3>
  78  * The constant pool may contain constants tagged {@code CONSTANT_Dynamic},
  79  * equipped with bootstrap methods which perform their resolution.
  80  * Such a <em>dynamic constant</em> is originally in an unresolved state.
  81  * Before the JVM can use a dynamically-computed constant, it must first be <em>resolved</em>.
  82  * Dynamically-computed constant resolution is accomplished by calling a <em>bootstrap method</em>
  83  * which is given the static information content of the constant,
  84  * and which must produce a value of the constant's statically declared type.
  85  * <p>
  86  * Each dynamically-computed constant statically specifies its own
  87  * bootstrap method as a constant pool reference.
  88  * The constant pool reference also specifies the constant's name and field type descriptor,
  89  * just like {@code getstatic} and the other field reference instructions.
  90  * (Roughly speaking, a dynamically-computed constant is to a dynamically-computed call site
  91  * as a {@code CONSTANT_Fieldref} is to a {@code CONSTANT_Methodref}.)
  92  *
  93  * <h3><a id="bsm"></a>Execution of bootstrap methods</h3>
  94  * Resolving a dynamically-computed call site or constant
  95  * starts with resolving constants from the constant pool for the
  96  * following items:
  97  * <ul>
  98  * <li>the bootstrap method, a {@code CONSTANT_MethodHandle}</li>
  99  * <li>the {@code Class} or {@code MethodType} derived from
 100  * type component of the {@code CONSTANT_NameAndType} descriptor</li>
 101  * <li>static arguments, if any (note that static arguments can themselves be
 102  * dynamically-computed constants)</li>
 103  * </ul>
 104  * <p>
 105  * The bootstrap method is then invoked, as if by
 106  * {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke},
 107  * with the following arguments:
 108  * <ul>
 109  * <li>a {@code MethodHandles.Lookup}, which is a lookup object on the <em>caller class</em>
 110  * in which dynamically-computed constant or call site occurs</li>
 111  * <li>a {@code String}, the name mentioned in the {@code CONSTANT_NameAndType}</li>
 112  * <li>a {@code MethodType} or {@code Class}, the resolved type descriptor of the {@code CONSTANT_NameAndType}</li>
 113  * <li>a {@code Class}, the resolved type descriptor of the constant, if it is a dynamic constant </li>
 114  * <li>the additional resolved static arguments, if any</li>
 115  * </ul>
 116  * <p>
 117  * For a dynamically-computed call site, the returned result must be a non-null reference to a
 118  * {@link java.lang.invoke.CallSite CallSite}.
 119  * The type of the call site's target must be exactly equal to the type
 120  * derived from the invocation's type descriptor and passed to
 121  * the bootstrap method. If these conditions are not met, a {@code BootstrapMethodError} is thrown.
 122  * On success the call site then becomes permanently linked to the {@code invokedynamic}
 123  * instruction.
 124  * <p>
 125  * For a dynamically-computed constant, the first parameter of the bootstrap
 126  * method must be assignable to {@code MethodHandles.Lookup}. If this condition
 127  * is not met, a {@code BootstrapMethodError} is thrown.
 128  * On success the result of the bootstrap method is cached as the resolved
 129  * constant value.
 130  * <p>
 131  * If an exception, {@code E} say, occurs during execution of the bootstrap method, then
 132  * resolution fails and terminates abnormally. {@code E} is rethrown if the type of
 133  * {@code E} is {@code Error} or a subclass, otherwise a
 134  * {@code BootstrapMethodError} that wraps {@code E} is thrown.
 135  * If this happens, the same error will be thrown for all
 136  * subsequent attempts to execute the {@code invokedynamic} instruction or load the
 137  * dynamically-computed constant.
 138  *
 139  * <h3>Timing of resolution</h3>
 140  * An {@code invokedynamic} instruction is linked just before its first execution.
 141  * A dynamically-computed constant is resolved just before the first time it is used
 142  * (by pushing it on the stack or linking it as a bootstrap method parameter).
 143  * The bootstrap method call implementing the linkage occurs within
 144  * a thread that is attempting a first execution or first use.
 145  * <p>
 146  * If there are several such threads, the bootstrap method may be
 147  * invoked in several threads concurrently.
 148  * Therefore, bootstrap methods which access global application
 149  * data must take the usual precautions against race conditions.
 150  * In any case, every {@code invokedynamic} instruction is either
 151  * unlinked or linked to a unique {@code CallSite} object.
 152  * <p>
 153  * In an application which requires {@code invokedynamic} instructions with individually
 154  * mutable behaviors, their bootstrap methods should produce distinct
 155  * {@link java.lang.invoke.CallSite CallSite} objects, one for each linkage request.
 156  * Alternatively, an application can link a single {@code CallSite} object
 157  * to several {@code invokedynamic} instructions, in which case
 158  * a change to the target method will become visible at each of
 159  * the instructions.
 160  * <p>
 161  * If several threads simultaneously execute a bootstrap method for a single dynamically-computed
 162  * call site or constant, the JVM must choose one bootstrap method result and install it visibly to
 163  * all threads.  Any other bootstrap method calls are allowed to complete, but their
 164  * results are ignored.
 165 
 166  * <p style="font-size:smaller;">
 167  * <em>Discussion:</em>
 168  * These rules do not enable the JVM to share call sites,
 169  * or to issue &ldquo;causeless&rdquo; bootstrap method calls.
 170  * Every {@code invokedynamic} instruction transitions at most once from unlinked to linked,
 171  * just before its first invocation.
 172  * There is no way to undo the effect of a completed bootstrap method call.
 173  *
 174  * <h3>Types of bootstrap methods</h3>
 175  * For a dynamically-computed call site, the bootstrap method is invoked with parameter
 176  * types {@code MethodHandles.Lookup}, {@code String}, {@code MethodType}, and the types
 177  * of any static arguments; the return type is {@code CallSite}.
 178  * <p>
 179  * For a dynamically-computed constant, the bootstrap method is invoked with parameter types
 180  * {@code MethodHandles.Lookup}, {@code String}, {@code Class}, and the types of any
 181  * static arguments; the return type is the type represented by the {@code Class}.
 182  * <p>
 183  * Because {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke} allows for
 184  * adaptations between the invoked method type and the bootstrap method handle's method type,
 185  * there is flexibility in the declaration of the bootstrap method.
 186  * For a dynamically-computed constant the first parameter type of the bootstrap method handle
 187  * must be assignable to {@code MethodHandles.Lookup}, other than that constraint the same degree
 188  * of flexibility applies to bootstrap methods of dynamically-computed call sites and
 189  * dynamically-computed constants.
 190  * Note: this constraint allows for the future possibility where the bootstrap method is
 191  * invoked with just the parameter types of static arguments, thereby supporting a wider
 192  * range of methods compatible with the static arguments (such as methods that don't declare
 193  * or require the lookup, name, and type meta-data parameters).
 194  * <p> For example, for dynamically-computed call site, a the first argument
 195  * could be {@code Object} instead of {@code MethodHandles.Lookup}, and the return type
 196  * could also be {@code Object} instead of {@code CallSite}.
 197  * (Note that the types and number of the stacked arguments limit
 198  * the legal kinds of bootstrap methods to appropriately typed
 199  * static methods and constructors.)
 200  * <p>
 201  * If a pushed value is a primitive type, it may be converted to a reference by boxing conversion.
 202  * If the bootstrap method is a variable arity method (its modifier bit {@code 0x0080} is set),
 203  * then some or all of the arguments specified here may be collected into a trailing array parameter.
 204  * (This is not a special rule, but rather a useful consequence of the interaction
 205  * between {@code CONSTANT_MethodHandle} constants, the modifier bit for variable arity methods,
 206  * and the {@link java.lang.invoke.MethodHandle#asVarargsCollector asVarargsCollector} transformation.)
 207  * <p>
 208  * Given these rules, here are examples of legal bootstrap method declarations for
 209  * dynamically-computed call sites, given various numbers {@code N} of extra arguments.
 210  * The first row (marked {@code *}) will work for any number of extra arguments.
 211  * <table class="plain" style="vertical-align:top">
 212  * <caption style="display:none">Static argument types</caption>
 213  * <thead>
 214  * <tr><th scope="col">N</th><th scope="col">Sample bootstrap method</th></tr>
 215  * </thead>
 216  * <tbody>
 217  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">*</th><td>
 218  *     <ul style="list-style:none; padding-left: 0; margin:0">
 219  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code>
 220  *     <li><code>CallSite bootstrap(Object... args)</code>
 221  *     <li><code>CallSite bootstrap(Object caller, Object... nameAndTypeWithArgs)</code>
 222  *     </ul></td></tr>
 223  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">0</th><td>
 224  *     <ul style="list-style:none; padding-left: 0; margin:0">
 225  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type)</code>
 226  *     <li><code>CallSite bootstrap(Lookup caller, Object... nameAndType)</code>
 227  *     </ul></td></tr>
 228  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">1</th><td>
 229  *     <code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object arg)</code></td></tr>
 230  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">2</th><td>
 231  *     <ul style="list-style:none; padding-left: 0; margin:0">
 232  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code>
 233  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String... args)</code>
 234  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String x, int y)</code>
 235  *     </ul></td></tr>
 236  * </tbody>
 237  * </table>
 238  * The last example assumes that the extra arguments are of type
 239  * {@code String} and {@code Integer} (or {@code int}), respectively.
 240  * The second-to-last example assumes that all extra arguments are of type
 241  * {@code String}.
 242  * The other examples work with all types of extra arguments.  Note that all
 243  * the examples except the second and third also work with dynamically-computed
 244  * constants if the return type is changed to be compatible with the
 245  * constant's declared type (such as {@code Object}, which is always compatible).
 246  * <p>
 247  * Since dynamically-computed constants can be provided as static arguments to bootstrap
 248  * methods, there are no limitations on the types of bootstrap arguments.
 249  * However, arguments of type {@code boolean}, {@code byte}, {@code short}, or {@code char}
 250  * cannot be <em>directly</em> supplied by {@code CONSTANT_Integer}
 251  * constant pool entries, since the {@code asType} conversions do
 252  * not perform the necessary narrowing primitive conversions.
 253  * <p>
 254  * In the above examples, the return type is always {@code CallSite},
 255  * but that is not a necessary feature of bootstrap methods.
 256  * In the case of a dynamically-computed call site, the only requirement is that
 257  * the return type of the bootstrap method must be convertible
 258  * (using the {@code asType} conversions) to {@code CallSite}, which
 259  * means the bootstrap method return type might be {@code Object} or
 260  * {@code ConstantCallSite}.
 261  * In the case of a dynamically-resolved constant, the return type of the bootstrap
 262  * method must be convertible to the type of the constant, as
 263  * represented by its field type descriptor.  For example, if the
 264  * dynamic constant has a field type descriptor of {@code "C"}
 265  * ({@code char}) then the bootstrap method return type could be
 266  * {@code Object}, {@code Character}, or {@code char}, but not
 267  * {@code int} or {@code Integer}.
 268  *
 269  * @author John Rose, JSR 292 EG
 270  * @since 1.7
 271  */
 272 
 273 package java.lang.invoke;