author | jjg |
Tue, 30 May 2017 15:48:54 -0700 | |
changeset 45433 | 01ecdcf197df |
parent 42103 | 8e1fe4345fd7 |
child 45437 | c48b8edad8dd |
permissions | -rw-r--r-- |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1 |
/* |
45433 | 2 |
* Copyright (c) 2008, 2017, 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> |
36934 | 35 |
* <li>The classes {@link java.lang.invoke.MethodHandle MethodHandle} |
36 |
* {@link java.lang.invoke.VarHandle VarHandle} contain |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
37 |
* <a href="MethodHandle.html#sigpoly">signature polymorphic methods</a> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
38 |
* which can be linked regardless of their type descriptor. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
39 |
* 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
|
40 |
* </li> |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
41 |
* |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
42 |
* <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
|
43 |
* 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
|
44 |
* </li> |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
45 |
* </ul> |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
46 |
* |
45433 | 47 |
* <h1><a id="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
|
48 |
* 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
|
49 |
* 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
|
50 |
* current version of that specification. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
51 |
* |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
52 |
* Each occurrence of an {@code invokedynamic} instruction is called a <em>dynamic call site</em>. |
45433 | 53 |
* <h2><a id="indyinsn"></a>{@code invokedynamic} instructions</h2> |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
54 |
* 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
|
55 |
* 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
|
56 |
* <p> |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
57 |
* 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
|
58 |
* the call site must first be <em>linked</em>. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
59 |
* 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
|
60 |
* 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
|
61 |
* 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
|
62 |
* that gives the behavior of the call site. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
63 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
64 |
* Each {@code invokedynamic} instruction statically specifies its own |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
65 |
* bootstrap method as a constant pool reference. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
66 |
* 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
|
67 |
* just like {@code invokevirtual} and the other invoke instructions. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
68 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
69 |
* 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
|
70 |
* 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
|
71 |
* the type descriptor of the dynamic call site. |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
72 |
* This resolution process may trigger class loading. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
73 |
* 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
|
74 |
* This error becomes the abnormal termination of the dynamic |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
75 |
* call site execution. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
76 |
* Linkage does not trigger class initialization. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
77 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
78 |
* 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
|
79 |
* <ul> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
80 |
* <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
|
81 |
* <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
|
82 |
* <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
|
83 |
* <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
|
84 |
* </ul> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
85 |
* Invocation is as if by |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9266
diff
changeset
|
86 |
* {@link java.lang.invoke.MethodHandle#invoke MethodHandle.invoke}. |
42103
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
87 |
* The returned result must be a {@link java.lang.invoke.CallSite CallSite} |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
88 |
* (or a subclass), otherwise a |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
89 |
* {@link java.lang.BootstrapMethodError BootstrapMethodError} is thrown. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
90 |
* 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
|
91 |
* derived from the dynamic call site's type descriptor and passed to |
42103
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
92 |
* the bootstrap method, otherwise a {@code BootstrapMethodError} is thrown. |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
93 |
* On success the call site then becomes permanently linked to the dynamic call |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
94 |
* site. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
95 |
* <p> |
42103
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
96 |
* If an exception, {@code E} say, occurs when linking the call site then the |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
97 |
* linkage fails and terminates abnormally. {@code E} is rethrown if the type of |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
98 |
* {@code E} is {@code Error} or a subclass, otherwise a |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
99 |
* {@code BootstrapMethodError} that wraps {@code E} is thrown. |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
100 |
* If this happens, the same {@code Error} or subclass will the thrown for all |
8e1fe4345fd7
8166974: invokedynamic implementation should not wrap Errors
psandoz
parents:
36934
diff
changeset
|
101 |
* subsequent attempts to execute the dynamic call site. |
18569 | 102 |
* <h2>timing of linkage</h2> |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
103 |
* 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
|
104 |
* The bootstrap method call implementing the linkage occurs within |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
105 |
* a thread that is attempting a first execution. |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
106 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
107 |
* 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
|
108 |
* invoked in several threads concurrently. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
109 |
* Therefore, bootstrap methods which access global application |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
110 |
* data must take the usual precautions against race conditions. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
111 |
* In any case, every {@code invokedynamic} instruction is either |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
112 |
* 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
|
113 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
114 |
* 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
|
115 |
* 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
|
116 |
* {@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
|
117 |
* 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
|
118 |
* to several {@code invokedynamic} instructions, in which case |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
119 |
* 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
|
120 |
* the instructions. |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
121 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
122 |
* 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
|
123 |
* 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
|
124 |
* 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
|
125 |
* 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
|
126 |
* chosen target object. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9645
diff
changeset
|
127 |
|
7557
06bbd3ae0835
7001379: bootstrap method data needs to be moved from constant pool to a classfile attribute
jrose
parents:
7555
diff
changeset
|
128 |
* <p style="font-size:smaller;"> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
129 |
* <em>Discussion:</em> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
130 |
* 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
|
131 |
* or to issue “causeless” bootstrap method calls. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
132 |
* 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
|
133 |
* just before its first invocation. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
134 |
* 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
|
135 |
* |
18569 | 136 |
* <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
|
137 |
* 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
|
138 |
* 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
|
139 |
* 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
|
140 |
* 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
|
141 |
* 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
|
142 |
* (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
|
143 |
* 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
|
144 |
* static methods and constructors of {@code CallSite} subclasses.) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
145 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
146 |
* 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
|
147 |
* 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
|
148 |
* 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
|
149 |
* 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
|
150 |
* 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
|
151 |
* (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
|
152 |
* 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
|
153 |
* 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
|
154 |
* 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
|
155 |
* 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
|
156 |
* <p> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9266
diff
changeset
|
157 |
* 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
|
158 |
* 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
|
159 |
* 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
|
160 |
* 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
|
161 |
* (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
|
162 |
* 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
|
163 |
* 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
|
164 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
165 |
* 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
|
166 |
* given various numbers {@code N} of extra arguments. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
167 |
* 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
|
168 |
* <table border=1 cellpadding=5 summary="Static argument types"> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
169 |
* <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
|
170 |
* <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
|
171 |
* <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
|
172 |
* <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
|
173 |
* <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
|
174 |
* <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
|
175 |
* <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
|
176 |
* <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
|
177 |
* <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
|
178 |
* <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
|
179 |
* </table> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
180 |
* 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
|
181 |
* {@code CONSTANT_String} and {@code CONSTANT_Integer}, respectively. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
182 |
* 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
|
183 |
* {@code CONSTANT_String}. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7668
diff
changeset
|
184 |
* 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
|
185 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
186 |
* 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
|
187 |
* For example, the fourth argument could be {@code MethodHandle}, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
188 |
* 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
|
189 |
* the {@code CONSTANT_InvokeDynamic} entry. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
9266
diff
changeset
|
190 |
* 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
|
191 |
* 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
|
192 |
* 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
|
193 |
* (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
|
194 |
* resulting in a {@code BootstrapMethodError}.) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
195 |
* <p> |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
9752
diff
changeset
|
196 |
* 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
|
197 |
* 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
|
198 |
* 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
|
199 |
* 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
|
200 |
* 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
|
201 |
* 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
|
202 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
203 |
* 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
|
204 |
* to safely and compactly encode metadata. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
205 |
* In principle, the name and extra arguments are redundant, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7051
diff
changeset
|
206 |
* 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
|
207 |
* 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
|
208 |
* |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
209 |
* @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
|
210 |
* @since 1.7 |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
211 |
*/ |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
212 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8349
diff
changeset
|
213 |
package java.lang.invoke; |