rt/emul/compact/src/main/java/java/lang/invoke/package-info.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 09 Aug 2014 11:11:13 +0200
branchjdk8-b132
changeset 1646 c880a8a8803b
permissions -rw-r--r--
Batch of classes necessary to implement invoke dynamic interfaces. Taken from JDK8 build 132
jaroslav@1646
     1
/*
jaroslav@1646
     2
 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
jaroslav@1646
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jaroslav@1646
     4
 *
jaroslav@1646
     5
 * This code is free software; you can redistribute it and/or modify it
jaroslav@1646
     6
 * under the terms of the GNU General Public License version 2 only, as
jaroslav@1646
     7
 * published by the Free Software Foundation.  Oracle designates this
jaroslav@1646
     8
 * particular file as subject to the "Classpath" exception as provided
jaroslav@1646
     9
 * by Oracle in the LICENSE file that accompanied this code.
jaroslav@1646
    10
 *
jaroslav@1646
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
jaroslav@1646
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jaroslav@1646
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
jaroslav@1646
    14
 * version 2 for more details (a copy is included in the LICENSE file that
jaroslav@1646
    15
 * accompanied this code).
jaroslav@1646
    16
 *
jaroslav@1646
    17
 * You should have received a copy of the GNU General Public License version
jaroslav@1646
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
jaroslav@1646
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jaroslav@1646
    20
 *
jaroslav@1646
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jaroslav@1646
    22
 * or visit www.oracle.com if you need additional information or have any
jaroslav@1646
    23
 * questions.
jaroslav@1646
    24
 */
jaroslav@1646
    25
jaroslav@1646
    26
