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 MethodType} or {@code Class} 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, in general, 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>for a dynamically-computed call site a {@code MethodType} (the resolved
 113  * type descriptor of the call site), or for a dynamically computed constant,
 114  * a {@code Class} (the resolved type descriptor of the constant)</li>
 115  * <li>the additional resolved static arguments, if any</li>
 116  * </ul>
 117  * For dynamically computed constant, if the bootstrap method has no parameters
 118  * or the first parameter type is not assignable to {@code MethodHandles.Lookup}
 119  * then the method is invoked as described above but with just the arguments
 120  * that are the additional resolved static arguments, if any.
 121  * <p>
 122  * For a dynamically-computed call site, the returned result must be a non-null reference to a
 123  * {@link java.lang.invoke.CallSite CallSite}.
 124  * The type of the call site's target must be exactly equal to the type
 125  * derived from the invocation's type descriptor and passed to
 126  * the bootstrap method. If these conditions are not met, a {@code BootstrapMethodError} is thrown.
 127  * On success the call site then becomes permanently linked to the {@code invokedynamic}
 128  * instruction.
 129  * <p>
 130  * For a dynamically-computed constant, the result of the bootstrap method is
 131  * cached as the resolved constant value.  The constant value must be
 132  * convertible to the {@code Class} derived from type component of the
 133  * {@code CONSTANT_NameAndType} descriptor, otherwise a
 134  * {@code BootstrapMethodError} is thrown.
 135  * <p>
 136  * If an exception, {@code E} say, occurs during execution of the bootstrap method, then
 137  * resolution fails and terminates abnormally. {@code E} is rethrown if the type of
 138  * {@code E} is {@code Error} or a subclass, otherwise a
 139  * {@code BootstrapMethodError} that wraps {@code E} is thrown.
 140  * If this happens, the same error will be thrown for all
 141  * subsequent attempts to execute the {@code invokedynamic} instruction or load the
 142  * dynamically-computed constant.
 143  *
 144  * <h3>Timing of resolution</h3>
 145  * An {@code invokedynamic} instruction is linked just before its first execution.
 146  * A dynamically-computed constant is resolved just before the first time it is used
 147  * (by pushing it on the stack or linking it as a bootstrap method parameter).
 148  * The bootstrap method call implementing the linkage occurs within
 149  * a thread that is attempting a first execution or first use.
 150  * <p>
 151  * If there are several such threads, the bootstrap method may be
 152  * invoked in several threads concurrently.
 153  * Therefore, bootstrap methods which access global application
 154  * data must take the usual precautions against race conditions.
 155  * In any case, every {@code invokedynamic} instruction is either
 156  * unlinked or linked to a unique {@code CallSite} object.
 157  * <p>
 158  * In an application which requires {@code invokedynamic} instructions with individually
 159  * mutable behaviors, their bootstrap methods should produce distinct
 160  * {@link java.lang.invoke.CallSite CallSite} objects, one for each linkage request.
 161  * Alternatively, an application can link a single {@code CallSite} object
 162  * to several {@code invokedynamic} instructions, in which case
 163  * a change to the target method will become visible at each of
 164  * the instructions.
 165  * <p>
 166  * If several threads simultaneously execute a bootstrap method for a single dynamically-computed
 167  * call site or constant, the JVM must choose one bootstrap method result and install it visibly to
 168  * all threads.  Any other bootstrap method calls are allowed to complete, but their
 169  * results are ignored.
 170 
 171  * <p style="font-size:smaller;">
 172  * <em>Discussion:</em>
 173  * These rules do not enable the JVM to share call sites,
 174  * or to issue &ldquo;causeless&rdquo; bootstrap method calls.
 175  * Every {@code invokedynamic} instruction transitions at most once from unlinked to linked,
 176  * just before its first invocation.
 177  * There is no way to undo the effect of a completed bootstrap method call.
 178  *
 179  * <h3>Types of bootstrap methods</h3>
 180  * For a dynamically-computed call site, the bootstrap method is invoked with parameter
 181  * types {@code MethodHandles.Lookup}, {@code String}, {@code MethodType}, and the types
 182  * of any static arguments; the return type is {@code CallSite}.
 183  * <p>
 184  * For a dynamically-computed constant, if the first parameter of the bootstrap
 185  * method is assignable to {@code MethodHandles.Lookup} then the bootstrap
 186  * method is invoked with parameter types {@code MethodHandles.Lookup},
 187  * {@code String}, {@code Class}, and the types of any static arguments.
 188  * Otherwise, the bootstrap method is invoked with just the parameter types of
 189  * any static arguments.  In either case the return type is the type represented
 190  * by the {@code Class} (regardless of whether the bootstrap is invoked with
 191  * just the static arguments).
 192  * <p>
 193  * Because {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke} allows for
 194  * adaptations between the invoked method type and the bootstrap method handle's method type,
 195  * there is flexibility in the declaration of the bootstrap method.
 196  * <p> For example, for dynamically-computed call site, a the first argument
 197  * could be {@code Object} instead of {@code MethodHandles.Lookup}, and the return type
 198  * could also be {@code Object} instead of {@code CallSite}.
 199  * (Note that the types and number of the stacked arguments limit
 200  * the legal kinds of bootstrap methods to appropriately typed
 201  * static methods and constructors.)
 202  * <p>
 203  * If a pushed value is a primitive type, it may be converted to a reference by boxing conversion.
 204  * If the bootstrap method is a variable arity method (its modifier bit {@code 0x0080} is set),
 205  * then some or all of the arguments specified here may be collected into a trailing array parameter.
 206  * (This is not a special rule, but rather a useful consequence of the interaction
 207  * between {@code CONSTANT_MethodHandle} constants, the modifier bit for variable arity methods,
 208  * and the {@link java.lang.invoke.MethodHandle#asVarargsCollector asVarargsCollector} transformation.)
 209  * <p>
 210  * Given these rules the following examples and explanations are presented for
 211  * legal bootstrap method declarations.
 212  * Here are examples of legal bootstrap method declarations for
 213  * dynamically-computed call sites, given various numbers {@code N} of extra arguments.
 214  * The first row (marked {@code *}) will work for any number of extra arguments.
 215  * <table class="plain" style="vertical-align:top">
 216  * <caption style="display:none">Static argument types</caption>
 217  * <thead>
 218  * <tr><th scope="col">N</th><th scope="col">Sample bootstrap method for a dynamically-computed call site</th></tr>
 219  * </thead>
 220  * <tbody>
 221  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">*</th><td>
 222  *     <ul style="list-style:none; padding-left: 0; margin:0">
 223  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code>
 224  *     <li><code>CallSite bootstrap(Object... args)</code>
 225  *     <li><code>CallSite bootstrap(Object caller, Object... nameAndTypeWithArgs)</code>
 226  *     </ul></td></tr>
 227  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">0</th><td>
 228  *     <ul style="list-style:none; padding-left: 0; margin:0">
 229  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type)</code>
 230  *     <li><code>CallSite bootstrap(Lookup caller, Object... nameAndType)</code>
 231  *     </ul></td></tr>
 232  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">1</th><td>
 233  *     <code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object arg)</code></td></tr>
 234  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">2</th><td>
 235  *     <ul style="list-style:none; padding-left: 0; margin:0">
 236  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code>
 237  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String... args)</code>
 238  *     <li><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String x, int y)</code>
 239  *     </ul></td></tr>
 240  * </tbody>
 241  * </table>
 242  * The last example assumes that the extra arguments are of type
 243  * {@code String} and {@code Integer} (or {@code int}), respectively.
 244  * The second-to-last example assumes that all extra arguments are of type
 245  * {@code String}.
 246  * The other examples work with all types of extra arguments.
 247  * <p>
 248  * Since dynamically-computed constants can be provided as static arguments to
 249  * bootstrap methods for both dynamically-computed call sites and
 250  * dynamically-computed constants, there are no limitations on the types of
 251  * bootstrap arguments.  However, arguments of type {@code boolean},
 252  * {@code byte}, {@code short}, or {@code char} cannot be <em>directly</em>
 253  * supplied by {@code CONSTANT_Integer} constant pool entries, since the
 254  * {@code asType} conversions do not perform the necessary narrowing primitive
 255  * conversions.
 256  * <p>
 257  * In the above examples, the return type is always {@code CallSite}, but that
 258  * is not a necessary feature of call site bootstrap methods.  The only
 259  * requirement is that the return type of the bootstrap method must be
 260  * convertible (using the {@code asType} conversions) to {@code CallSite}, which
 261  * means the bootstrap method return type might be {@code Object} or
 262  * {@code ConstantCallSite}.
 263  * <p>
 264  * Here are examples of legal bootstrap method declarations for
 265  * dynamically-computed constants, given various numbers {@code N} of extra
 266  * arguments.  The first row (marked {@code *}) will work for any number of
 267  * extra arguments.
 268  * <table class="plain" style="vertical-align:top">
 269  * <caption style="display:none">Static argument types</caption>
 270  * <thead>
 271  * <tr><th scope="col">N</th><th scope="col">Sample bootstrap method for a dynamically-computed constant</th></tr>
 272  * </thead>
 273  * <tbody>
 274  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">*</th><td>
 275  *     <ul style="list-style:none; padding-left: 0; margin:0">
 276  *     <li><code>Object bootstrap(Lookup caller, String name, Class type, Object... args)</code>
 277  *     <li><code>Object bootstrap(Object... args)</code>
 278  *     <li><code>Object bootstrap(Object firstArg, Object... otherArgs)</code>
 279  *     </ul></td></tr>
 280  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">0</th><td>
 281  *     <ul style="list-style:none; padding-left: 0; margin:0">
 282  *     <li><code>Object bootstrap(Lookup caller, String name, Class type)</code>
 283  *     <li><code>Object bootstrap(Lookup caller, Object... nameAndType)</code>
 284  *     <li><code>Object bootstrap()</code>
 285  *     <li><code>Object bootstrap(Object... args)</code>
 286  *     </ul></td></tr>
 287  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">1</th><td>
 288  *     <ul style="list-style:none; padding-left: 0; margin:0">
 289  *     <li><code>Object bootstrap(Lookup caller, String name, Class type, Object arg)</code>
 290  *     <li><code>Object bootstrap(Object arg)</code>
 291  *     </ul></td></tr>
 292  * <tr><th scope="row" style="font-weight:normal; vertical-align:top">2</th><td>
 293  *     <ul style="list-style:none; padding-left: 0; margin:0">
 294  *     <li><code>Object bootstrap(Lookup caller, String name, Class type, Object... args)</code>
 295  *     <li><code>Object bootstrap(Lookup caller, String name, Class type, String... args)</code>
 296  *     <li><code>Object bootstrap(Lookup caller, String name, Class type, String x, int y)</code>
 297  *     <li><code>Object bootstrap(Object... args)</code>
 298  *     <li><code>Object bootstrap(String... args)</code>
 299  *     <li><code>Object bootstrap(String x, int y)</code>
 300  *     </ul></td></tr>
 301  * </tbody>
 302  * </table>
 303  * The example methods whose first parameter type, if any, is not {@code Lookup}
 304  * will be invoked with just the static arguments.
 305  * <p>
 306  * In the above examples, the return type is always {@code Object}, but that
 307  * is not a necessary feature of dynamic constant bootstrap methods.  The only
 308  * requirement is that method must be convertible to the type of the constant,
 309  * as represented by its field type descriptor.  For example, if the dynamic
 310  * constant has a field type descriptor of {@code "C"} ({@code char}) then the
 311  * bootstrap method return type could be {@code Object}, {@code Character}, or
 312  * {@code char}, but not {@code int} or {@code Integer}.
 313  *
 314  * @author John Rose, JSR 292 EG
 315  * @since 1.7
 316  */
 317 
 318 package java.lang.invoke;