author | chegar |
Wed, 11 Nov 2015 09:19:12 +0000 | |
changeset 33674 | 566777f73c32 |
parent 25859 | 3317bb8137f4 |
child 36934 | 590fc47a0aeb |
permissions | -rw-r--r-- |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1 |
/* |
23010
6dadb192ad81
8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents:
18569
diff
changeset
|
2 |
* Copyright (c) 2008, 2013, 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 | 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 | 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 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
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 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
27 |
* The {@code java.lang.invoke} package contains dynamic language support provided directly by |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
28 |
* the Java core class libraries and virtual machine. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
29 |
* |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
30 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
31 |
* As described in the Java Virtual Machine Specification, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
32 |
* certain types in this package have special relations to dynamic |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
33 |
* language support in the virtual machine: |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
34 |
* <ul> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
35 |
* <li>The class {@link java.lang.invoke.MethodHandle MethodHandle} contains |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
36 |
* <a href="MethodHandle.html#sigpoly">signature polymorphic methods</a> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
37 |
* which can be linked regardless of their type descriptor. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
38 |
* Normally, method linkage requires exact matching of type descriptors. |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
39 |
* </li> |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
40 |
* |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
41 |
* <li>The JVM bytecode format supports immediate constants of |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
42 |
* the classes {@link java.lang.invoke.MethodHandle MethodHandle} and {@link java.lang.invoke.MethodType MethodType}. |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
43 |
* </li> |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
44 |
* </ul> |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
45 |
* |
18569 | 46 |
* <h1><a name="jvm_mods"></a>Summary of relevant Java Virtual Machine changes</h1> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
47 |
* The following low-level information summarizes relevant parts of the |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
48 |
* Java Virtual Machine specification. For full details, please see the |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
49 |
* current version of that specification. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
50 |
* |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
51 |
* Each occurrence of an {@code invokedynamic} instruction is called a <em>dynamic call site</em>. |
18569 | 52 |
* <h2><a name="indyinsn"></a>{@code invokedynamic} instructions</h2> |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
53 |
* 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
|
54 |
* no target method for the call site to invoke. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
55 |
* <p> |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
56 |
* 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
|
57 |
* the call site must first be <em>linked</em>. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
58 |
* 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
|
59 |
* which is given the static information content of the call site, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
60 |
* and which must produce a {@link java.lang.invoke.MethodHandle method handle} |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
61 |
* that gives the behavior of the call site. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
62 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
63 |
* Each {@code invokedynamic} instruction statically specifies its own |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
64 |
* bootstrap method as a constant pool reference. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
65 |
* The constant pool reference also specifies the call site's name and type descriptor, |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
66 |
* just like {@code invokevirtual} and the other invoke instructions. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
67 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
68 |
* Linking starts with resolving the constant pool entry for the |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
69 |
* bootstrap method, and resolving a {@link java.lang.invoke.MethodType MethodType} object for |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
70 |
* the type descriptor of the dynamic call site. |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
71 |
* This resolution process may trigger class loading. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
72 |
* 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
|
73 |
* This error becomes the abnormal termination of the dynamic |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
74 |
* call site execution. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
75 |
* Linkage does not trigger class initialization. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
76 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
77 |
* The bootstrap method is invoked on at least three values: |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
78 |
* <ul> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
79 |
* <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
|
80 |
* <li>a {@code String}, the method name mentioned in the call site </li> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
81 |
* <li>a {@code MethodType}, the resolved type descriptor of the call </li> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
82 |
* <li>optionally, between 1 and 251 additional static arguments taken from the constant pool </li> |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
83 |
* </ul> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
84 |
* Invocation is as if by |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9266
diff
changeset
|
85 |
* {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke}. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
86 |
* The returned result must be a {@link java.lang.invoke.CallSite CallSite} (or a subclass). |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
87 |
* The type of the call site's target must be exactly equal to the type |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
88 |
* derived from the dynamic call site's type descriptor and passed to |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
89 |
* the bootstrap method. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
90 |
* 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
|
91 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
92 |
* As documented in the JVM specification, all failures arising from |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
93 |
* the linkage of a dynamic call site are reported |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
94 |
* by a {@link java.lang.BootstrapMethodError BootstrapMethodError}, |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
95 |
* 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
|
96 |
* site execution. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
97 |
* If this happens, the same error will the thrown for all subsequent |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
98 |
* attempts to execute the dynamic call site. |
7557
06bbd3ae0835
7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents:
7555
diff
changeset
|
99 |
* |
18569 | 100 |
* <h2>timing of linkage</h2> |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
101 |
* 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
|
102 |
* The bootstrap method call implementing the linkage occurs within |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
103 |
* a thread that is attempting a first execution. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
104 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
105 |
* 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
|
106 |
* invoked in several threads concurrently. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
107 |
* Therefore, bootstrap methods which access global application |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
108 |
* data must take the usual precautions against race conditions. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
109 |
* In any case, every {@code invokedynamic} instruction is either |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
110 |
* 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
|
111 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
112 |
* 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
|
113 |
* mutable behaviors, their bootstrap methods should produce distinct |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
114 |
* {@link java.lang.invoke.CallSite CallSite} objects, one for each linkage request. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
115 |
* 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
|
116 |
* to several {@code invokedynamic} instructions, in which case |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
117 |
* 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
|
118 |
* the instructions. |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
119 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
120 |
* 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
|
121 |
* 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
|
122 |
* 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
|
123 |
* 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
|
124 |
* chosen target object. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
125 |
|
7557
06bbd3ae0835
7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents:
7555
diff
changeset
|
126 |
* <p style="font-size:smaller;"> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
127 |
* <em>Discussion:</em> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
128 |
* These rules do not enable the JVM to duplicate dynamic call sites, |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
129 |
* or to issue “causeless” bootstrap method calls. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
130 |
* Every dynamic call site transitions at most once from unlinked to linked, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
131 |
* just before its first invocation. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
132 |
* There is no way to undo the effect of a completed bootstrap method call. |
7557
06bbd3ae0835
7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents:
7555
diff
changeset
|
133 |
* |
18569 | 134 |
* <h2>types of bootstrap methods</h2> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
135 |
* As long as each bootstrap method can be correctly invoked |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
136 |
* by {@code MethodHandle.invoke}, its detailed type is arbitrary. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
137 |
* For example, the first argument could be {@code Object} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
138 |
* instead of {@code MethodHandles.Lookup}, and the return type |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
139 |
* could also be {@code Object} instead of {@code CallSite}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
140 |
* (Note that the types and number of the stacked arguments limit |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
141 |
* the legal kinds of bootstrap methods to appropriately typed |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
142 |
* static methods and constructors of {@code CallSite} subclasses.) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
143 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
144 |
* 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
|
145 |
* 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
|
146 |
* 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
|
147 |
* If the {@code invokedynamic} instruction specifies one or more static arguments, |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
148 |
* those values will be passed as additional arguments to the method handle. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
149 |
* (Note that because there is a limit of 255 arguments to any method, |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
150 |
* at most 251 extra arguments can be supplied, since the bootstrap method |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
151 |
* handle itself and its first three arguments must also be stacked.) |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9266
diff
changeset
|
152 |
* The bootstrap method will be invoked as if by either {@code MethodHandle.invoke} |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
153 |
* or {@code invokeWithArguments}. (There is no way to tell the difference.) |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
154 |
* <p> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9266
diff
changeset
|
155 |
* The normal argument conversion rules for {@code MethodHandle.invoke} apply to all stacked arguments. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
156 |
* For example, if a pushed value is a primitive type, it may be converted to a reference by boxing conversion. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
157 |
* If the bootstrap method is a variable arity method (its modifier bit {@code 0x0080} is set), |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
158 |
* then some or all of the arguments specified here may be collected into a trailing array parameter. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
159 |
* (This is not a special rule, but rather a useful consequence of the interaction |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
160 |
* between {@code CONSTANT_MethodHandle} constants, the modifier bit for variable arity methods, |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
161 |
* and the {@link java.lang.invoke.MethodHandle#asVarargsCollector asVarargsCollector} transformation.) |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
162 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
163 |
* Given these rules, here are examples of legal bootstrap method declarations, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
164 |
* given various numbers {@code N} of extra arguments. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
165 |
* The first rows (marked {@code *}) will work for any number of extra arguments. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
166 |
* <table border=1 cellpadding=5 summary="Static argument types"> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
167 |
* <tr><th>N</th><th>sample bootstrap method</th></tr> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
168 |
* <tr><td>*</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code></td></tr> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
169 |
* <tr><td>*</td><td><code>CallSite bootstrap(Object... args)</code></td></tr> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
170 |
* <tr><td>*</td><td><code>CallSite bootstrap(Object caller, Object... nameAndTypeWithArgs)</code></td></tr> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
171 |
* <tr><td>0</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type)</code></td></tr> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
172 |
* <tr><td>0</td><td><code>CallSite bootstrap(Lookup caller, Object... nameAndType)</code></td></tr> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
173 |
* <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
|
174 |
* <tr><td>2</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, Object... args)</code></td></tr> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
175 |
* <tr><td>2</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String... args)</code></td></tr> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
176 |
* <tr><td>2</td><td><code>CallSite bootstrap(Lookup caller, String name, MethodType type, String x, int y)</code></td></tr> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
177 |
* </table> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
178 |
* The last example assumes that the extra arguments are of type |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
179 |
* {@code CONSTANT_String} and {@code CONSTANT_Integer}, respectively. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
180 |
* The second-to-last example assumes that all extra arguments are of type |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
181 |
* {@code CONSTANT_String}. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
182 |
* The other examples work with all types of extra arguments. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
183 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
184 |
* 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
|
185 |
* For example, the fourth argument could be {@code MethodHandle}, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
186 |
* 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
|
187 |
* the {@code CONSTANT_InvokeDynamic} entry. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9266
diff
changeset
|
188 |
* In that case, the {@code MethodHandle.invoke} call will pass the extra method handle |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9266
diff
changeset
|
189 |
* constant as an {@code Object}, but the type matching machinery of {@code MethodHandle.invoke} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
190 |
* will cast the reference back to {@code MethodHandle} before invoking the bootstrap method. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
191 |
* (If a string constant were passed instead, by badly generated code, that cast would then fail, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
192 |
* resulting in a {@code BootstrapMethodError}.) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
193 |
* <p> |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
9752
diff
changeset
|
194 |
* Note that, as a consequence of the above rules, the bootstrap method may accept a primitive |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
9752
diff
changeset
|
195 |
* argument, if it can be represented by a constant pool entry. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
9752
diff
changeset
|
196 |
* However, arguments of type {@code boolean}, {@code byte}, {@code short}, or {@code char} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
9752
diff
changeset
|
197 |
* cannot be created for bootstrap methods, since such constants cannot be directly |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
9752
diff
changeset
|
198 |
* represented in the constant pool, and the invocation of the bootstrap method will |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
9752
diff
changeset
|
199 |
* not perform the necessary narrowing primitive conversions. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
9752
diff
changeset
|
200 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
201 |
* 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
|
202 |
* to safely and compactly encode metadata. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
203 |
* In principle, the name and extra arguments are redundant, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
204 |
* 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
|
205 |
* 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
|
206 |
* |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
207 |
* @author John Rose, JSR 292 EG |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
208 |
* @since 1.7 |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
209 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
210 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
211 |
package java.lang.invoke; |