/**
jaroslav@1646
    27
 * The {@code java.lang.invoke} package contains dynamic language support provided directly by
jaroslav@1646
    28
 * the Java core class libraries and virtual machine.
jaroslav@1646
    29
 *
jaroslav@1646
    30
 * <p>
jaroslav@1646
    31
 * As described in the Java Virtual Machine Specification,
jaroslav@1646
    32
 * certain types in this package have special relations to dynamic
jaroslav@1646
    33
 * language support in the virtual machine:
jaroslav@1646
    34
 * <ul>
jaroslav@1646
    35
 * <li>The class {@link java.lang.invoke.MethodHandle MethodHandle} contains
jaroslav@1646
    36
 * <a href="MethodHandle.html#sigpoly">signature polymorphic methods</a>
jaroslav@1646
    37
 * which can be linked regardless of their type descriptor.
jaroslav@1646
    38
 * Normally, method linkage requires exact matching of type descriptors.
jaroslav@1646
    39
 * </li>
jaroslav@1646
    40
 *
jaroslav@1646
    41
 * <li>The JVM bytecode format supports immediate constants of
jaroslav@1646
    42
 * the classes {@link java.lang.invoke.MethodHandle MethodHandle} and {@link java.lang.invoke.MethodType MethodType}.
jaroslav@1646
    43
 * </li>
jaroslav@1646
    44
 * </ul>
jaroslav@1646
    45
 *
jaroslav@1646
    46
 * <h1><a name="jvm_mods"></a>Summary of relevant Java Virtual Machine changes</h1>
jaroslav@1646
    47
 * The following low-level information summarizes relevant parts of the
jaroslav@1646
    48
 * Java Virtual Machine specification.  For full details, please see the
jaroslav@1646
    49
 * current version of that specification.
jaroslav@1646
    50
 *
jaroslav@1646
    51
 * Each occurrence of an {@code invokedynamic} instruction is called a <em>dynamic call site</em>.
jaroslav@1646
    52
 * <h2><a name="indyinsn"></a>{@code invokedynamic} instructions</h2>
jaroslav@1646
    53
 * A dynamic call site is originally in an unlinked state.  In this state, there is
jaroslav@1646
    54
 * no target method for the call site to invoke.
jaroslav@1646
    55
 * <p>
jaroslav@1646
    56
 * Before the JVM can execute a dynamic call site (an {@code invokedynamic} instruction),
jaroslav@1646
    57
 * the call site must first be <em>linked</em>.
jaroslav@1646
    58
 * Linking is accomplished by calling a <em>bootstrap method</em>
jaroslav@1646
    59
 * which is given the static information content of the call site,
jaroslav@1646
    60
 * and which must produce a {@link java.lang.invoke.MethodHandle method handle}
jaroslav@1646
    61
 * that gives the behavior of the call site.
jaroslav@1646
    62
 * <p>
jaroslav@1646
    63
 * Each {@code invokedynamic} instruction statically specifies its own
jaroslav@1646
    64
 * bootstrap method as a constant pool reference.
jaroslav@1646
    65
 * The constant pool reference also specifies the call site's name and type descriptor,
jaroslav@1646
    66
 * just like {@code invokevirtual} and the other invoke instructions.
jaroslav@1646
    67
 * <p>
jaroslav@1646
    68
 * Linking starts with resolving the constant pool entry for the
jaroslav@1646
    69
 * bootstrap method, and resolving a {@link java.lang.invoke.MethodType MethodType} object for
jaroslav@1646
    70
 * the type descriptor of the dynamic call site.
jaroslav@1646
    71
 * This resolution process may trigger class loading.
jaroslav@1646
    72
 * It may therefore throw an error if a class fails to load.
jaroslav@1646
    73
 * This error becomes the abnormal termination of the dynamic
jaroslav@1646
    74
 * call site execution.
jaroslav@1646
    75
 * Linkage does not trigger class initialization.
jaroslav@1646
    76
 * <p>
jaroslav@1646
    77
 * The bootstrap method is invoked on at least three values:
jaroslav@1646
    78
 * <ul>
jaroslav@1646
    79
 * <li>a {@code MethodHandles.Lookup}, a lookup object on the <em>caller class</em> in which dynamic call site occurs </li>
jaroslav@1646
    80
 * <li>a {@code String}, the method name mentioned in the call site </li>
jaroslav@1646
    81
 * <li>a {@code MethodType}, the resolved type descriptor of the call </li>
jaroslav@1646
    82
 * <li>optionally, between 1 and 251 additional static arguments taken from the constant pool </li>
jaroslav@1646
    83
 * </ul>
jaroslav@1646
    84
 * Invocation is as if by
jaroslav@1646
    85
 * {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke}.
jaroslav@1646
    86
 * The returned result must be a {@link java.lang.invoke.CallSite CallSite} (or a subclass).
jaroslav@1646
    87
 * The type of the call site's target must be exactly equal to the type
jaroslav@1646
    88
 * derived from the dynamic call site's type descriptor and passed to
jaroslav@1646
    89
 * the bootstrap method.
jaroslav@1646
    90
 * The call site then becomes permanently linked to the dynamic call site.
jaroslav@1646
    91
 * <p>
jaroslav@1646
    92
 * As documented in the JVM specification, all failures arising from
jaroslav@1646
    93
 * the linkage of a dynamic call site are reported
jaroslav@1646
    94
 * by a {@link java.lang.BootstrapMethodError BootstrapMethodError},
jaroslav@1646
    95
 * which is thrown as the abnormal termination of the dynamic call
jaroslav@1646
    96
 * site execution.
jaroslav@1646
    97
 * If this happens, the same error will the thrown for all subsequent
jaroslav@1646
    98
 * attempts to execute the dynamic call site.
jaroslav@1646
    99
 *
jaroslav@1646
   100
 * <h2>timing of linkage</h2>
jaroslav@1646
   101
 * A dynamic call site is linked just before its first execution.
jaroslav@1646
   102
 * The bootstrap method call implementing the linkage occurs within
jaroslav@1646
   103
 * a thread that is attempting a first execution.
jaroslav@1646
   104
 * <p>
jaroslav@1646
   105
 * If there are several such threads, the bootstrap method may be
jaroslav@1646
   106
 * invoked in several threads concurrently.
jaroslav@1646
   107
 * Therefore, bootstrap methods which access global application
jaroslav@1646
   108
 * data must take the usual precautions against race conditions.
jaroslav@1646
   109
 * In any case, every {@code invokedynamic} instruction is either
jaroslav@1646
   110
 * unlinked or linked to a unique {@code CallSite} object.
jaroslav@1646
   111
 * <p>
jaroslav@1646
   112
 * In an application which requires dynamic call sites with individually
jaroslav@1646
   113
 * mutable behaviors, their bootstrap methods should produce distinct
jaroslav@1646
   114
 * {@link java.lang.invoke.CallSite CallSite} objects, one for each linkage request.
jaroslav@1646
   115
 * Alternatively, an application can link a single {@code CallSite} object
jaroslav@1646
   116
 * to several {@code invokedynamic} instructions, in which case
jaroslav@1646
   117
 * a change to the target method will become visible at each of
jaroslav@1646
   118
 * the instructions.
jaroslav@1646
   119
 * <p>
jaroslav@1646
   120
 * If several threads simultaneously execute a bootstrap method for a single dynamic
jaroslav@1646
   121
 * call site, the JVM must choose one {@code CallSite} object and install it visibly to
jaroslav@1646
   122
 * all threads.  Any other bootstrap method calls are allowed to complete, but their
jaroslav@1646
   123
 * results are ignored, and their dynamic call site invocations proceed with the originally
jaroslav@1646
   124
 * chosen target object.
jaroslav@1646
   125
jaroslav@1646
   126
 * <p style="font-size:smaller;">
jaroslav@1646
   127
 * <em>Discussion:</em>
jaroslav@1646
   128
 * These rules do not enable the JVM to duplicate dynamic call sites,
jaroslav@1646
   129
 * or to issue &ldquo;causeless&rdquo; bootstrap method calls.
jaroslav@1646
   130
 * Every dynamic call site transitions at most once from unlinked to linked,
jaroslav@1646
   131
 * just before its first invocation.
jaroslav@1646
   132
 * There is no way to undo the effect of a completed bootstrap method call.
jaroslav@1646
   133
 *
jaroslav@1646
   134
 * <h2>types of bootstrap methods</h2>
jaroslav@1646
   135
 * As long as each bootstrap method can be correctly invoked
jaroslav@1646
   136
 * by {@code MethodHandle.invoke}, its detailed type is arbitrary.
jaroslav@1646
   137
 * For example, the first argument could be {@code Object}
jaroslav@1646
   138
 * instead of {@code MethodHandles.Lookup}, and the return type
jaroslav@1646
   139
 * could also be {@code Object} instead of {@code CallSite}.
jaroslav@1646
   140
 * (Note that the types and number of the stacked arguments limit
jaroslav@1646
   141
 * the legal kinds of bootstrap methods to appropriately typed
jaroslav@1646
   142
 * static methods and constructors of {@code CallSite} subclasses.)
jaroslav@1646
   143
 * <p>
jaroslav@1646
   144
 * If a given {@code invokedynamic} instruction specifies no static arguments,
jaroslav@1646
   145
 * the instruction's bootstrap method will be invoked on three arguments,
jaroslav@1646
   146
 * conveying the instruction's caller class, name, and method type.
jaroslav@1646
   147
 * If the {@code invokedynamic} instruction specifies one or more static arguments,
jaroslav@1646
   148
 * those values will be passed as additional arguments to the method handle.
jaroslav@1646
   149
 * (Note that because there is a limit of 255 arguments to any method,
jaroslav@1646
   150
 * at most 251 extra arguments can be supplied, since the bootstrap method
jaroslav@1646
   151
 * handle itself and its first three arguments must also be stacked.)
jaroslav@1646
   152
 * The bootstrap method will be invoked as if by either {@code MethodHandle.invoke}
jaroslav@1646
   153
 * or {@code invokeWithArguments}.  (There is no way to tell the difference.)
jaroslav@1646
   154
 * <p>
jaroslav@1646
   155
 * The normal argument conversion rules for {@code MethodHandle.invoke} apply to all stacked arguments.
jaroslav@1646
   156
 * For example, if a pushed value is a primitive type, it may be converted to a reference by boxing conversion.
jaroslav@1646
   157
 * If the bootstrap method is a variable arity method (its modifier bit {@code 0x0080} is set),
jaroslav@1646
   158
 * then some or all of the arguments specified here may be collected into a trailing array parameter.
jaroslav@1646
   159
 * (This is not a special rule, but rather a useful consequence of the interaction
jaroslav@1646
   160
 * between {@code CONSTANT_MethodHandle} constants, the modifier bit for variable arity methods,
jaroslav@1646
   161
 * and the {@link java.lang.invoke.MethodHandle#asVarargsCollector asVarargsCollector} transformation.)
jaroslav@1646
   162
 * <p>
jaroslav@1646
   163
 * Given these rules, here are examples of legal bootstrap method declarations,
jaroslav@1646
   164
 * given various numbers {@code N} of extra arguments.
jaroslav@1646
   165
 * The first rows (marked {@code *}) will work for any number of extra arguments.
jaroslav@1646
   166
 * <table border=1 cellpadding=5 summary="Static argument types">
jaroslav@1646
   167
 * <tr><th>N</th><th>sample bootstrap method</th></tr>
jaroslav@1646
   168
 * <tr><td>*</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code></td></tr>
jaroslav@1646
   169
 * <tr><td>*</td><td><code>CallSite bootstrap(Object... args)</code></td></tr>
jaroslav@1646
   170
 * <tr><td>*</td><td><code>CallSite bootstrap(Object caller, Object... nameAndTypeWithArgs)</code></td></tr>
jaroslav@1646
   171
 * <tr><td>0</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type)</code></td></tr>
jaroslav@1646
   172
 * <tr><td>0</td><td><code>CallSite bootstrap(Lookup caller, Object... nameAndType)</code></td></tr>
jaroslav@1646
   173
 * <tr><td>1</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object arg)</code></td></tr>
jaroslav@1646
   174
 * <tr><td>2</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code></td></tr>
jaroslav@1646
   175
 * <tr><td>2</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String... args)</code></td></tr>
jaroslav@1646
   176
 * <tr><td>2</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String x, int y)</code></td></tr>
jaroslav@1646
   177
 * </table>
jaroslav@1646
   178
 * The last example assumes that the extra arguments are of type
jaroslav@1646
   179
 * {@code CONSTANT_String} and {@code CONSTANT_Integer}, respectively.
jaroslav@1646
   180
 * The second-to-last example assumes that all extra arguments are of type
jaroslav@1646
   181
 * {@code CONSTANT_String}.
jaroslav@1646
   182
 * The other examples work with all types of extra arguments.
jaroslav@1646
   183
 * <p>
jaroslav@1646
   184
 * As noted above, the actual method type of the bootstrap method can vary.
jaroslav@1646
   185
 * For example, the fourth argument could be {@code MethodHandle},
jaroslav@1646
   186
 * if that is the type of the corresponding constant in
jaroslav@1646
   187
 * the {@code CONSTANT_InvokeDynamic} entry.
jaroslav@1646
   188
 * In that case, the {@code MethodHandle.invoke} call will pass the extra method handle
jaroslav@1646
   189
 * constant as an {@code Object}, but the type matching machinery of {@code MethodHandle.invoke}
jaroslav@1646
   190
 * will cast the reference back to {@code MethodHandle} before invoking the bootstrap method.
jaroslav@1646
   191
 * (If a string constant were passed instead, by badly generated code, that cast would then fail,
jaroslav@1646
   192
 * resulting in a {@code BootstrapMethodError}.)
jaroslav@1646
   193
 * <p>
jaroslav@1646
   194
 * Note that, as a consequence of the above rules, the bootstrap method may accept a primitive
jaroslav@1646
   195
 * argument, if it can be represented by a constant pool entry.
jaroslav@1646
   196
 * However, arguments of type {@code boolean}, {@code byte}, {@code short}, or {@code char}
jaroslav@1646
   197
 * cannot be created for bootstrap methods, since such constants cannot be directly
jaroslav@1646
   198
 * represented in the constant pool, and the invocation of the bootstrap method will
jaroslav@1646
   199
 * not perform the necessary narrowing primitive conversions.
jaroslav@1646
   200
 * <p>
jaroslav@1646
   201
 * Extra bootstrap method arguments are intended to allow language implementors
jaroslav@1646
   202
 * to safely and compactly encode metadata.
jaroslav@1646
   203
 * In principle, the name and extra arguments are redundant,
jaroslav@1646
   204
 * since each call site could be given its own unique bootstrap method.
jaroslav@1646
   205
 * Such a practice is likely to produce large class files and constant pools.
jaroslav@1646
   206
 *
jaroslav@1646
   207
 * @author John Rose, JSR 292 EG
jaroslav@1646
   208
 * @since 1.7
jaroslav@1646
   209
 */
jaroslav@1646
   210
jaroslav@1646
   211
package java.lang.invoke;