jdk/src/share/classes/java/dyn/package-info.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 7562 a0ad195efe2c
child 8345 9e2483e6cfab
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 7562
diff changeset
     2
 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     4
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    10
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    15
 * accompanied this code).
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    16
 *
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4537
diff changeset
    23
 * questions.
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    24
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    25
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    26
/**
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    27
 * This package contains dynamic language support provided directly by
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
    28
 * the Java core class libraries and virtual machine.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    29
 * <p>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    30
 * Certain types in this package have special relations to dynamic
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    31
 * language support in the virtual machine:
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    32
 * <ul>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    33
 * <li>In source code, a call to
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    34
 * {@link java.dyn.MethodHandle#invokeExact   MethodHandle.invokeExact} or
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    35
 * {@link java.dyn.MethodHandle#invokeGeneric MethodHandle.invokeGeneric}
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    36
 * will compile and link, regardless of the requested type signature.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    37
 * As usual, the Java compiler emits an {@code invokevirtual}
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    38
 * instruction with the given signature against the named method.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    39
 * The JVM links any such call (regardless of signature) to a dynamically
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    40
 * typed method handle invocation.  In the case of {@code invokeGeneric},
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    41
 * argument and return value conversions are applied.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    42
 * </li>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    43
 *
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    44
 * <li>The JVM bytecode format supports immediate constants of
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    45
 * the classes {@link java.dyn.MethodHandle MethodHandle} and {@link java.dyn.MethodType MethodType}.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    46
 * </li>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    47
 * </ul>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    48
 *
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    49
 * <h2><a name="jvm_mods"></a>Corresponding JVM bytecode format changes</h2>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    50
 * <em>The following low-level information is presented here as a preview of
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    51
 * changes being made to the Java Virtual Machine specification for JSR 292.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    52
 * This information will be incorporated in a future version of the JVM specification.</em>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    53
 *
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
    54
 * <h3><a name="indyinsn"></a>{@code invokedynamic} instruction format</h3>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    55
 * In bytecode, an {@code invokedynamic} instruction is formatted as five bytes.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    56
 * The first byte is the opcode 186 (hexadecimal {@code BA}).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    57
 * The next two bytes are a constant pool index (in the same format as for the other {@code invoke} instructions).
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
    58
 * The final two bytes are reserved for future use and required to be zero.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    59
 * The constant pool reference of an {@code invokedynamic} instruction is to a entry
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    60
 * with tag {@code CONSTANT_InvokeDynamic} (decimal 18).  See below for its format.
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    61
 * (The tag value 17 is also temporarily allowed.  See below.)
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    62
 * The entry specifies the following information:
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    63
 * <ul>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    64
 * <li>a bootstrap method (a {@link java.dyn.MethodHandle MethodHandle} constant)</li>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    65
 * <li>the dynamic invocation name (a UTF8 string)</li>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    66
 * <li>the argument and return types of the call (encoded as a signature in a UTF8 string)</li>
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    67
 * <li>optionally, a sequence of additional <em>static arguments</em> to the bootstrap method ({@code ldc}-type constants)</li>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    68
 * </ul>
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    69
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    70
 * Each instance of an {@code invokedynamic} instruction is called a <em>dynamic call site</em>.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    71
 * Multiple instances of an {@code invokedynamic} instruction can share a single
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    72
 * {@code CONSTANT_InvokeDynamic} entry.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    73
 * In any case, distinct call sites always have distinct linkage state.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    74
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    75
 * A dynamic call site is originally in an unlinked state.  In this state, there is
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    76
 * no target method for the call site to invoke.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    77
 * A dynamic call site is linked by means of a bootstrap method,
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    78
 * as <a href="#bsm">described below</a>.
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    79
 *
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    80
 * <p style="font-size:smaller;">
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    81
 * (Historic Note: Some older JVMs may allow the index of a {@code CONSTANT_NameAndType}
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    82
 * instead of a {@code CONSTANT_InvokeDynamic}.  In earlier, obsolete versions of this API, the
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    83
 * bootstrap method was specified dynamically, in a per-class basis, during class initialization.)
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    84
 *
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
    85
 * <h3><a name="indycon"></a>constant pool entries for {@code invokedynamic} instructions</h3>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    86
 * If a constant pool entry has the tag {@code CONSTANT_InvokeDynamic} (decimal 18),
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    87
 * it must contain exactly four more bytes after the tag.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    88
 * These bytes are interpreted as two 16-bit indexes, in the usual {@code u2} format.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    89
 * The first pair of bytes after the tag must be an index into a side table called the
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    90
 * <em>bootstrap method table</em>, which is stored in the {@code BootstrapMethods}
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    91
 * attribute as <a href="#bsmattr">described below</a>.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    92
 * The second pair of bytes must be an index to a {@code CONSTANT_NameAndType}.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    93
 * This table is not part of the constant pool.  Instead, it is stored
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    94
 * in a class attribute named {@code BootstrapMethods}, described below.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
    95
 * <p>
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    96
 * The first index specifies a bootstrap method used by the associated dynamic call sites.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    97
 * The second index specifies the method name, argument types, and return type of the dynamic call site.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
    98
 * The structure of such an entry is therefore analogous to a {@code CONSTANT_Methodref},
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
    99
 * except that the bootstrap method specifier reference replaces
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   100
 * the {@code CONSTANT_Class} reference of a {@code CONSTANT_Methodref} entry.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   101
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   102
 * Some older JVMs may allow an older constant pool entry tag of decimal 17.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   103
 * The format and behavior of a constant pool entry with this tag is identical to
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   104
 * an entry with a tag of decimal 18, except that the first index refers directly
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   105
 * to a {@code CONSTANT_MethodHandle} to use as the bootstrap method.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   106
 * This format does not require the bootstrap method table.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   107
 *
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   108
 * <p style="font-size:smaller;">
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   109
 * <em>(Note: The Proposed Final Draft of this specification is likely to support
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   110
 * only the tag 18, not the tag 17.)</em>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   111
 *
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   112
 * <h3><a name="mtcon"></a>constant pool entries for {@linkplain java.dyn.MethodType method types}</h3>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   113
 * If a constant pool entry has the tag {@code CONSTANT_MethodType} (decimal 16),
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   114
 * it must contain exactly two more bytes, which must be an index to a {@code CONSTANT_Utf8}
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   115
 * entry which represents a method type signature.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   116
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   117
 * The JVM will ensure that on first
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   118
 * execution of an {@code ldc} instruction for this entry, a {@link java.dyn.MethodType MethodType}
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   119
 * will be created which represents the signature.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   120
 * Any classes mentioned in the {@code MethodType} will be loaded if necessary,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   121
 * but not initialized.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   122
 * Access checking and error reporting is performed exactly as it is for
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   123
 * references by {@code ldc} instructions to {@code CONSTANT_Class} constants.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   124
 *
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   125
 * <h3><a name="mhcon"></a>constant pool entries for {@linkplain java.dyn.MethodHandle method handles}</h3>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   126
 * If a constant pool entry has the tag {@code CONSTANT_MethodHandle} (decimal 15),
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   127
 * it must contain exactly three more bytes.  The first byte after the tag is a subtag
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   128
 * value which must be in the range 1 through 9, and the last two must be an index to a
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   129
 * {@code CONSTANT_Fieldref}, {@code CONSTANT_Methodref}, or
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   130
 * {@code CONSTANT_InterfaceMethodref} entry which represents a field or method
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   131
 * for which a method handle is to be created.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   132
 * Furthermore, the subtag value and the type of the constant index value
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   133
 * must agree according to the table below.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   134
 * <p>
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   135
 * The JVM will ensure that on first execution of an {@code ldc} instruction
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   136
 * for this entry, a {@link java.dyn.MethodHandle MethodHandle} will be created which represents
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   137
 * the field or method reference, according to the specific mode implied by the subtag.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   138
 * <p>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   139
 * As with {@code CONSTANT_Class} and {@code CONSTANT_MethodType} constants,
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   140
 * the {@code Class} or {@code MethodType} object which reifies the field or method's
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   141
 * type is created.  Any classes mentioned in this reification will be loaded if necessary,
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   142
 * but not initialized, and access checking and error reporting performed as usual.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   143
 * <p>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   144
 * The method handle itself will have a type and behavior determined by the subtag as follows:
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   145
 * <code>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   146
 * <table border=1 cellpadding=5 summary="CONSTANT_MethodHandle subtypes">
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   147
 * <tr><th>N</th><th>subtag name</th><th>member</th><th>MH type</th><th>MH behavior</th></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   148
 * <tr><td>1</td><td>REF_getField</td><td>C.f:T</td><td>(C)T</td><td>getfield C.f:T</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   149
 * <tr><td>2</td><td>REF_getStatic</td><td>C.f:T</td><td>(&nbsp;)T</td><td>getstatic C.f:T</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   150
 * <tr><td>3</td><td>REF_putField</td><td>C.f:T</td><td>(C,T)void</td><td>putfield C.f:T</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   151
 * <tr><td>4</td><td>REF_putStatic</td><td>C.f:T</td><td>(T)void</td><td>putstatic C.f:T</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   152
 * <tr><td>5</td><td>REF_invokeVirtual</td><td>C.m(A*)T</td><td>(C,A*)T</td><td>invokevirtual C.m(A*)T</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   153
 * <tr><td>6</td><td>REF_invokeStatic</td><td>C.m(A*)T</td><td>(C,A*)T</td><td>invokestatic C.m(A*)T</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   154
 * <tr><td>7</td><td>REF_invokeSpecial</td><td>C.m(A*)T</td><td>(C,A*)T</td><td>invokespecial C.m(A*)T</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   155
 * <tr><td>8</td><td>REF_newInvokeSpecial</td><td>C.&lt;init&gt;(A*)void</td><td>(A*)C</td><td>new C; dup; invokespecial C.&lt;init&gt;(A*)void</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   156
 * <tr><td>9</td><td>REF_invokeInterface</td><td>C.m(A*)T</td><td>(C,A*)T</td><td>invokeinterface C.m(A*)T</td></tr>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   157
 * </table>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   158
 * </code>
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   159
 * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   160
 * The special name {@code <clinit>} is not allowed.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   161
 * The special name {@code <init>} is not allowed except for subtag 8 as shown.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   162
 * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   163
 * The JVM verifier and linker apply the same access checks and restrictions for these references as for the hypothetical
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   164
 * bytecode instructions specified in the last column of the table.  In particular, method handles to
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   165
 * private and protected members can be created in exactly those classes for which the corresponding
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   166
 * normal accesses are legal.
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   167
 * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   168
 * A constant may refer to a method or constructor with the {@code varargs}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   169
 * bit (hexadecimal {@code 80}) set in its modifier bitmask.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   170
 * The method handle constant produced for such a method behaves the same
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   171
 * as if the {@code varargs} bit were not set.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   172
 * The argument-collecting behavior of {@code varargs} can be emulated by
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   173
 * adapting the method handle constant with
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   174
 * {@link java.dyn.MethodHandle#asCollector asCollector}.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   175
 * There is no provision for doing this automatically.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   176
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   177
 * Although the {@code CONSTANT_MethodHandle} and {@code CONSTANT_MethodType} constant types
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   178
 * resolve class names, they do not force class initialization.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   179
 * Method handle constants for subtags {@code REF_getStatic}, {@code REF_putStatic}, and {@code REF_invokeStatic}
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   180
 * may force class initialization on their first invocation, just like the corresponding bytecodes.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   181
 * <p>
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   182
 * The rules of section 5.4.3 of the
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   183
 * <a href="http://java.sun.com/docs/books/jvms/second_edition/html/ConstantPool.doc.html#73492">JVM Specification</a>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   184
 * apply to the resolution of {@code CONSTANT_MethodType}, {@code CONSTANT_MethodHandle},
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   185
 * and {@code CONSTANT_InvokeDynamic} constants,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   186
 * by the execution of {@code invokedynamic} and {@code ldc} instructions.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   187
 * (Roughly speaking, this means that every use of a constant pool entry
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   188
 * must lead to the same outcome.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   189
 * If the resoultion succeeds, the same object reference is produced
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   190
 * by every subsequent execution of the same instruction.
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   191
 * If the resolution of the constant causes an error to occur,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   192
 * the same error will be re-thrown on every subsequent attempt
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   193
 * to use this particular constant.)
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   194
 * <p>
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   195
 * Constants created by the resolution of these constant pool types are not necessarily
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   196
 * interned.  Except for {@link CONSTANT_Class} and {@link CONSTANT_String} entries,
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   197
 * two distinct constant pool entries might not resolve to the same reference
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   198
 * even if they contain the same symbolic reference.
5722
4ada807383c8 6939134: JSR 292 adjustments to method handle invocation
jrose
parents: 4537
diff changeset
   199
 *
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   200
 * <h2><a name="bsm"></a>Bootstrap Methods</h2>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   201
 * Before the JVM can execute a dynamic call site (an {@code invokedynamic} instruction),
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   202
 * the call site must first be <em>linked</em>.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   203
 * Linking is accomplished by calling a <em>bootstrap method</em>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   204
 * which is given the static information content of the call site,
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   205
 * and which must produce a {@link java.dyn.MethodHandle method handle}
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   206
 * that gives the behavior of the call site.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   207
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   208
 * Each {@code invokedynamic} instruction statically specifies its own
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   209
 * bootstrap method as a constant pool reference.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   210
 * The constant pool reference also specifies the call site's name and type signature,
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   211
 * just like {@code invokevirtual} and the other invoke instructions.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   212
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   213
 * Linking starts with resolving the constant pool entry for the
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   214
 * bootstrap method, and resolving a {@link java.dyn.MethodType MethodType} object for
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   215
 * the type signature of the dynamic call site.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   216
 * This resolution process may trigger class loading.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   217
 * It may therefore throw an error if a class fails to load.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   218
 * This error becomes the abnormal termination of the dynamic
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   219
 * call site execution.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   220
 * Linkage does not trigger class initialization.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   221
 * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   222
 * Next, the bootstrap method call is started, with four or five values being stacked:
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   223
 * <ul>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   224
 * <li>a {@code MethodHandle}, the resolved bootstrap method itself </li>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   225
 * <li>a {@code MethodHandles.Lookup}, a lookup object on the <em>caller class</em> in which dynamic call site occurs </li>
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   226
 * <li>a {@code String}, the method name mentioned in the call site </li>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   227
 * <li>a {@code MethodType}, the resolved type signature of the call </li>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   228
 * <li>optionally, a single object representing one or more <a href="#args">additional static arguments</a> </li>
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   229
 * </ul>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   230
 * The method handle is then applied to the other values as if by
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   231
 * {@link java.dyn.MethodHandle#invokeGeneric invokeGeneric}.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   232
 * The returned result must be a {@link java.dyn.CallSite CallSite} (or a subclass).
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   233
 * The type of the call site's target must be exactly equal to the type
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   234
 * derived from the dynamic call site signature and passed to
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   235
 * the bootstrap method.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   236
 * The call site then becomes permanently linked to the dynamic call site.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   237
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   238
 * As long as each bootstrap method can be correctly invoked
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   239
 * by <code>invokeGeneric</code>, its detailed type is arbitrary.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   240
 * For example, the first argument could be {@code Object}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   241
 * instead of {@code MethodHandles.Lookup}, and the return type
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   242
 * could also be {@code Object} instead of {@code CallSite}.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   243
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   244
 * As with any method handle constant, a {@code varargs} modifier bit
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   245
 * on the bootstrap method is ignored.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   246
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   247
 * Note that the first argument of the bootstrap method cannot be
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   248
 * a simple {@code Class} reference.  (This is a change from earlier
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   249
 * versions of this specification.  If the caller class is needed,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   250
 * it is easy to {@linkplain java.dyn.MethodHandles.Lookup#lookupClass() extract it}
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   251
 * from the {@code Lookup} object.)
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   252
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   253
 * After resolution, the linkage process may fail in a variety of ways.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   254
 * All failures are reported by an {@link java.dyn.InvokeDynamicBootstrapError InvokeDynamicBootstrapError},
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   255
 * which is thrown as the abnormal termination of the dynamic call
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   256
 * site execution.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   257
 * The following circumstances will cause this:
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   258
 * <ul>
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   259
 * <li>the index to the bootstrap method specifier is out of range </li>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   260
 * <li>the bootstrap method cannot be resolved </li>
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   261
 * <li>the {@code MethodType} to pass to the bootstrap method cannot be resolved </li>
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   262
 * <li>a static argument to the bootstrap method cannot be resolved
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   263
 *     (i.e., a {@code CONSTANT_Class}, {@code CONSTANT_MethodType},
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   264
 *     or {@code CONSTANT_MethodHandle} argument cannot be linked) </li>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   265
 * <li>the bootstrap method has the wrong arity,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   266
 *     causing {@code invokeGeneric} to throw {@code WrongMethodTypeException} </li>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   267
 * <li>the bootstrap method has a wrong argument or return type </li>
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   268
 * <li>the bootstrap method invocation completes abnormally </li>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   269
 * <li>the result from the bootstrap invocation is not a reference to
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   270
 *     an object of type {@link java.dyn.CallSite CallSite} </li>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   271
 * <li>the target of the {@code CallSite} does not have a target of
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   272
 *     the expected {@code MethodType} </li>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   273
 * </ul>
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   274
 *
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   275
 * <h3><a name="linktime"></a>timing of linkage</h3>
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   276
 * A dynamic call site is linked just before its first execution.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   277
 * The bootstrap method call implementing the linkage occurs within
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   278
 * a thread that is attempting a first execution.
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   279
 * <p>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   280
 * If there are several such threads, the bootstrap method may be
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   281
 * invoked in several threads concurrently.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   282
 * Therefore, bootstrap methods which access global application
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   283
 * data must take the usual precautions against race conditions.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   284
 * In any case, every {@code invokedynamic} instruction is either
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   285
 * unlinked or linked to a unique {@code CallSite} object.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   286
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   287
 * In an application which requires dynamic call sites with individually
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   288
 * mutable behaviors, their bootstrap methods should produce distinct
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   289
 * {@link java.dyn.CallSite CallSite} objects, one for each linkage request.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   290
 * Alternatively, an application can link a single {@code CallSite} object
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   291
 * to several {@code invokedynamic} instructions, in which case
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   292
 * a change to the target method will become visible at each of
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   293
 * the instructions.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   294
 * <p>
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   295
 * If several threads simultaneously execute a bootstrap method for a single dynamic
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   296
 * call site, the JVM must choose one {@code CallSite} object and install it visibly to
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   297
 * all threads.  Any other bootstrap method calls are allowed to complete, but their
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   298
 * results are ignored, and their dynamic call site invocations proceed with the originally
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   299
 * chosen target object.
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   300
 *
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   301
 * <p style="font-size:smaller;">
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   302
 * (Historic Note: Unlike some previous versions of this specification,
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   303
 * these rules do not enable the JVM to duplicate dynamic call sites,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   304
 * or to issue &ldquo;causeless&rdquo; bootstrap method calls.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   305
 * Every dynamic call site transitions at most once from unlinked to linked,
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   306
 * just before its first invocation.)
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   307
 *
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   308
 * <h3><a name="bsmattr">the {@code BootstrapMethods} attribute </h3>
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   309
 * Each {@code CONSTANT_InvokeDynamic} entry contains an index which references
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   310
 * a bootstrap method specifier; all such specifiers are contained in a separate array.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   311
 * This array is defined by a class attribute named {@code BootstrapMethods}.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   312
 * The body of this attribute consists of a sequence of byte pairs, all interpreted as
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   313
 * as 16-bit counts or constant pool indexes, in the {@code u2} format.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   314
 * The attribute body starts with a count of bootstrap method specifiers,
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   315
 * which is immediately followed by the sequence of specifiers.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   316
 * <p>
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   317
 * Each bootstrap method specifier contains an index to a
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   318
 * {@code CONSTANT_MethodHandle} constant, which is the bootstrap
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   319
 * method itself.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   320
 * This is followed by a count, and then a sequence (perhaps empty) of
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   321
 * indexes to <a href="#args">additional static arguments</a>
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   322
 * for the bootstrap method.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   323
 * <p>
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   324
 * During class loading, the verifier must check the structure of the
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   325
 * {@code BootstrapMethods} attribute.  In particular, each constant
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   326
 * pool index must be of the correct type.  A bootstrap method index
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   327
 * must refer to a {@code CONSTANT_MethodHandle} (tag 15).
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   328
 * Every other index must refer to a valid operand of an
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   329
 * {@code ldc_w} or {@code ldc2_w} instruction (tag 3..8 or 15..16).
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   330
 *
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   331
 * <h3><a name="args">static arguments to the bootstrap method</h3>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   332
 * An {@code invokedynamic} instruction specifies at least three arguments
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   333
 * to pass to its bootstrap method:
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   334
 * The caller class (expressed as a {@link java.dyn.MethodHandles.Lookup Lookup object},
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   335
 * the name (extracted from the {@code CONSTANT_NameAndType} entry),
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   336
 * and the type (also extracted from the {@code CONSTANT_NameAndType} entry).
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   337
 * The {@code invokedynamic} instruction may specify additional metadata values
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   338
 * to pass to its bootstrap method.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   339
 * Collectively, these values are called <em>static arguments</em> to the
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   340
 * {@code invokedynamic} instruction, because they are used once at link
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   341
 * time to determine the instruction's behavior on subsequent sets of
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   342
 * <em>dynamic arguments</em>.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   343
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   344
 * Static arguments are used to communicate application-specific meta-data
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   345
 * to the bootstrap method.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   346
 * Drawn from the constant pool, they may include references to classes, method handles,
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   347
 * strings, or numeric data that may be relevant to the task of linking that particular call site.
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   348
 * <p>
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   349
 * Static arguments are specified constant pool indexes stored in the {@code BootstrapMethods} attribute.
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   350
 * Before the bootstrap method is invoked, each index is used to compute an {@code Object}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   351
 * reference to the indexed value in the constant pool.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   352
 * If the value is a primitive type, it is converted to a reference by boxing conversion.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   353
 * The valid constant pool entries are listed in this table:
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   354
 * <code>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   355
 * <table border=1 cellpadding=5 summary="Static argument types">
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   356
 * <tr><th>entry type</th><th>argument type</th><th>argument value</th></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   357
 * <tr><td>CONSTANT_String</td><td><code>java.lang.String</code></td><td>the indexed string literal</td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   358
 * <tr><td>CONSTANT_Class</td><td><code>java.lang.Class</code></td><td>the indexed class, resolved</td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   359
 * <tr><td>CONSTANT_Integer</td><td><code>java.lang.Integer</code></td><td>the indexed int value</td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   360
 * <tr><td>CONSTANT_Long</td><td><code>java.lang.Long</code></td><td>the indexed long value</td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   361
 * <tr><td>CONSTANT_Float</td><td><code>java.lang.Float</code></td><td>the indexed float value</td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   362
 * <tr><td>CONSTANT_Double</td><td><code>java.lang.Double</code></td><td>the indexed double value</td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   363
 * <tr><td>CONSTANT_MethodHandle</td><td><code>java.dyn.MethodHandle</code></td><td>the indexed method handle constant</td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   364
 * <tr><td>CONSTANT_MethodType</td><td><code>java.dyn.MethodType</code></td><td>the indexed method type constant</td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   365
 * </table>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   366
 * </code>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   367
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   368
 * If a given {@code invokedynamic} instruction specifies no static arguments,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   369
 * the instruction's bootstrap method will be invoked on three arguments,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   370
 * conveying the instruction's caller class, name, and method type.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   371
 * If the {@code invokedynamic} instruction specifies one or more static arguments,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   372
 * a fourth argument will be passed to the bootstrap argument,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   373
 * either an {@code Object} reference to the sole extra argument (if there is one)
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   374
 * or an {@code Object} array of references to all the arguments (if there are two or more),
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   375
 * as if the bootstrap method is a variable-arity method.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   376
 * <code>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   377
 * <table border=1 cellpadding=5 summary="Static argument types">
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   378
 * <tr><th>N</th><th>sample bootstrap method</th></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   379
 * <tr><td>0</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type)</code></td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   380
 * <tr><td>1</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object arg)</code></td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   381
 * <tr><td>2</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code></td></tr>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   382
 * </table>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   383
 * </code>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   384
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   385
 * The argument and return types listed here are used by the {@code invokeGeneric}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   386
 * call to the bootstrap method.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   387
 * As noted above, the actual method type of the bootstrap method can vary.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   388
 * For example, the fourth argument could be {@code MethodHandle},
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   389
 * if that is the type of the corresponding constant in
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   390
 * the {@code CONSTANT_InvokeDynamic} entry.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   391
 * In that case, the {@code invokeGeneric} call will pass the extra method handle
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   392
 * constant as an {@code Object}, but the type matching machinery of {@code invokeGeneric}
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   393
 * will cast the reference back to {@code MethodHandle} before invoking the bootstrap method.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   394
 * (If a string constant were passed instead, by badly generated code, that cast would then fail.)
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   395
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   396
 * If the fourth argument is an array, the array element type must be {@code Object},
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   397
 * since object arrays (as produced by the JVM at this point) cannot be converted
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   398
 * to other array types.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   399
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   400
 * If an array is provided, it will appear to be freshly allocated.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   401
 * That is, the same array will not appear to two bootstrap method calls.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   402
 * <p>
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   403
 * Extra bootstrap method arguments are intended to allow language implementors
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   404
 * to safely and compactly encode metadata.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   405
 * In principle, the name and extra arguments are redundant,
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   406
 * since each call site could be given its own unique bootstrap method.
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   407
 * Such a practice is likely to produce large class files and constant pools.
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   408
 *
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   409
 * <p style="font-size:smaller;">
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   410
 * <em>PROVISIONAL API, WORK IN PROGRESS:</em>
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   411
 * (Usage Note: There is no mechanism for specifying five or more positional arguments to the bootstrap method.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   412
 * If there are two or more arguments, the Java code of the bootstrap method is required to extract them from
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   413
 * a varargs-style object array.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   414
 * This design uses varargs because it anticipates some use cases where bootstrap arguments
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   415
 * contribute components of variable-length structures, such as virtual function tables
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   416
 * or interpreter token streams.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   417
 * Such parameters would be awkward or impossible to manage if represented
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   418
 * as normal positional method arguments,
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   419
 * since there would need to be one Java method per length.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   420
 * On balance, leaving out the varargs feature would cause more trouble to users than keeping it.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   421
 * Also, this design allows bootstrap methods to be called in a limited JVM stack depth.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   422
 * At both the user and JVM level, the difference between varargs and non-varargs
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   423
 * calling sequences can easily be bridged via the
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   424
 * {@link java.dyn.MethodHandle#asSpreader asSpreader}
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   425
 * and {@link java.dyn.MethodHandle#asSpreader asCollector} methods.)
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   426
 *
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7557
diff changeset
   427
 * <h2><a name="structs"></a>Structure Summary</h2>
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   428
 * <blockquote><pre>// summary of constant and attribute structures
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   429
struct CONSTANT_MethodHandle_info {
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   430
  u1 tag = 15;
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   431
  u1 reference_kind;       // 1..8 (one of REF_invokeVirtual, etc.)
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   432
  u2 reference_index;      // index to CONSTANT_Fieldref or *Methodref
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   433
}
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   434
struct CONSTANT_MethodType_info {
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   435
  u1 tag = 16;
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   436
  u2 descriptor_index;    // index to CONSTANT_Utf8, as in NameAndType
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   437
}
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   438
struct CONSTANT_InvokeDynamic_17_info {
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   439
  u1 tag = 17;
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   440
  u2 bootstrap_method_index;   // index to CONSTANT_MethodHandle
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   441
  u2 name_and_type_index;      // same as for CONSTANT_Methodref, etc.
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   442
}
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   443
struct CONSTANT_InvokeDynamic_info {
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   444
  u1 tag = 18;
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   445
  u2 bootstrap_method_attr_index;  // index into BootstrapMethods_attr
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   446
  u2 name_and_type_index;          // index to CONSTANT_NameAndType, as in Methodref
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   447
}
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   448
struct BootstrapMethods_attr {
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   449
 u2 name;  // CONSTANT_Utf8 = "BootstrapMethods"
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   450
 u4 size;
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   451
 u2 bootstrap_method_count;
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   452
 struct bootstrap_method_specifier {
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   453
   u2 bootstrap_method_ref;  // index to CONSTANT_MethodHandle
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   454
   u2 bootstrap_argument_count;
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   455
   u2 bootstrap_arguments[bootstrap_argument_count];  // constant pool indexes
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   456
 } bootstrap_methods[bootstrap_method_count];
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   457
}
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   458
 * </pre></blockquote>
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   459
 * <p>
7557
06bbd3ae0835 7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents: 7555
diff changeset
   460
 * <em>Note: The Proposed Final Draft of JSR 292 may remove the constant tag 17,
7555
a279ebc3b25c 6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents: 7051
diff changeset
   461
 * for the sake of simplicity.</em>
7051
1c545d70a157 6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents: 5725
diff changeset
   462
 *
2707
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   463
 * @author John Rose, JSR 292 EG
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   464
 */
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   465
5a17df307cbc 6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff changeset
   466
package java.dyn;