author | mhaupt |
Tue, 23 Feb 2016 07:17:54 +0100 | |
changeset 36112 | e71e97060bf5 |
parent 34720 | c2192aa0ab88 |
child 36934 | 590fc47a0aeb |
permissions | -rw-r--r-- |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1 |
/* |
36112
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
2 |
* Copyright (c) 2008, 2016, 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 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
26 |
package java.lang.invoke; |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
27 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
28 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
29 |
import java.util.*; |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29094
diff
changeset
|
30 |
import jdk.internal.HotSpotIntrinsicCandidate; |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
31 |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
32 |
import static java.lang.invoke.MethodHandleStatics.*; |
4537 | 33 |
|
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
34 |
/** |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
35 |
* A method handle is a typed, directly executable reference to an underlying method, |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
36 |
* constructor, field, or similar low-level operation, with optional |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
37 |
* transformations of arguments or return values. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
38 |
* These transformations are quite general, and include such patterns as |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
39 |
* {@linkplain #asType conversion}, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
40 |
* {@linkplain #bindTo insertion}, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
41 |
* {@linkplain java.lang.invoke.MethodHandles#dropArguments deletion}, |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
42 |
* and {@linkplain java.lang.invoke.MethodHandles#filterArguments substitution}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
43 |
* |
18569 | 44 |
* <h1>Method handle contents</h1> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
45 |
* Method handles are dynamically and strongly typed according to their parameter and return types. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
46 |
* They are not distinguished by the name or the defining class of their underlying methods. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
47 |
* A method handle must be invoked using a symbolic type descriptor which matches |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
48 |
* the method handle's own {@linkplain #type type descriptor}. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
49 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
50 |
* Every method handle reports its type descriptor via the {@link #type type} accessor. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
51 |
* This type descriptor is a {@link java.lang.invoke.MethodType MethodType} object, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
52 |
* whose structure is a series of classes, one of which is |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
53 |
* the return type of the method (or {@code void.class} if none). |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
54 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
55 |
* A method handle's type controls the types of invocations it accepts, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
56 |
* and the kinds of transformations that apply to it. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
57 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
58 |
* A method handle contains a pair of special invoker methods |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
59 |
* called {@link #invokeExact invokeExact} and {@link #invoke invoke}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
60 |
* Both invoker methods provide direct access to the method handle's |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
61 |
* underlying method, constructor, field, or other operation, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
62 |
* as modified by transformations of arguments and return values. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
63 |
* Both invokers accept calls which exactly match the method handle's own type. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
64 |
* The plain, inexact invoker also accepts a range of other call types. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
65 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
66 |
* Method handles are immutable and have no visible state. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
67 |
* Of course, they can be bound to underlying methods or data which exhibit state. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
68 |
* With respect to the Java Memory Model, any method handle will behave |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
69 |
* as if all of its (internal) fields are final variables. This means that any method |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
70 |
* handle made visible to the application will always be fully formed. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
71 |
* This is true even if the method handle is published through a shared |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
72 |
* variable in a data race. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
73 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
74 |
* Method handles cannot be subclassed by the user. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
75 |
* Implementations may (or may not) create internal subclasses of {@code MethodHandle} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
76 |
* which may be visible via the {@link java.lang.Object#getClass Object.getClass} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
77 |
* operation. The programmer should not draw conclusions about a method handle |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
78 |
* from its specific class, as the method handle class hierarchy (if any) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
79 |
* may change from time to time or across implementations from different vendors. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
80 |
* |
18569 | 81 |
* <h1>Method handle compilation</h1> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
82 |
* A Java method call expression naming {@code invokeExact} or {@code invoke} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
83 |
* can invoke a method handle from Java source code. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
84 |
* From the viewpoint of source code, these methods can take any arguments |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
85 |
* and their result can be cast to any return type. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
86 |
* Formally this is accomplished by giving the invoker methods |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
87 |
* {@code Object} return types and variable arity {@code Object} arguments, |
8350
2479a9688a39
7016520: JSR 292 rules for polymorphic signature processing must be in package-info
jrose
parents:
8346
diff
changeset
|
88 |
* but they have an additional quality called <em>signature polymorphism</em> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
89 |
* which connects this freedom of invocation directly to the JVM execution stack. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
90 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
91 |
* As is usual with virtual methods, source-level calls to {@code invokeExact} |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
92 |
* and {@code invoke} compile to an {@code invokevirtual} instruction. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
93 |
* More unusually, the compiler must record the actual argument types, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
94 |
* and may not perform method invocation conversions on the arguments. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
95 |
* Instead, it must push them on the stack according to their own unconverted types. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
96 |
* The method handle object itself is pushed on the stack before the arguments. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
97 |
* The compiler then calls the method handle with a symbolic type descriptor which |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
98 |
* describes the argument and return types. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
99 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
100 |
* To issue a complete symbolic type descriptor, the compiler must also determine |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
101 |
* the return type. This is based on a cast on the method invocation expression, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
102 |
* if there is one, or else {@code Object} if the invocation is an expression |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
103 |
* or else {@code void} if the invocation is a statement. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
104 |
* The cast may be to a primitive type (but not {@code void}). |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
105 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
106 |
* As a corner case, an uncasted {@code null} argument is given |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
107 |
* a symbolic type descriptor of {@code java.lang.Void}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
108 |
* The ambiguity with the type {@code Void} is harmless, since there are no references of type |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
109 |
* {@code Void} except the null reference. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
110 |
* |
18569 | 111 |
* <h1>Method handle invocation</h1> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
112 |
* The first time a {@code invokevirtual} instruction is executed |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
113 |
* it is linked, by symbolically resolving the names in the instruction |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
114 |
* and verifying that the method call is statically legal. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
115 |
* This is true of calls to {@code invokeExact} and {@code invoke}. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
116 |
* In this case, the symbolic type descriptor emitted by the compiler is checked for |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
117 |
* correct syntax and names it contains are resolved. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
118 |
* Thus, an {@code invokevirtual} instruction which invokes |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
119 |
* a method handle will always link, as long |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
120 |
* as the symbolic type descriptor is syntactically well-formed |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
121 |
* and the types exist. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
122 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
123 |
* When the {@code invokevirtual} is executed after linking, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
124 |
* the receiving method handle's type is first checked by the JVM |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
125 |
* to ensure that it matches the symbolic type descriptor. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
126 |
* If the type match fails, it means that the method which the |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
127 |
* caller is invoking is not present on the individual |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
128 |
* method handle being invoked. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
129 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
130 |
* In the case of {@code invokeExact}, the type descriptor of the invocation |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
131 |
* (after resolving symbolic type names) must exactly match the method type |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
132 |
* of the receiving method handle. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
133 |
* In the case of plain, inexact {@code invoke}, the resolved type descriptor |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
134 |
* must be a valid argument to the receiver's {@link #asType asType} method. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
135 |
* Thus, plain {@code invoke} is more permissive than {@code invokeExact}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
136 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
137 |
* After type matching, a call to {@code invokeExact} directly |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
138 |
* and immediately invoke the method handle's underlying method |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
139 |
* (or other behavior, as the case may be). |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
140 |
* <p> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
141 |
* A call to plain {@code invoke} works the same as a call to |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
142 |
* {@code invokeExact}, if the symbolic type descriptor specified by the caller |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
143 |
* exactly matches the method handle's own type. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
144 |
* If there is a type mismatch, {@code invoke} attempts |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
145 |
* to adjust the type of the receiving method handle, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
146 |
* as if by a call to {@link #asType asType}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
147 |
* to obtain an exactly invokable method handle {@code M2}. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
148 |
* This allows a more powerful negotiation of method type |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
149 |
* between caller and callee. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
150 |
* <p> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
151 |
* (<em>Note:</em> The adjusted method handle {@code M2} is not directly observable, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
152 |
* and implementations are therefore not required to materialize it.) |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
153 |
* |
18569 | 154 |
* <h1>Invocation checking</h1> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
155 |
* In typical programs, method handle type matching will usually succeed. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
156 |
* But if a match fails, the JVM will throw a {@link WrongMethodTypeException}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
157 |
* either directly (in the case of {@code invokeExact}) or indirectly as if |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
158 |
* by a failed call to {@code asType} (in the case of {@code invoke}). |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
159 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
160 |
* Thus, a method type mismatch which might show up as a linkage error |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
161 |
* in a statically typed program can show up as |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
162 |
* a dynamic {@code WrongMethodTypeException} |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
163 |
* in a program which uses method handles. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
164 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
165 |
* Because method types contain "live" {@code Class} objects, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
166 |
* method type matching takes into account both types names and class loaders. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
167 |
* Thus, even if a method handle {@code M} is created in one |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
168 |
* class loader {@code L1} and used in another {@code L2}, |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
169 |
* method handle calls are type-safe, because the caller's symbolic type |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
170 |
* descriptor, as resolved in {@code L2}, |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
171 |
* is matched against the original callee method's symbolic type descriptor, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
172 |
* as resolved in {@code L1}. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
173 |
* The resolution in {@code L1} happens when {@code M} is created |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
174 |
* and its type is assigned, while the resolution in {@code L2} happens |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
175 |
* when the {@code invokevirtual} instruction is linked. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
176 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
177 |
* Apart from the checking of type descriptors, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
178 |
* a method handle's capability to call its underlying method is unrestricted. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
179 |
* If a method handle is formed on a non-public method by a class |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
180 |
* that has access to that method, the resulting handle can be used |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
181 |
* in any place by any caller who receives a reference to it. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
182 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
183 |
* Unlike with the Core Reflection API, where access is checked every time |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
184 |
* a reflective method is invoked, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
185 |
* method handle access checking is performed |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
186 |
* <a href="MethodHandles.Lookup.html#access">when the method handle is created</a>. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
187 |
* In the case of {@code ldc} (see below), access checking is performed as part of linking |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
188 |
* the constant pool entry underlying the constant method handle. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
189 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
190 |
* Thus, handles to non-public methods, or to methods in non-public classes, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
191 |
* should generally be kept secret. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
192 |
* They should not be passed to untrusted code unless their use from |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
193 |
* the untrusted code would be harmless. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
194 |
* |
18569 | 195 |
* <h1>Method handle creation</h1> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
196 |
* Java code can create a method handle that directly accesses |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
197 |
* any method, constructor, or field that is accessible to that code. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
198 |
* This is done via a reflective, capability-based API called |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
199 |
* {@link java.lang.invoke.MethodHandles.Lookup MethodHandles.Lookup} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
200 |
* For example, a static method handle can be obtained |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
201 |
* from {@link java.lang.invoke.MethodHandles.Lookup#findStatic Lookup.findStatic}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
202 |
* There are also conversion methods from Core Reflection API objects, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
203 |
* such as {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
204 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
205 |
* Like classes and strings, method handles that correspond to accessible |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
206 |
* fields, methods, and constructors can also be represented directly |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
207 |
* in a class file's constant pool as constants to be loaded by {@code ldc} bytecodes. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
208 |
* A new type of constant pool entry, {@code CONSTANT_MethodHandle}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
209 |
* refers directly to an associated {@code CONSTANT_Methodref}, |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
210 |
* {@code CONSTANT_InterfaceMethodref}, or {@code CONSTANT_Fieldref} |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
211 |
* constant pool entry. |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
212 |
* (For full details on method handle constants, |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
213 |
* see sections 4.4.8 and 5.4.3.5 of the Java Virtual Machine Specification.) |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
214 |
* <p> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
215 |
* Method handles produced by lookups or constant loads from methods or |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
216 |
* constructors with the variable arity modifier bit ({@code 0x0080}) |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
217 |
* have a corresponding variable arity, as if they were defined with |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
218 |
* the help of {@link #asVarargsCollector asVarargsCollector}. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
219 |
* <p> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
220 |
* A method reference may refer either to a static or non-static method. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
221 |
* In the non-static case, the method handle type includes an explicit |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
222 |
* receiver argument, prepended before any other arguments. |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
223 |
* In the method handle's type, the initial receiver argument is typed |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
224 |
* according to the class under which the method was initially requested. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
225 |
* (E.g., if a non-static method handle is obtained via {@code ldc}, |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
226 |
* the type of the receiver is the class named in the constant pool entry.) |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
227 |
* <p> |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
228 |
* Method handle constants are subject to the same link-time access checks |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
229 |
* their corresponding bytecode instructions, and the {@code ldc} instruction |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
230 |
* will throw corresponding linkage errors if the bytecode behaviors would |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
231 |
* throw such errors. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
232 |
* <p> |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
233 |
* As a corollary of this, access to protected members is restricted |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
234 |
* to receivers only of the accessing class, or one of its subclasses, |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
235 |
* and the accessing class must in turn be a subclass (or package sibling) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
236 |
* of the protected member's defining class. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
237 |
* If a method reference refers to a protected non-static method or field |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
238 |
* of a class outside the current package, the receiver argument will |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
239 |
* be narrowed to the type of the accessing class. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
240 |
* <p> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
241 |
* When a method handle to a virtual method is invoked, the method is |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
242 |
* always looked up in the receiver (that is, the first argument). |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
243 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
244 |
* A non-virtual method handle to a specific virtual method implementation |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
245 |
* can also be created. These do not perform virtual lookup based on |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
246 |
* receiver type. Such a method handle simulates the effect of |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
247 |
* an {@code invokespecial} instruction to the same method. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
248 |
* |
18569 | 249 |
* <h1>Usage examples</h1> |
4537 | 250 |
* Here are some examples of usage: |
20877
83ea903d5cf1
8026183: minor documentation problems in java.lang.invoke
rriggs
parents:
20535
diff
changeset
|
251 |
* <blockquote><pre>{@code |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
252 |
Object x, y; String s; int i; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
253 |
MethodType mt; MethodHandle mh; |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
254 |
MethodHandles.Lookup lookup = MethodHandles.lookup(); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
255 |
// mt is (char,char)String |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
256 |
mt = MethodType.methodType(String.class, char.class, char.class); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
257 |
mh = lookup.findVirtual(String.class, "replace", mt); |
7556
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
258 |
s = (String) mh.invokeExact("daddy",'d','n'); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
259 |
// invokeExact(Ljava/lang/String;CC)Ljava/lang/String; |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
260 |
assertEquals(s, "nanny"); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
261 |
// weakly typed invocation (using MHs.invoke) |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
262 |
s = (String) mh.invokeWithArguments("sappy", 'p', 'v'); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
263 |
assertEquals(s, "savvy"); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
264 |
// mt is (Object[])List |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
265 |
mt = MethodType.methodType(java.util.List.class, Object[].class); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
266 |
mh = lookup.findStatic(java.util.Arrays.class, "asList", mt); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
267 |
assert(mh.isVarargsCollector()); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
268 |
x = mh.invoke("one", "two"); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
269 |
// invoke(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/Object; |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
270 |
assertEquals(x, java.util.Arrays.asList("one","two")); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
271 |
// mt is (Object,Object,Object)Object |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
272 |
mt = MethodType.genericMethodType(3); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
273 |
mh = mh.asType(mt); |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
274 |
x = mh.invokeExact((Object)1, (Object)2, (Object)3); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
275 |
// invokeExact(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
276 |
assertEquals(x, java.util.Arrays.asList(1,2,3)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
277 |
// mt is ()int |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
278 |
mt = MethodType.methodType(int.class); |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
279 |
mh = lookup.findVirtual(java.util.List.class, "size", mt); |
7556
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
280 |
i = (int) mh.invokeExact(java.util.Arrays.asList(1,2,3)); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
281 |
// invokeExact(Ljava/util/List;)I |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
282 |
assert(i == 3); |
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
283 |
mt = MethodType.methodType(void.class, String.class); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
284 |
mh = lookup.findVirtual(java.io.PrintStream.class, "println", mt); |
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
285 |
mh.invokeExact(System.out, "Hello, world."); |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
286 |
// invokeExact(Ljava/io/PrintStream;Ljava/lang/String;)V |
18156 | 287 |
* }</pre></blockquote> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
288 |
* Each of the above calls to {@code invokeExact} or plain {@code invoke} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
289 |
* generates a single invokevirtual instruction with |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
290 |
* the symbolic type descriptor indicated in the following comment. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
291 |
* In these examples, the helper method {@code assertEquals} is assumed to |
20535
cc85c8626435
8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents:
20534
diff
changeset
|
292 |
* be a method which calls {@link java.util.Objects#equals(Object,Object) Objects.equals} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
293 |
* on its arguments, and asserts that the result is true. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
294 |
* |
18569 | 295 |
* <h1>Exceptions</h1> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
296 |
* The methods {@code invokeExact} and {@code invoke} are declared |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
297 |
* to throw {@link java.lang.Throwable Throwable}, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
298 |
* which is to say that there is no static restriction on what a method handle |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
299 |
* can throw. Since the JVM does not distinguish between checked |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
300 |
* and unchecked exceptions (other than by their class, of course), |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
301 |
* there is no particular effect on bytecode shape from ascribing |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
302 |
* checked exceptions to method handle invocations. But in Java source |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
303 |
* code, methods which perform method handle calls must either explicitly |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
304 |
* throw {@code Throwable}, or else must catch all |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
305 |
* throwables locally, rethrowing only those which are legal in the context, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
306 |
* and wrapping ones which are illegal. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
307 |
* |
18569 | 308 |
* <h1><a name="sigpoly"></a>Signature polymorphism</h1> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
309 |
* The unusual compilation and linkage behavior of |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
310 |
* {@code invokeExact} and plain {@code invoke} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
311 |
* is referenced by the term <em>signature polymorphism</em>. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
312 |
* As defined in the Java Language Specification, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
313 |
* a signature polymorphic method is one which can operate with |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
314 |
* any of a wide range of call signatures and return types. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
315 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
316 |
* In source code, a call to a signature polymorphic method will |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
317 |
* compile, regardless of the requested symbolic type descriptor. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
318 |
* As usual, the Java compiler emits an {@code invokevirtual} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
319 |
* instruction with the given symbolic type descriptor against the named method. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
320 |
* The unusual part is that the symbolic type descriptor is derived from |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
321 |
* the actual argument and return types, not from the method declaration. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
322 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
323 |
* When the JVM processes bytecode containing signature polymorphic calls, |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
324 |
* it will successfully link any such call, regardless of its symbolic type descriptor. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
325 |
* (In order to retain type safety, the JVM will guard such calls with suitable |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
326 |
* dynamic type checks, as described elsewhere.) |
4537 | 327 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
328 |
* Bytecode generators, including the compiler back end, are required to emit |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
329 |
* untransformed symbolic type descriptors for these methods. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
330 |
* Tools which determine symbolic linkage are required to accept such |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
331 |
* untransformed descriptors, without reporting linkage errors. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
332 |
* |
18569 | 333 |
* <h1>Interoperation between method handles and the Core Reflection API</h1> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
334 |
* Using factory methods in the {@link java.lang.invoke.MethodHandles.Lookup Lookup} API, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
335 |
* any class member represented by a Core Reflection API object |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
336 |
* can be converted to a behaviorally equivalent method handle. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
337 |
* For example, a reflective {@link java.lang.reflect.Method Method} can |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
338 |
* be converted to a method handle using |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
339 |
* {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
340 |
* The resulting method handles generally provide more direct and efficient |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
341 |
* access to the underlying class members. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
342 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
343 |
* As a special case, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
344 |
* when the Core Reflection API is used to view the signature polymorphic |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
345 |
* methods {@code invokeExact} or plain {@code invoke} in this class, |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
346 |
* they appear as ordinary non-polymorphic methods. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
347 |
* Their reflective appearance, as viewed by |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
348 |
* {@link java.lang.Class#getDeclaredMethod Class.getDeclaredMethod}, |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
349 |
* is unaffected by their special status in this API. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
350 |
* For example, {@link java.lang.reflect.Method#getModifiers Method.getModifiers} |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
351 |
* will report exactly those modifier bits required for any similarly |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
352 |
* declared method, including in this case {@code native} and {@code varargs} bits. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
353 |
* <p> |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
354 |
* As with any reflected method, these methods (when reflected) may be |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
355 |
* invoked via {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
356 |
* However, such reflective calls do not result in method handle invocations. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
357 |
* Such a call, if passed the required argument |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
358 |
* (a single one, of type {@code Object[]}), will ignore the argument and |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
359 |
* will throw an {@code UnsupportedOperationException}. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
360 |
* <p> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
361 |
* Since {@code invokevirtual} instructions can natively |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
362 |
* invoke method handles under any symbolic type descriptor, this reflective view conflicts |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
363 |
* with the normal presentation of these methods via bytecodes. |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
364 |
* Thus, these two native methods, when reflectively viewed by |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
365 |
* {@code Class.getDeclaredMethod}, may be regarded as placeholders only. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
366 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
367 |
* In order to obtain an invoker method for a particular type descriptor, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
368 |
* use {@link java.lang.invoke.MethodHandles#exactInvoker MethodHandles.exactInvoker}, |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
369 |
* or {@link java.lang.invoke.MethodHandles#invoker MethodHandles.invoker}. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
370 |
* The {@link java.lang.invoke.MethodHandles.Lookup#findVirtual Lookup.findVirtual} |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
371 |
* API is also able to return a method handle |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
372 |
* to call {@code invokeExact} or plain {@code invoke}, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
373 |
* for any specified type descriptor . |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
374 |
* |
18569 | 375 |
* <h1>Interoperation between method handles and Java generics</h1> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
376 |
* A method handle can be obtained on a method, constructor, or field |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
377 |
* which is declared with Java generic types. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
378 |
* As with the Core Reflection API, the type of the method handle |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
379 |
* will constructed from the erasure of the source-level type. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
380 |
* When a method handle is invoked, the types of its arguments |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
381 |
* or the return value cast type may be generic types or type instances. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
382 |
* If this occurs, the compiler will replace those |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
383 |
* types by their erasures when it constructs the symbolic type descriptor |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
384 |
* for the {@code invokevirtual} instruction. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
385 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
386 |
* Method handles do not represent |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
387 |
* their function-like types in terms of Java parameterized (generic) types, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
388 |
* because there are three mismatches between function-like types and parameterized |
4537 | 389 |
* Java types. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
390 |
* <ul> |
4537 | 391 |
* <li>Method types range over all possible arities, |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
392 |
* from no arguments to up to the <a href="MethodHandle.html#maxarity">maximum number</a> of allowed arguments. |
4537 | 393 |
* Generics are not variadic, and so cannot represent this.</li> |
394 |
* <li>Method types can specify arguments of primitive types, |
|
395 |
* which Java generic types cannot range over.</li> |
|
396 |
* <li>Higher order functions over method handles (combinators) are |
|
397 |
* often generic across a wide range of function types, including |
|
398 |
* those of multiple arities. It is impossible to represent such |
|
399 |
* genericity with a Java type parameter.</li> |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
400 |
* </ul> |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
401 |
* |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
402 |
* <h1><a name="maxarity"></a>Arity limits</h1> |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
403 |
* The JVM imposes on all methods and constructors of any kind an absolute |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
404 |
* limit of 255 stacked arguments. This limit can appear more restrictive |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
405 |
* in certain cases: |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
406 |
* <ul> |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
407 |
* <li>A {@code long} or {@code double} argument counts (for purposes of arity limits) as two argument slots. |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
408 |
* <li>A non-static method consumes an extra argument for the object on which the method is called. |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
409 |
* <li>A constructor consumes an extra argument for the object which is being constructed. |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
410 |
* <li>Since a method handle’s {@code invoke} method (or other signature-polymorphic method) is non-virtual, |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
411 |
* it consumes an extra argument for the method handle itself, in addition to any non-virtual receiver object. |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
412 |
* </ul> |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
413 |
* These limits imply that certain method handles cannot be created, solely because of the JVM limit on stacked arguments. |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
414 |
* For example, if a static JVM method accepts exactly 255 arguments, a method handle cannot be created for it. |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
415 |
* Attempts to create method handles with impossible method types lead to an {@link IllegalArgumentException}. |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
416 |
* In particular, a method handle’s type must not have an arity of the exact maximum 255. |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
417 |
* |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
418 |
* @see MethodType |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
419 |
* @see MethodHandles |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
420 |
* @author John Rose, JSR 292 EG |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
421 |
*/ |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8350
diff
changeset
|
422 |
public abstract class MethodHandle { |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
423 |
|
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
424 |
/** |
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
425 |
* Internal marker interface which distinguishes (to the Java compiler) |
8350
2479a9688a39
7016520: JSR 292 rules for polymorphic signature processing must be in package-info
jrose
parents:
8346
diff
changeset
|
426 |
* those methods which are <a href="MethodHandle.html#sigpoly">signature polymorphic</a>. |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
427 |
*/ |
8350
2479a9688a39
7016520: JSR 292 rules for polymorphic signature processing must be in package-info
jrose
parents:
8346
diff
changeset
|
428 |
@java.lang.annotation.Target({java.lang.annotation.ElementType.METHOD}) |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
429 |
@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME) |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
430 |
@interface PolymorphicSignature { } |
4537 | 431 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
432 |
private final MethodType type; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
433 |
/*private*/ final LambdaForm form; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
434 |
// form is not private so that invokers can easily fetch it |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
435 |
/*private*/ MethodHandle asTypeCache; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
436 |
// asTypeCache is not private so that invokers can easily fetch it |
29020
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
437 |
/*non-public*/ byte customizationCount; |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
438 |
// customizationCount should be accessible from invokers |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
439 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
440 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
441 |
* Reports the type of this method handle. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
442 |
* Every invocation of this method handle via {@code invokeExact} must exactly match this type. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
443 |
* @return the method handle type |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
444 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
445 |
public MethodType type() { |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
446 |
return type; |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
447 |
} |
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
448 |
|
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
449 |
/** |
8821
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8350
diff
changeset
|
450 |
* Package-private constructor for the method handle implementation hierarchy. |
2836ee97ee27
6839872: remove implementation inheritance from JSR 292 APIs
jrose
parents:
8350
diff
changeset
|
451 |
* Method handle inheritance will be contained completely within |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
452 |
* the {@code java.lang.invoke} package. |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
453 |
*/ |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
454 |
// @param type type (permanently assigned) of the new method handle |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
455 |
/*non-public*/ MethodHandle(MethodType type, LambdaForm form) { |
29094
a4fd2b5e49f8
8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents:
29020
diff
changeset
|
456 |
this.type = Objects.requireNonNull(type); |
a4fd2b5e49f8
8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents:
29020
diff
changeset
|
457 |
this.form = Objects.requireNonNull(form).uncustomize(); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
458 |
|
29020
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
459 |
this.form.prepare(); // TO DO: Try to delay this step until just before invocation. |
4537 | 460 |
} |
461 |
||
7562
a0ad195efe2c
7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents:
7556
diff
changeset
|
462 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
463 |
* Invokes the method handle, allowing any caller type descriptor, but requiring an exact type match. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
464 |
* The symbolic type descriptor at the call site of {@code invokeExact} must |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
465 |
* exactly match this method handle's {@link #type type}. |
4537 | 466 |
* No conversions are allowed on arguments or return values. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
467 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
468 |
* When this method is observed via the Core Reflection API, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
469 |
* it will appear as a single native method, taking an object array and returning an object. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
470 |
* If this native method is invoked directly via |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
471 |
* {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}, via JNI, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
472 |
* or indirectly via {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
473 |
* it will throw an {@code UnsupportedOperationException}. |
18569 | 474 |
* @param args the signature-polymorphic parameter list, statically represented using varargs |
475 |
* @return the signature-polymorphic result, statically represented using {@code Object} |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
476 |
* @throws WrongMethodTypeException if the target's type is not identical with the caller's symbolic type descriptor |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
477 |
* @throws Throwable anything thrown by the underlying method propagates unchanged through the method handle call |
4537 | 478 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29094
diff
changeset
|
479 |
@HotSpotIntrinsicCandidate |
7556
4a5711d43948
6979327: method handle invocation should use casts instead of type parameters to specify return type
jrose
parents:
7555
diff
changeset
|
480 |
public final native @PolymorphicSignature Object invokeExact(Object... args) throws Throwable; |
5722
4ada807383c8
6939134: JSR 292 adjustments to method handle invocation
jrose
parents:
4537
diff
changeset
|
481 |
|
4537 | 482 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
483 |
* Invokes the method handle, allowing any caller type descriptor, |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
484 |
* and optionally performing conversions on arguments and return values. |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
485 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
486 |
* If the call site's symbolic type descriptor exactly matches this method handle's {@link #type type}, |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
487 |
* the call proceeds as if by {@link #invokeExact invokeExact}. |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
488 |
* <p> |
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
489 |
* Otherwise, the call proceeds as if this method handle were first |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
490 |
* adjusted by calling {@link #asType asType} to adjust this method handle |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
491 |
* to the required type, and then the call proceeds as if by |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
492 |
* {@link #invokeExact invokeExact} on the adjusted method handle. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
493 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
494 |
* There is no guarantee that the {@code asType} call is actually made. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
495 |
* If the JVM can predict the results of making the call, it may perform |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
496 |
* adaptations directly on the caller's arguments, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
497 |
* and call the target method handle according to its own exact type. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
498 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
499 |
* The resolved type descriptor at the call site of {@code invoke} must |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
500 |
* be a valid argument to the receivers {@code asType} method. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
501 |
* In particular, the caller must specify the same argument arity |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
502 |
* as the callee's type, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
503 |
* if the callee is not a {@linkplain #asVarargsCollector variable arity collector}. |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
504 |
* <p> |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
505 |
* When this method is observed via the Core Reflection API, |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
506 |
* it will appear as a single native method, taking an object array and returning an object. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
507 |
* If this native method is invoked directly via |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
508 |
* {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}, via JNI, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
509 |
* or indirectly via {@link java.lang.invoke.MethodHandles.Lookup#unreflect Lookup.unreflect}, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
510 |
* it will throw an {@code UnsupportedOperationException}. |
18569 | 511 |
* @param args the signature-polymorphic parameter list, statically represented using varargs |
512 |
* @return the signature-polymorphic result, statically represented using {@code Object} |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
513 |
* @throws WrongMethodTypeException if the target's type cannot be adjusted to the caller's symbolic type descriptor |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
514 |
* @throws ClassCastException if the target's type can be adjusted to the caller, but a reference cast fails |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
515 |
* @throws Throwable anything thrown by the underlying method propagates unchanged through the method handle call |
4537 | 516 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29094
diff
changeset
|
517 |
@HotSpotIntrinsicCandidate |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
518 |
public final native @PolymorphicSignature Object invoke(Object... args) throws Throwable; |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
519 |
|
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
520 |
/** |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
521 |
* Private method for trusted invocation of a method handle respecting simplified signatures. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
522 |
* Type mismatches will not throw {@code WrongMethodTypeException}, but could crash the JVM. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
523 |
* <p> |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
524 |
* The caller signature is restricted to the following basic types: |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
525 |
* Object, int, long, float, double, and void return. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
526 |
* <p> |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
527 |
* The caller is responsible for maintaining type correctness by ensuring |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
528 |
* that the each outgoing argument value is a member of the range of the corresponding |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
529 |
* callee argument type. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
530 |
* (The caller should therefore issue appropriate casts and integer narrowing |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
531 |
* operations on outgoing argument values.) |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
532 |
* The caller can assume that the incoming result value is part of the range |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
533 |
* of the callee's return type. |
18569 | 534 |
* @param args the signature-polymorphic parameter list, statically represented using varargs |
535 |
* @return the signature-polymorphic result, statically represented using {@code Object} |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
536 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29094
diff
changeset
|
537 |
@HotSpotIntrinsicCandidate |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
538 |
/*non-public*/ final native @PolymorphicSignature Object invokeBasic(Object... args) throws Throwable; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
539 |
|
18569 | 540 |
/** |
541 |
* Private method for trusted invocation of a MemberName of kind {@code REF_invokeVirtual}. |
|
542 |
* The caller signature is restricted to basic types as with {@code invokeBasic}. |
|
543 |
* The trailing (not leading) argument must be a MemberName. |
|
544 |
* @param args the signature-polymorphic parameter list, statically represented using varargs |
|
545 |
* @return the signature-polymorphic result, statically represented using {@code Object} |
|
546 |
*/ |
|
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29094
diff
changeset
|
547 |
@HotSpotIntrinsicCandidate |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
548 |
/*non-public*/ static native @PolymorphicSignature Object linkToVirtual(Object... args) throws Throwable; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
549 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
550 |
/** |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
551 |
* Private method for trusted invocation of a MemberName of kind {@code REF_invokeStatic}. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
552 |
* The caller signature is restricted to basic types as with {@code invokeBasic}. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
553 |
* The trailing (not leading) argument must be a MemberName. |
18569 | 554 |
* @param args the signature-polymorphic parameter list, statically represented using varargs |
555 |
* @return the signature-polymorphic result, statically represented using {@code Object} |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
556 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29094
diff
changeset
|
557 |
@HotSpotIntrinsicCandidate |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
558 |
/*non-public*/ static native @PolymorphicSignature Object linkToStatic(Object... args) throws Throwable; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
559 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
560 |
/** |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
561 |
* Private method for trusted invocation of a MemberName of kind {@code REF_invokeSpecial}. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
562 |
* The caller signature is restricted to basic types as with {@code invokeBasic}. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
563 |
* The trailing (not leading) argument must be a MemberName. |
18569 | 564 |
* @param args the signature-polymorphic parameter list, statically represented using varargs |
565 |
* @return the signature-polymorphic result, statically represented using {@code Object} |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
566 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29094
diff
changeset
|
567 |
@HotSpotIntrinsicCandidate |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
568 |
/*non-public*/ static native @PolymorphicSignature Object linkToSpecial(Object... args) throws Throwable; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
569 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
570 |
/** |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
571 |
* Private method for trusted invocation of a MemberName of kind {@code REF_invokeInterface}. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
572 |
* The caller signature is restricted to basic types as with {@code invokeBasic}. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
573 |
* The trailing (not leading) argument must be a MemberName. |
18569 | 574 |
* @param args the signature-polymorphic parameter list, statically represented using varargs |
575 |
* @return the signature-polymorphic result, statically represented using {@code Object} |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
576 |
*/ |
31671
362e0c0acece
8076112: Add @HotSpotIntrinsicCandidate annotation to indicate methods for which Java Runtime has intrinsics
zmajo
parents:
29094
diff
changeset
|
577 |
@HotSpotIntrinsicCandidate |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
578 |
/*non-public*/ static native @PolymorphicSignature Object linkToInterface(Object... args) throws Throwable; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
579 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
580 |
/** |
20535
cc85c8626435
8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents:
20534
diff
changeset
|
581 |
* Performs a variable arity invocation, passing the arguments in the given list |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
582 |
* to the method handle, as if via an inexact {@link #invoke invoke} from a call site |
4537 | 583 |
* which mentions only the type {@code Object}, and whose arity is the length |
20535
cc85c8626435
8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents:
20534
diff
changeset
|
584 |
* of the argument list. |
4537 | 585 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
586 |
* Specifically, execution proceeds as if by the following steps, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
587 |
* although the methods are not guaranteed to be called if the JVM |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
588 |
* can predict their effects. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
589 |
* <ul> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
590 |
* <li>Determine the length of the argument array as {@code N}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
591 |
* For a null reference, {@code N=0}. </li> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
592 |
* <li>Determine the general type {@code TN} of {@code N} arguments as |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
593 |
* as {@code TN=MethodType.genericMethodType(N)}.</li> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
594 |
* <li>Force the original target method handle {@code MH0} to the |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
595 |
* required type, as {@code MH1 = MH0.asType(TN)}. </li> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
596 |
* <li>Spread the array into {@code N} separate arguments {@code A0, ...}. </li> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
597 |
* <li>Invoke the type-adjusted method handle on the unpacked arguments: |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
598 |
* MH1.invokeExact(A0, ...). </li> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
599 |
* <li>Take the return value as an {@code Object} reference. </li> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
600 |
* </ul> |
4537 | 601 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
602 |
* Because of the action of the {@code asType} step, the following argument |
4537 | 603 |
* conversions are applied as necessary: |
604 |
* <ul> |
|
605 |
* <li>reference casting |
|
606 |
* <li>unboxing |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
607 |
* <li>widening primitive conversions |
4537 | 608 |
* </ul> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
609 |
* <p> |
4537 | 610 |
* The result returned by the call is boxed if it is a primitive, |
611 |
* or forced to null if the return type is void. |
|
612 |
* <p> |
|
613 |
* This call is equivalent to the following code: |
|
20877
83ea903d5cf1
8026183: minor documentation problems in java.lang.invoke
rriggs
parents:
20535
diff
changeset
|
614 |
* <blockquote><pre>{@code |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
615 |
* MethodHandle invoker = MethodHandles.spreadInvoker(this.type(), 0); |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
616 |
* Object result = invoker.invokeExact(this, arguments); |
20535
cc85c8626435
8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents:
20534
diff
changeset
|
617 |
* }</pre></blockquote> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
618 |
* <p> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
619 |
* Unlike the signature polymorphic methods {@code invokeExact} and {@code invoke}, |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
620 |
* {@code invokeWithArguments} can be accessed normally via the Core Reflection API and JNI. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
621 |
* It can therefore be used as a bridge between native or reflective code and method handles. |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
622 |
* |
4537 | 623 |
* @param arguments the arguments to pass to the target |
624 |
* @return the result returned by the target |
|
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
625 |
* @throws ClassCastException if an argument cannot be converted by reference casting |
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
626 |
* @throws WrongMethodTypeException if the target's type cannot be adjusted to take the given number of {@code Object} arguments |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
627 |
* @throws Throwable anything thrown by the target method invocation |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
628 |
* @see MethodHandles#spreadInvoker |
4537 | 629 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
630 |
public Object invokeWithArguments(Object... arguments) throws Throwable { |
26468 | 631 |
MethodType invocationType = MethodType.genericMethodType(arguments == null ? 0 : arguments.length); |
632 |
return invocationType.invokers().spreadInvoker(0).invokeExact(asType(invocationType), arguments); |
|
4537 | 633 |
} |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
634 |
|
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
635 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
636 |
* Performs a variable arity invocation, passing the arguments in the given array |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
637 |
* to the method handle, as if via an inexact {@link #invoke invoke} from a call site |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
638 |
* which mentions only the type {@code Object}, and whose arity is the length |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
639 |
* of the argument array. |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
640 |
* <p> |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
641 |
* This method is also equivalent to the following code: |
20877
83ea903d5cf1
8026183: minor documentation problems in java.lang.invoke
rriggs
parents:
20535
diff
changeset
|
642 |
* <blockquote><pre>{@code |
83ea903d5cf1
8026183: minor documentation problems in java.lang.invoke
rriggs
parents:
20535
diff
changeset
|
643 |
* invokeWithArguments(arguments.toArray() |
83ea903d5cf1
8026183: minor documentation problems in java.lang.invoke
rriggs
parents:
20535
diff
changeset
|
644 |
* }</pre></blockquote> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
645 |
* |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
646 |
* @param arguments the arguments to pass to the target |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
647 |
* @return the result returned by the target |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
648 |
* @throws NullPointerException if {@code arguments} is a null reference |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
649 |
* @throws ClassCastException if an argument cannot be converted by reference casting |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
650 |
* @throws WrongMethodTypeException if the target's type cannot be adjusted to take the given number of {@code Object} arguments |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
651 |
* @throws Throwable anything thrown by the target method invocation |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
652 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
653 |
public Object invokeWithArguments(java.util.List<?> arguments) throws Throwable { |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
654 |
return invokeWithArguments(arguments.toArray()); |
4537 | 655 |
} |
656 |
||
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
657 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
658 |
* Produces an adapter method handle which adapts the type of the |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
659 |
* current method handle to a new type. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
660 |
* The resulting method handle is guaranteed to report a type |
4537 | 661 |
* which is equal to the desired new type. |
662 |
* <p> |
|
663 |
* If the original type and new type are equal, returns {@code this}. |
|
664 |
* <p> |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
665 |
* The new method handle, when invoked, will perform the following |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
666 |
* steps: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
667 |
* <ul> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
668 |
* <li>Convert the incoming argument list to match the original |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
669 |
* method handle's argument list. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
670 |
* <li>Invoke the original method handle on the converted argument list. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
671 |
* <li>Convert any result returned by the original method handle |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
672 |
* to the return type of new method handle. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
673 |
* </ul> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
674 |
* <p> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
675 |
* This method provides the crucial behavioral difference between |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
676 |
* {@link #invokeExact invokeExact} and plain, inexact {@link #invoke invoke}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
677 |
* The two methods |
25979 | 678 |
* perform the same steps when the caller's type descriptor exactly matches |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
679 |
* the callee's, but when the types differ, plain {@link #invoke invoke} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
680 |
* also calls {@code asType} (or some internal equivalent) in order |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
681 |
* to match up the caller's and callee's types. |
4537 | 682 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
683 |
* If the current method is a variable arity method handle |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
684 |
* argument list conversion may involve the conversion and collection |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
685 |
* of several arguments into an array, as |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
686 |
* {@linkplain #asVarargsCollector described elsewhere}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
687 |
* In every other case, all conversions are applied <em>pairwise</em>, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
688 |
* which means that each argument or return value is converted to |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
689 |
* exactly one argument or return value (or no return value). |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
690 |
* The applied conversions are defined by consulting the |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
691 |
* the corresponding component types of the old and new |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
692 |
* method handle types. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
693 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
694 |
* Let <em>T0</em> and <em>T1</em> be corresponding new and old parameter types, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
695 |
* or old and new return types. Specifically, for some valid index {@code i}, let |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
696 |
* <em>T0</em>{@code =newType.parameterType(i)} and <em>T1</em>{@code =this.type().parameterType(i)}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
697 |
* Or else, going the other way for return values, let |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
698 |
* <em>T0</em>{@code =this.type().returnType()} and <em>T1</em>{@code =newType.returnType()}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
699 |
* If the types are the same, the new method handle makes no change |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
700 |
* to the corresponding argument or return value (if any). |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
701 |
* Otherwise, one of the following conversions is applied |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
702 |
* if possible: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
703 |
* <ul> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
704 |
* <li>If <em>T0</em> and <em>T1</em> are references, then a cast to <em>T1</em> is applied. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
705 |
* (The types do not need to be related in any particular way. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
706 |
* This is because a dynamic value of null can convert to any reference type.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
707 |
* <li>If <em>T0</em> and <em>T1</em> are primitives, then a Java method invocation |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
708 |
* conversion (JLS 5.3) is applied, if one exists. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
709 |
* (Specifically, <em>T0</em> must convert to <em>T1</em> by a widening primitive conversion.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
710 |
* <li>If <em>T0</em> is a primitive and <em>T1</em> a reference, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
711 |
* a Java casting conversion (JLS 5.5) is applied if one exists. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
712 |
* (Specifically, the value is boxed from <em>T0</em> to its wrapper class, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
713 |
* which is then widened as needed to <em>T1</em>.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
714 |
* <li>If <em>T0</em> is a reference and <em>T1</em> a primitive, an unboxing |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
715 |
* conversion will be applied at runtime, possibly followed |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
716 |
* by a Java method invocation conversion (JLS 5.3) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
717 |
* on the primitive value. (These are the primitive widening conversions.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
718 |
* <em>T0</em> must be a wrapper class or a supertype of one. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
719 |
* (In the case where <em>T0</em> is Object, these are the conversions |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
720 |
* allowed by {@link java.lang.reflect.Method#invoke java.lang.reflect.Method.invoke}.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
721 |
* The unboxing conversion must have a possibility of success, which means that |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
722 |
* if <em>T0</em> is not itself a wrapper class, there must exist at least one |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
723 |
* wrapper class <em>TW</em> which is a subtype of <em>T0</em> and whose unboxed |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
724 |
* primitive value can be widened to <em>T1</em>. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
725 |
* <li>If the return type <em>T1</em> is marked as void, any returned value is discarded |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
726 |
* <li>If the return type <em>T0</em> is void and <em>T1</em> a reference, a null value is introduced. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
727 |
* <li>If the return type <em>T0</em> is void and <em>T1</em> a primitive, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
728 |
* a zero value is introduced. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
729 |
* </ul> |
24572
5c9e5961d21c
8037210: Get rid of char-based descriptions 'J' of basic types
vlivanov
parents:
22960
diff
changeset
|
730 |
* (<em>Note:</em> Both <em>T0</em> and <em>T1</em> may be regarded as static types, |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
731 |
* because neither corresponds specifically to the <em>dynamic type</em> of any |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
732 |
* actual argument or return value.) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
733 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
734 |
* The method handle conversion cannot be made if any one of the required |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
735 |
* pairwise conversions cannot be made. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
736 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
737 |
* At runtime, the conversions applied to reference arguments |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
738 |
* or return values may require additional runtime checks which can fail. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
739 |
* An unboxing operation may fail because the original reference is null, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
740 |
* causing a {@link java.lang.NullPointerException NullPointerException}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
741 |
* An unboxing operation or a reference cast may also fail on a reference |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
742 |
* to an object of the wrong type, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
743 |
* causing a {@link java.lang.ClassCastException ClassCastException}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
744 |
* Although an unboxing operation may accept several kinds of wrappers, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
745 |
* if none are available, a {@code ClassCastException} will be thrown. |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
746 |
* |
4537 | 747 |
* @param newType the expected type of the new method handle |
748 |
* @return a method handle which delegates to {@code this} after performing |
|
749 |
* any necessary argument conversions, and arranges for any |
|
750 |
* necessary return value conversions |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
751 |
* @throws NullPointerException if {@code newType} is a null reference |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
752 |
* @throws WrongMethodTypeException if the conversion cannot be made |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
753 |
* @see MethodHandles#explicitCastArguments |
4537 | 754 |
*/ |
7554
8a0ad9757002
6939224: MethodHandle.invokeGeneric needs to perform the correct set of conversions
jrose
parents:
7051
diff
changeset
|
755 |
public MethodHandle asType(MethodType newType) { |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
756 |
// Fast path alternative to a heavyweight {@code asType} call. |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
757 |
// Return 'this' if the conversion will be a no-op. |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
758 |
if (newType == type) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
759 |
return this; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
760 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
761 |
// Return 'this.asTypeCache' if the conversion is already memoized. |
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
762 |
MethodHandle atc = asTypeCached(newType); |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
763 |
if (atc != null) { |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
764 |
return atc; |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
765 |
} |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
766 |
return asTypeUncached(newType); |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
767 |
} |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
768 |
|
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
769 |
private MethodHandle asTypeCached(MethodType newType) { |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
770 |
MethodHandle atc = asTypeCache; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
771 |
if (atc != null && newType == atc.type) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
772 |
return atc; |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
773 |
} |
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
774 |
return null; |
20527
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
775 |
} |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
776 |
|
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
777 |
/** Override this to change asType behavior. */ |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
778 |
/*non-public*/ MethodHandle asTypeUncached(MethodType newType) { |
d241258cfbcb
8024761: JSR 292 improve performance of generic invocation
jrose
parents:
19817
diff
changeset
|
779 |
if (!type.isConvertibleTo(newType)) |
9730
e4b334d47f4b
7032850: MethodHandle.invokeGeneric throws unspecified RuntimeException if parameterized method is called
jrose
parents:
9646
diff
changeset
|
780 |
throw new WrongMethodTypeException("cannot convert "+this+" to "+newType); |
26474
655d08549e43
8050877: Improve code for pairwise argument conversions and value boxing/unboxing
vlivanov
parents:
26473
diff
changeset
|
781 |
return asTypeCache = MethodHandleImpl.makePairwiseConvert(this, newType, true); |
4537 | 782 |
} |
783 |
||
784 |
/** |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
785 |
* Makes an <em>array-spreading</em> method handle, which accepts a trailing array argument |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
786 |
* and spreads its elements as positional arguments. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
787 |
* The new method handle adapts, as its <i>target</i>, |
4537 | 788 |
* the current method handle. The type of the adapter will be |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
789 |
* the same as the type of the target, except that the final |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
790 |
* {@code arrayLength} parameters of the target's type are replaced |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
791 |
* by a single array parameter of type {@code arrayType}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
792 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
793 |
* If the array element type differs from any of the corresponding |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
794 |
* argument types on the original target, |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
795 |
* the original target is adapted to take the array elements directly, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
796 |
* as if by a call to {@link #asType asType}. |
4537 | 797 |
* <p> |
798 |
* When called, the adapter replaces a trailing array argument |
|
799 |
* by the array's elements, each as its own argument to the target. |
|
800 |
* (The order of the arguments is preserved.) |
|
801 |
* They are converted pairwise by casting and/or unboxing |
|
802 |
* to the types of the trailing parameters of the target. |
|
803 |
* Finally the target is called. |
|
804 |
* What the target eventually returns is returned unchanged by the adapter. |
|
805 |
* <p> |
|
806 |
* Before calling the target, the adapter verifies that the array |
|
807 |
* contains exactly enough elements to provide a correct argument count |
|
808 |
* to the target method handle. |
|
809 |
* (The array may also be null when zero elements are required.) |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
810 |
* <p> |
20530
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
811 |
* If, when the adapter is called, the supplied array argument does |
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
812 |
* not have the correct number of elements, the adapter will throw |
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
813 |
* an {@link IllegalArgumentException} instead of invoking the target. |
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
814 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
815 |
* Here are some simple examples of array-spreading method handles: |
18156 | 816 |
* <blockquote><pre>{@code |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
817 |
MethodHandle equals = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
818 |
.findVirtual(String.class, "equals", methodType(boolean.class, Object.class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
819 |
assert( (boolean) equals.invokeExact("me", (Object)"me")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
820 |
assert(!(boolean) equals.invokeExact("me", (Object)"thee")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
821 |
// spread both arguments from a 2-array: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
822 |
MethodHandle eq2 = equals.asSpreader(Object[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
823 |
assert( (boolean) eq2.invokeExact(new Object[]{ "me", "me" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
824 |
assert(!(boolean) eq2.invokeExact(new Object[]{ "me", "thee" })); |
20530
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
825 |
// try to spread from anything but a 2-array: |
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
826 |
for (int n = 0; n <= 10; n++) { |
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
827 |
Object[] badArityArgs = (n == 2 ? null : new Object[n]); |
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
828 |
try { assert((boolean) eq2.invokeExact(badArityArgs) && false); } |
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
829 |
catch (IllegalArgumentException ex) { } // OK |
b54a1f5cd35f
8001109: arity mismatch on a call to spreader method handle should elicit IllegalArgumentException
jrose
parents:
20529
diff
changeset
|
830 |
} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
831 |
// spread both arguments from a String array: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
832 |
MethodHandle eq2s = equals.asSpreader(String[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
833 |
assert( (boolean) eq2s.invokeExact(new String[]{ "me", "me" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
834 |
assert(!(boolean) eq2s.invokeExact(new String[]{ "me", "thee" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
835 |
// spread second arguments from a 1-array: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
836 |
MethodHandle eq1 = equals.asSpreader(Object[].class, 1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
837 |
assert( (boolean) eq1.invokeExact("me", new Object[]{ "me" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
838 |
assert(!(boolean) eq1.invokeExact("me", new Object[]{ "thee" })); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
839 |
// spread no arguments from a 0-array or null: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
840 |
MethodHandle eq0 = equals.asSpreader(Object[].class, 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
841 |
assert( (boolean) eq0.invokeExact("me", (Object)"me", new Object[0])); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
842 |
assert(!(boolean) eq0.invokeExact("me", (Object)"thee", (Object[])null)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
843 |
// asSpreader and asCollector are approximate inverses: |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
844 |
for (int n = 0; n <= 2; n++) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
845 |
for (Class<?> a : new Class<?>[]{Object[].class, String[].class, CharSequence[].class}) { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
846 |
MethodHandle equals2 = equals.asSpreader(a, n).asCollector(a, n); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
847 |
assert( (boolean) equals2.invokeWithArguments("me", "me")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
848 |
assert(!(boolean) equals2.invokeWithArguments("me", "thee")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
849 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
850 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
851 |
MethodHandle caToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
852 |
.findStatic(Arrays.class, "toString", methodType(String.class, char[].class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
853 |
assertEquals("[A, B, C]", (String) caToString.invokeExact("ABC".toCharArray())); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
854 |
MethodHandle caString3 = caToString.asCollector(char[].class, 3); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
855 |
assertEquals("[A, B, C]", (String) caString3.invokeExact('A', 'B', 'C')); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
856 |
MethodHandle caToString2 = caString3.asSpreader(char[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
857 |
assertEquals("[A, B, C]", (String) caToString2.invokeExact('A', "BC".toCharArray())); |
18156 | 858 |
* }</pre></blockquote> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
859 |
* @param arrayType usually {@code Object[]}, the type of the array argument from which to extract the spread arguments |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
860 |
* @param arrayLength the number of arguments to spread from an incoming array argument |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
861 |
* @return a new method handle which spreads its final array argument, |
4537 | 862 |
* before calling the original method handle |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
863 |
* @throws NullPointerException if {@code arrayType} is a null reference |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
864 |
* @throws IllegalArgumentException if {@code arrayType} is not an array type, |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
865 |
* or if target does not have at least |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
866 |
* {@code arrayLength} parameter types, |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
867 |
* or if {@code arrayLength} is negative, |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
868 |
* or if the resulting method handle's type would have |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
869 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
870 |
* @throws WrongMethodTypeException if the implied {@code asType} call fails |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
871 |
* @see #asCollector |
4537 | 872 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
873 |
public MethodHandle asSpreader(Class<?> arrayType, int arrayLength) { |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
874 |
return asSpreader(type().parameterCount() - arrayLength, arrayType, arrayLength); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
875 |
} |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
876 |
|
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
877 |
/** |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
878 |
* Makes an <em>array-spreading</em> method handle, which accepts an array argument at a given position and spreads |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
879 |
* its elements as positional arguments in place of the array. The new method handle adapts, as its <i>target</i>, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
880 |
* the current method handle. The type of the adapter will be the same as the type of the target, except that the |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
881 |
* {@code arrayLength} parameters of the target's type, starting at the zero-based position {@code spreadArgPos}, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
882 |
* are replaced by a single array parameter of type {@code arrayType}. |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
883 |
* <p> |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
884 |
* This method behaves very much like {@link #asSpreader(Class, int)}, but accepts an additional {@code spreadArgPos} |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
885 |
* argument to indicate at which position in the parameter list the spreading should take place. |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
886 |
* <p> |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
887 |
* @apiNote Example: |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
888 |
* <blockquote><pre>{@code |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
889 |
MethodHandle compare = LOOKUP.findStatic(Objects.class, "compare", methodType(int.class, Object.class, Object.class, Comparator.class)); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
890 |
MethodHandle compare2FromArray = compare.asSpreader(0, Object[].class, 2); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
891 |
Object[] ints = new Object[]{3, 9, 7, 7}; |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
892 |
Comparator<Integer> cmp = (a, b) -> a - b; |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
893 |
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 0, 2), cmp) < 0); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
894 |
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 1, 3), cmp) > 0); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
895 |
assertTrue((int) compare2FromArray.invoke(Arrays.copyOfRange(ints, 2, 4), cmp) == 0); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
896 |
* }</pre></blockquote> |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
897 |
* @param spreadArgPos the position (zero-based index) in the argument list at which spreading should start. |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
898 |
* @param arrayType usually {@code Object[]}, the type of the array argument from which to extract the spread arguments |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
899 |
* @param arrayLength the number of arguments to spread from an incoming array argument |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
900 |
* @return a new method handle which spreads an array argument at a given position, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
901 |
* before calling the original method handle |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
902 |
* @throws NullPointerException if {@code arrayType} is a null reference |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
903 |
* @throws IllegalArgumentException if {@code arrayType} is not an array type, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
904 |
* or if target does not have at least |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
905 |
* {@code arrayLength} parameter types, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
906 |
* or if {@code arrayLength} is negative, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
907 |
* or if {@code spreadArgPos} has an illegal value (negative, or together with arrayLength exceeding the |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
908 |
* number of arguments), |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
909 |
* or if the resulting method handle's type would have |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
910 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
911 |
* @throws WrongMethodTypeException if the implied {@code asType} call fails |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
912 |
* |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
913 |
* @see #asSpreader(Class, int) |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
914 |
* @since 9 |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
915 |
*/ |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
916 |
public MethodHandle asSpreader(int spreadArgPos, Class<?> arrayType, int arrayLength) { |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
917 |
MethodType postSpreadType = asSpreaderChecks(arrayType, spreadArgPos, arrayLength); |
27755 | 918 |
MethodHandle afterSpread = this.asType(postSpreadType); |
919 |
BoundMethodHandle mh = afterSpread.rebind(); |
|
920 |
LambdaForm lform = mh.editor().spreadArgumentsForm(1 + spreadArgPos, arrayType, arrayLength); |
|
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
921 |
MethodType preSpreadType = postSpreadType.replaceParameterTypes(spreadArgPos, spreadArgPos + arrayLength, arrayType); |
27755 | 922 |
return mh.copyWith(preSpreadType, lform); |
9731
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
923 |
} |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
924 |
|
26477
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
925 |
/** |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
926 |
* See if {@code asSpreader} can be validly called with the given arguments. |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
927 |
* Return the type of the method handle call after spreading but before conversions. |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
928 |
*/ |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
929 |
private MethodType asSpreaderChecks(Class<?> arrayType, int pos, int arrayLength) { |
9731
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
930 |
spreadArrayChecks(arrayType, arrayLength); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
931 |
int nargs = type().parameterCount(); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
932 |
if (nargs < arrayLength || arrayLength < 0) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
933 |
throw newIllegalArgumentException("bad spread array length"); |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
934 |
if (pos < 0 || pos + arrayLength > nargs) { |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
935 |
throw newIllegalArgumentException("bad spread position"); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
936 |
} |
26477
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
937 |
Class<?> arrayElement = arrayType.getComponentType(); |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
938 |
MethodType mtype = type(); |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
939 |
boolean match = true, fail = false; |
34336
3f251ae0f2d3
8143798: jck failures: api/java_lang/invoke/MethodHandle/index_MethodsTests[asSpreaderWMTE]: java.lang.VerifyError: Bad type on operand stack
mhaupt
parents:
33874
diff
changeset
|
940 |
for (int i = pos; i < pos + arrayLength; i++) { |
26477
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
941 |
Class<?> ptype = mtype.parameterType(i); |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
942 |
if (ptype != arrayElement) { |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
943 |
match = false; |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
944 |
if (!MethodType.canConvert(arrayElement, ptype)) { |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
945 |
fail = true; |
9731
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
946 |
break; |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
947 |
} |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
948 |
} |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
949 |
} |
26477
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
950 |
if (match) return mtype; |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
951 |
MethodType needType = mtype.asSpreaderType(arrayType, pos, arrayLength); |
26477
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
952 |
if (!fail) return needType; |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
953 |
// elicit an error: |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
954 |
this.asType(needType); |
e095a2b8a1f8
8057654: Extract checks performed during MethodHandle construction into separate methods
vlivanov
parents:
26474
diff
changeset
|
955 |
throw newInternalError("should not return", null); |
9731
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
956 |
} |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
957 |
|
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
958 |
private void spreadArrayChecks(Class<?> arrayType, int arrayLength) { |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
959 |
Class<?> arrayElement = arrayType.getComponentType(); |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
960 |
if (arrayElement == null) |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
961 |
throw newIllegalArgumentException("not an array type", arrayType); |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
962 |
if ((arrayLength & 0x7F) != arrayLength) { |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
963 |
if ((arrayLength & 0xFF) != arrayLength) |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
964 |
throw newIllegalArgumentException("array length is not legal", arrayLength); |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
965 |
assert(arrayLength >= 128); |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
966 |
if (arrayElement == long.class || |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
967 |
arrayElement == double.class) |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
968 |
throw newIllegalArgumentException("array length is not legal for long[] or double[]", arrayLength); |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
969 |
} |
4537 | 970 |
} |
971 |
||
972 |
/** |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
973 |
* Makes an <em>array-collecting</em> method handle, which accepts a given number of trailing |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
974 |
* positional arguments and collects them into an array argument. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
975 |
* The new method handle adapts, as its <i>target</i>, |
4537 | 976 |
* the current method handle. The type of the adapter will be |
977 |
* the same as the type of the target, except that a single trailing |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
978 |
* parameter (usually of type {@code arrayType}) is replaced by |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
979 |
* {@code arrayLength} parameters whose type is element type of {@code arrayType}. |
4537 | 980 |
* <p> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
981 |
* If the array type differs from the final argument type on the original target, |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
982 |
* the original target is adapted to take the array type directly, |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
983 |
* as if by a call to {@link #asType asType}. |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
984 |
* <p> |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
985 |
* When called, the adapter replaces its trailing {@code arrayLength} |
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
986 |
* arguments by a single new array of type {@code arrayType}, whose elements |
4537 | 987 |
* comprise (in order) the replaced arguments. |
988 |
* Finally the target is called. |
|
989 |
* What the target eventually returns is returned unchanged by the adapter. |
|
990 |
* <p> |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
991 |
* (The array may also be a shared constant when {@code arrayLength} is zero.) |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
992 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
993 |
* (<em>Note:</em> The {@code arrayType} is often identical to the last |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
994 |
* parameter type of the original target. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
995 |
* It is an explicit argument for symmetry with {@code asSpreader}, and also |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
996 |
* to allow the target to use a simple {@code Object} as its last parameter type.) |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
997 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
998 |
* In order to create a collecting adapter which is not restricted to a particular |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
999 |
* number of collected arguments, use {@link #asVarargsCollector asVarargsCollector} instead. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1000 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1001 |
* Here are some examples of array-collecting method handles: |
18156 | 1002 |
* <blockquote><pre>{@code |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1003 |
MethodHandle deepToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1004 |
.findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1005 |
assertEquals("[won]", (String) deepToString.invokeExact(new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1006 |
MethodHandle ts1 = deepToString.asCollector(Object[].class, 1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1007 |
assertEquals(methodType(String.class, Object.class), ts1.type()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1008 |
//assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); //FAIL |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1009 |
assertEquals("[[won]]", (String) ts1.invokeExact((Object) new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1010 |
// arrayType can be a subtype of Object[] |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1011 |
MethodHandle ts2 = deepToString.asCollector(String[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1012 |
assertEquals(methodType(String.class, String.class, String.class), ts2.type()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1013 |
assertEquals("[two, too]", (String) ts2.invokeExact("two", "too")); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1014 |
MethodHandle ts0 = deepToString.asCollector(Object[].class, 0); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1015 |
assertEquals("[]", (String) ts0.invokeExact()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1016 |
// collectors can be nested, Lisp-style |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1017 |
MethodHandle ts22 = deepToString.asCollector(Object[].class, 3).asCollector(String[].class, 2); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1018 |
assertEquals("[A, B, [C, D]]", ((String) ts22.invokeExact((Object)'A', (Object)"B", "C", "D"))); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1019 |
// arrayType can be any primitive array type |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1020 |
MethodHandle bytesToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1021 |
.findStatic(Arrays.class, "toString", methodType(String.class, byte[].class)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1022 |
.asCollector(byte[].class, 3); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1023 |
assertEquals("[1, 2, 3]", (String) bytesToString.invokeExact((byte)1, (byte)2, (byte)3)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1024 |
MethodHandle longsToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1025 |
.findStatic(Arrays.class, "toString", methodType(String.class, long[].class)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1026 |
.asCollector(long[].class, 1); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1027 |
assertEquals("[123]", (String) longsToString.invokeExact((long)123)); |
18156 | 1028 |
* }</pre></blockquote> |
36112
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1029 |
* <p> |
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1030 |
* <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector |
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1031 |
* variable-arity method handle}, even if the original target method handle was. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1032 |
* @param arrayType often {@code Object[]}, the type of the array argument which will collect the arguments |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1033 |
* @param arrayLength the number of arguments to collect into a new array argument |
4537 | 1034 |
* @return a new method handle which collects some trailing argument |
1035 |
* into an array, before calling the original method handle |
|
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1036 |
* @throws NullPointerException if {@code arrayType} is a null reference |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1037 |
* @throws IllegalArgumentException if {@code arrayType} is not an array type |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1038 |
* or {@code arrayType} is not assignable to this method handle's trailing parameter type, |
20529
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
1039 |
* or {@code arrayLength} is not a legal array size, |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
1040 |
* or the resulting method handle's type would have |
b49b07206f7d
8019417: JSR 292 javadoc should clarify method handle arity limits
jrose
parents:
20527
diff
changeset
|
1041 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1042 |
* @throws WrongMethodTypeException if the implied {@code asType} call fails |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1043 |
* @see #asSpreader |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1044 |
* @see #asVarargsCollector |
4537 | 1045 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1046 |
public MethodHandle asCollector(Class<?> arrayType, int arrayLength) { |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1047 |
return asCollector(type().parameterCount() - 1, arrayType, arrayLength); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1048 |
} |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1049 |
|
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1050 |
/** |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1051 |
* Makes an <em>array-collecting</em> method handle, which accepts a given number of positional arguments starting |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1052 |
* at a given position, and collects them into an array argument. The new method handle adapts, as its |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1053 |
* <i>target</i>, the current method handle. The type of the adapter will be the same as the type of the target, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1054 |
* except that the parameter at the position indicated by {@code collectArgPos} (usually of type {@code arrayType}) |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1055 |
* is replaced by {@code arrayLength} parameters whose type is element type of {@code arrayType}. |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1056 |
* <p> |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1057 |
* This method behaves very much like {@link #asCollector(Class, int)}, but differs in that its {@code |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1058 |
* collectArgPos} argument indicates at which position in the parameter list arguments should be collected. This |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1059 |
* index is zero-based. |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1060 |
* <p> |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1061 |
* @apiNote Examples: |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1062 |
* <blockquote><pre>{@code |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1063 |
StringWriter swr = new StringWriter(); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1064 |
MethodHandle swWrite = LOOKUP.findVirtual(StringWriter.class, "write", methodType(void.class, char[].class, int.class, int.class)).bindTo(swr); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1065 |
MethodHandle swWrite4 = swWrite.asCollector(0, char[].class, 4); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1066 |
swWrite4.invoke('A', 'B', 'C', 'D', 1, 2); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1067 |
assertEquals("BC", swr.toString()); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1068 |
swWrite4.invoke('P', 'Q', 'R', 'S', 0, 4); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1069 |
assertEquals("BCPQRS", swr.toString()); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1070 |
swWrite4.invoke('W', 'X', 'Y', 'Z', 3, 1); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1071 |
assertEquals("BCPQRSZ", swr.toString()); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1072 |
* }</pre></blockquote> |
36112
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1073 |
* <p> |
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1074 |
* <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector |
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1075 |
* variable-arity method handle}, even if the original target method handle was. |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1076 |
* @param collectArgPos the zero-based position in the parameter list at which to start collecting. |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1077 |
* @param arrayType often {@code Object[]}, the type of the array argument which will collect the arguments |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1078 |
* @param arrayLength the number of arguments to collect into a new array argument |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1079 |
* @return a new method handle which collects some arguments |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1080 |
* into an array, before calling the original method handle |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1081 |
* @throws NullPointerException if {@code arrayType} is a null reference |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1082 |
* @throws IllegalArgumentException if {@code arrayType} is not an array type |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1083 |
* or {@code arrayType} is not assignable to this method handle's array parameter type, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1084 |
* or {@code arrayLength} is not a legal array size, |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1085 |
* or {@code collectArgPos} has an illegal value (negative, or greater than the number of arguments), |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1086 |
* or the resulting method handle's type would have |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1087 |
* <a href="MethodHandle.html#maxarity">too many parameters</a> |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1088 |
* @throws WrongMethodTypeException if the implied {@code asType} call fails |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1089 |
* |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1090 |
* @see #asCollector(Class, int) |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1091 |
* @since 9 |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1092 |
*/ |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1093 |
public MethodHandle asCollector(int collectArgPos, Class<?> arrayType, int arrayLength) { |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1094 |
asCollectorChecks(arrayType, collectArgPos, arrayLength); |
27755 | 1095 |
BoundMethodHandle mh = rebind(); |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1096 |
MethodType resultType = type().asCollectorType(arrayType, collectArgPos, arrayLength); |
27755 | 1097 |
MethodHandle newArray = MethodHandleImpl.varargsArray(arrayType, arrayLength); |
1098 |
LambdaForm lform = mh.editor().collectArgumentArrayForm(1 + collectArgPos, newArray); |
|
1099 |
if (lform != null) { |
|
1100 |
return mh.copyWith(resultType, lform); |
|
26482
cea1ab1c3ac7
8057922: Improve LambdaForm sharing by using LambdaFormEditor more extensively
vlivanov
parents:
26477
diff
changeset
|
1101 |
} |
27755 | 1102 |
lform = mh.editor().collectArgumentsForm(1 + collectArgPos, newArray.type().basicType()); |
1103 |
return mh.copyWithExtendL(resultType, lform, newArray); |
|
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1104 |
} |
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1105 |
|
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1106 |
/** |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1107 |
* See if {@code asCollector} can be validly called with the given arguments. |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1108 |
* Return false if the last parameter is not an exact match to arrayType. |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1109 |
*/ |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1110 |
/*non-public*/ boolean asCollectorChecks(Class<?> arrayType, int pos, int arrayLength) { |
9731
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
1111 |
spreadArrayChecks(arrayType, arrayLength); |
9646
5ebbe5ab084f
6939861: JVM should handle more conversion operations
jrose
parents:
9645
diff
changeset
|
1112 |
int nargs = type().parameterCount(); |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1113 |
if (pos < 0 || pos >= nargs) { |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1114 |
throw newIllegalArgumentException("bad collect position"); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1115 |
} |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1116 |
if (nargs != 0) { |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1117 |
Class<?> param = type().parameterType(pos); |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1118 |
if (param == arrayType) return true; |
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1119 |
if (param.isAssignableFrom(arrayType)) return false; |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1120 |
} |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1121 |
throw newIllegalArgumentException("array type not assignable to argument", this, arrayType); |
4537 | 1122 |
} |
1123 |
||
1124 |
/** |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1125 |
* Makes a <em>variable arity</em> adapter which is able to accept |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1126 |
* any number of trailing positional arguments and collect them |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1127 |
* into an array argument. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1128 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1129 |
* The type and behavior of the adapter will be the same as |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1130 |
* the type and behavior of the target, except that certain |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1131 |
* {@code invoke} and {@code asType} requests can lead to |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1132 |
* trailing positional arguments being collected into target's |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1133 |
* trailing parameter. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1134 |
* Also, the last parameter type of the adapter will be |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1135 |
* {@code arrayType}, even if the target has a different |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1136 |
* last parameter type. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1137 |
* <p> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1138 |
* This transformation may return {@code this} if the method handle is |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1139 |
* already of variable arity and its trailing parameter type |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1140 |
* is identical to {@code arrayType}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1141 |
* <p> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1142 |
* When called with {@link #invokeExact invokeExact}, the adapter invokes |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1143 |
* the target with no argument changes. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1144 |
* (<em>Note:</em> This behavior is different from a |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1145 |
* {@linkplain #asCollector fixed arity collector}, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1146 |
* since it accepts a whole array of indeterminate length, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1147 |
* rather than a fixed number of arguments.) |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1148 |
* <p> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1149 |
* When called with plain, inexact {@link #invoke invoke}, if the caller |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1150 |
* type is the same as the adapter, the adapter invokes the target as with |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1151 |
* {@code invokeExact}. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1152 |
* (This is the normal behavior for {@code invoke} when types match.) |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1153 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1154 |
* Otherwise, if the caller and adapter arity are the same, and the |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1155 |
* trailing parameter type of the caller is a reference type identical to |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1156 |
* or assignable to the trailing parameter type of the adapter, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1157 |
* the arguments and return values are converted pairwise, |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1158 |
* as if by {@link #asType asType} on a fixed arity |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1159 |
* method handle. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1160 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1161 |
* Otherwise, the arities differ, or the adapter's trailing parameter |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1162 |
* type is not assignable from the corresponding caller type. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1163 |
* In this case, the adapter replaces all trailing arguments from |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1164 |
* the original trailing argument position onward, by |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1165 |
* a new array of type {@code arrayType}, whose elements |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1166 |
* comprise (in order) the replaced arguments. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1167 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1168 |
* The caller type must provides as least enough arguments, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1169 |
* and of the correct type, to satisfy the target's requirement for |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1170 |
* positional arguments before the trailing array argument. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1171 |
* Thus, the caller must supply, at a minimum, {@code N-1} arguments, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1172 |
* where {@code N} is the arity of the target. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1173 |
* Also, there must exist conversions from the incoming arguments |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1174 |
* to the target's arguments. |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1175 |
* As with other uses of plain {@code invoke}, if these basic |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1176 |
* requirements are not fulfilled, a {@code WrongMethodTypeException} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1177 |
* may be thrown. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1178 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1179 |
* In all cases, what the target eventually returns is returned unchanged by the adapter. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1180 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1181 |
* In the final case, it is exactly as if the target method handle were |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1182 |
* temporarily adapted with a {@linkplain #asCollector fixed arity collector} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1183 |
* to the arity required by the caller type. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1184 |
* (As with {@code asCollector}, if the array length is zero, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1185 |
* a shared constant may be used instead of a new array. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1186 |
* If the implied call to {@code asCollector} would throw |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1187 |
* an {@code IllegalArgumentException} or {@code WrongMethodTypeException}, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1188 |
* the call to the variable arity adapter must throw |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1189 |
* {@code WrongMethodTypeException}.) |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1190 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1191 |
* The behavior of {@link #asType asType} is also specialized for |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1192 |
* variable arity adapters, to maintain the invariant that |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1193 |
* plain, inexact {@code invoke} is always equivalent to an {@code asType} |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1194 |
* call to adjust the target type, followed by {@code invokeExact}. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1195 |
* Therefore, a variable arity adapter responds |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1196 |
* to an {@code asType} request by building a fixed arity collector, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1197 |
* if and only if the adapter and requested type differ either |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1198 |
* in arity or trailing argument type. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1199 |
* The resulting fixed arity collector has its type further adjusted |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1200 |
* (if necessary) to the requested type by pairwise conversion, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1201 |
* as if by another application of {@code asType}. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1202 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1203 |
* When a method handle is obtained by executing an {@code ldc} instruction |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1204 |
* of a {@code CONSTANT_MethodHandle} constant, and the target method is marked |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1205 |
* as a variable arity method (with the modifier bit {@code 0x0080}), |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1206 |
* the method handle will accept multiple arities, as if the method handle |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1207 |
* constant were created by means of a call to {@code asVarargsCollector}. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1208 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1209 |
* In order to create a collecting adapter which collects a predetermined |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1210 |
* number of arguments, and whose type reflects this predetermined number, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1211 |
* use {@link #asCollector asCollector} instead. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1212 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1213 |
* No method handle transformations produce new method handles with |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1214 |
* variable arity, unless they are documented as doing so. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1215 |
* Therefore, besides {@code asVarargsCollector}, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1216 |
* all methods in {@code MethodHandle} and {@code MethodHandles} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1217 |
* will return a method handle with fixed arity, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1218 |
* except in the cases where they are specified to return their original |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1219 |
* operand (e.g., {@code asType} of the method handle's own type). |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1220 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1221 |
* Calling {@code asVarargsCollector} on a method handle which is already |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1222 |
* of variable arity will produce a method handle with the same type and behavior. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1223 |
* It may (or may not) return the original variable arity method handle. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1224 |
* <p> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1225 |
* Here is an example, of a list-making variable arity method handle: |
18156 | 1226 |
* <blockquote><pre>{@code |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1227 |
MethodHandle deepToString = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1228 |
.findStatic(Arrays.class, "deepToString", methodType(String.class, Object[].class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1229 |
MethodHandle ts1 = deepToString.asVarargsCollector(Object[].class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1230 |
assertEquals("[won]", (String) ts1.invokeExact( new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1231 |
assertEquals("[won]", (String) ts1.invoke( new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1232 |
assertEquals("[won]", (String) ts1.invoke( "won" )); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1233 |
assertEquals("[[won]]", (String) ts1.invoke((Object) new Object[]{"won"})); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1234 |
// findStatic of Arrays.asList(...) produces a variable arity method handle: |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1235 |
MethodHandle asList = publicLookup() |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1236 |
.findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1237 |
assertEquals(methodType(List.class, Object[].class), asList.type()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1238 |
assert(asList.isVarargsCollector()); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1239 |
assertEquals("[]", asList.invoke().toString()); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1240 |
assertEquals("[1]", asList.invoke(1).toString()); |
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1241 |
assertEquals("[two, too]", asList.invoke("two", "too").toString()); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1242 |
String[] argv = { "three", "thee", "tee" }; |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1243 |
assertEquals("[three, thee, tee]", asList.invoke(argv).toString()); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1244 |
assertEquals("[three, thee, tee]", asList.invoke((Object[])argv).toString()); |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1245 |
List ls = (List) asList.invoke((Object)argv); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1246 |
assertEquals(1, ls.size()); |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1247 |
assertEquals("[three, thee, tee]", Arrays.toString((Object[])ls.get(0))); |
18156 | 1248 |
* }</pre></blockquote> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1249 |
* <p style="font-size:smaller;"> |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1250 |
* <em>Discussion:</em> |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1251 |
* These rules are designed as a dynamically-typed variation |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1252 |
* of the Java rules for variable arity methods. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1253 |
* In both cases, callers to a variable arity method or method handle |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1254 |
* can either pass zero or more positional arguments, or else pass |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1255 |
* pre-collected arrays of any length. Users should be aware of the |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1256 |
* special role of the final argument, and of the effect of a |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1257 |
* type match on that final argument, which determines whether |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1258 |
* or not a single trailing argument is interpreted as a whole |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1259 |
* array or a single element of an array to be collected. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1260 |
* Note that the dynamic type of the trailing argument has no |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1261 |
* effect on this decision, only a comparison between the symbolic |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1262 |
* type descriptor of the call site and the type descriptor of the method handle.) |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1263 |
* |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1264 |
* @param arrayType often {@code Object[]}, the type of the array argument which will collect the arguments |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1265 |
* @return a new method handle which can collect any number of trailing arguments |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1266 |
* into an array, before calling the original method handle |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1267 |
* @throws NullPointerException if {@code arrayType} is a null reference |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1268 |
* @throws IllegalArgumentException if {@code arrayType} is not an array type |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1269 |
* or {@code arrayType} is not assignable to this method handle's trailing parameter type |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1270 |
* @see #asCollector |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1271 |
* @see #isVarargsCollector |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1272 |
* @see #asFixedArity |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1273 |
*/ |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1274 |
public MethodHandle asVarargsCollector(Class<?> arrayType) { |
29094
a4fd2b5e49f8
8073479: Replace obj.getClass hacks with Objects.requireNonNull
shade
parents:
29020
diff
changeset
|
1275 |
Objects.requireNonNull(arrayType); |
33874
46651fd30c0b
8139885: implement JEP 274: enhanced method handles
mhaupt
parents:
31671
diff
changeset
|
1276 |
boolean lastMatch = asCollectorChecks(arrayType, type().parameterCount() - 1, 0); |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1277 |
if (isVarargsCollector() && lastMatch) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1278 |
return this; |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1279 |
return MethodHandleImpl.makeVarargsCollector(this, arrayType); |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1280 |
} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1281 |
|
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1282 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1283 |
* Determines if this method handle |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1284 |
* supports {@linkplain #asVarargsCollector variable arity} calls. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1285 |
* Such method handles arise from the following sources: |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1286 |
* <ul> |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1287 |
* <li>a call to {@linkplain #asVarargsCollector asVarargsCollector} |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1288 |
* <li>a call to a {@linkplain java.lang.invoke.MethodHandles.Lookup lookup method} |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1289 |
* which resolves to a variable arity Java method or constructor |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1290 |
* <li>an {@code ldc} instruction of a {@code CONSTANT_MethodHandle} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1291 |
* which resolves to a variable arity Java method or constructor |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1292 |
* </ul> |
9645
dabb5e4edc4c
7034977: JSR 292 MethodHandle.invokeGeneric should be renamed MethodHandle.invoke
jrose
parents:
8822
diff
changeset
|
1293 |
* @return true if this method handle accepts more than one arity of plain, inexact {@code invoke} calls |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1294 |
* @see #asVarargsCollector |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1295 |
* @see #asFixedArity |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1296 |
*/ |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1297 |
public boolean isVarargsCollector() { |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1298 |
return false; |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1299 |
} |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1300 |
|
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1301 |
/** |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1302 |
* Makes a <em>fixed arity</em> method handle which is otherwise |
20877
83ea903d5cf1
8026183: minor documentation problems in java.lang.invoke
rriggs
parents:
20535
diff
changeset
|
1303 |
* equivalent to the current method handle. |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1304 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1305 |
* If the current method handle is not of |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1306 |
* {@linkplain #asVarargsCollector variable arity}, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1307 |
* the current method handle is returned. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1308 |
* This is true even if the current method handle |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1309 |
* could not be a valid input to {@code asVarargsCollector}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1310 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1311 |
* Otherwise, the resulting fixed-arity method handle has the same |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1312 |
* type and behavior of the current method handle, |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1313 |
* except that {@link #isVarargsCollector isVarargsCollector} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1314 |
* will be false. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1315 |
* The fixed-arity method handle may (or may not) be the |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1316 |
* a previous argument to {@code asVarargsCollector}. |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1317 |
* <p> |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1318 |
* Here is an example, of a list-making variable arity method handle: |
18156 | 1319 |
* <blockquote><pre>{@code |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1320 |
MethodHandle asListVar = publicLookup() |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1321 |
.findStatic(Arrays.class, "asList", methodType(List.class, Object[].class)) |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1322 |
.asVarargsCollector(Object[].class); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1323 |
MethodHandle asListFix = asListVar.asFixedArity(); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1324 |
assertEquals("[1]", asListVar.invoke(1).toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1325 |
Exception caught = null; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1326 |
try { asListFix.invoke((Object)1); } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1327 |
catch (Exception ex) { caught = ex; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1328 |
assert(caught instanceof ClassCastException); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1329 |
assertEquals("[two, too]", asListVar.invoke("two", "too").toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1330 |
try { asListFix.invoke("two", "too"); } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1331 |
catch (Exception ex) { caught = ex; } |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1332 |
assert(caught instanceof WrongMethodTypeException); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1333 |
Object[] argv = { "three", "thee", "tee" }; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1334 |
assertEquals("[three, thee, tee]", asListVar.invoke(argv).toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1335 |
assertEquals("[three, thee, tee]", asListFix.invoke(argv).toString()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1336 |
assertEquals(1, ((List) asListVar.invoke((Object)argv)).size()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1337 |
assertEquals("[three, thee, tee]", asListFix.invoke((Object)argv).toString()); |
18156 | 1338 |
* }</pre></blockquote> |
9752
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1339 |
* |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1340 |
* @return a new method handle which accepts only a fixed number of arguments |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1341 |
* @see #asVarargsCollector |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1342 |
* @see #isVarargsCollector |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1343 |
*/ |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1344 |
public MethodHandle asFixedArity() { |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1345 |
assert(!isVarargsCollector()); |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1346 |
return this; |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1347 |
} |
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1348 |
|
88ab34b6da6d
7032323: code changes for JSR 292 EG adjustments to API, through Public Review
jrose
parents:
9731
diff
changeset
|
1349 |
/** |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1350 |
* Binds a value {@code x} to the first argument of a method handle, without invoking it. |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1351 |
* The new method handle adapts, as its <i>target</i>, |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1352 |
* the current method handle by binding it to the given argument. |
4537 | 1353 |
* The type of the bound handle will be |
1354 |
* the same as the type of the target, except that a single leading |
|
1355 |
* reference parameter will be omitted. |
|
1356 |
* <p> |
|
1357 |
* When called, the bound handle inserts the given value {@code x} |
|
1358 |
* as a new leading argument to the target. The other arguments are |
|
1359 |
* also passed unchanged. |
|
1360 |
* What the target eventually returns is returned unchanged by the bound handle. |
|
1361 |
* <p> |
|
1362 |
* The reference {@code x} must be convertible to the first parameter |
|
1363 |
* type of the target. |
|
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1364 |
* <p> |
36112
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1365 |
* <em>Note:</em> Because method handles are immutable, the target method handle |
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1366 |
* retains its original type and behavior. |
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1367 |
* <p> |
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1368 |
* <em>Note:</em> The resulting adapter is never a {@linkplain MethodHandle#asVarargsCollector |
e71e97060bf5
8150360: augment/correct MethodHandle API documentation
mhaupt
parents:
34720
diff
changeset
|
1369 |
* variable-arity method handle}, even if the original target method handle was. |
4537 | 1370 |
* @param x the value to bind to the first argument of the target |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1371 |
* @return a new method handle which prepends the given value to the incoming |
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1372 |
* argument list, before calling the original method handle |
4537 | 1373 |
* @throws IllegalArgumentException if the target does not have a |
1374 |
* leading parameter type that is a reference type |
|
1375 |
* @throws ClassCastException if {@code x} cannot be converted |
|
1376 |
* to the leading parameter type of the target |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1377 |
* @see MethodHandles#insertArguments |
4537 | 1378 |
*/ |
8346
3b891698c4ec
7012650: implement JSR 292 EG adjustments through January 2010
jrose
parents:
8345
diff
changeset
|
1379 |
public MethodHandle bindTo(Object x) { |
26469
e6bc14fae1cf
8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents:
26468
diff
changeset
|
1380 |
x = type.leadingReferenceParameter().cast(x); // throw CCE if needed |
e6bc14fae1cf
8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents:
26468
diff
changeset
|
1381 |
return bindArgumentL(0, x); |
4537 | 1382 |
} |
7051
1c545d70a157
6964498: JSR 292 invokedynamic sites need local bootstrap methods
jrose
parents:
5725
diff
changeset
|
1383 |
|
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1384 |
/** |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1385 |
* Returns a string representation of the method handle, |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1386 |
* starting with the string {@code "MethodHandle"} and |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1387 |
* ending with the string representation of the method handle's type. |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1388 |
* In other words, this method returns a string equal to the value of: |
20535
cc85c8626435
8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents:
20534
diff
changeset
|
1389 |
* <blockquote><pre>{@code |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1390 |
* "MethodHandle" + type().toString() |
20535
cc85c8626435
8024438: JSR 292 API specification maintenance for JDK 8
jrose
parents:
20534
diff
changeset
|
1391 |
* }</pre></blockquote> |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1392 |
* <p> |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1393 |
* (<em>Note:</em> Future releases of this API may add further information |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1394 |
* to the string representation. |
8822
8145ab9f5f86
7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents:
8821
diff
changeset
|
1395 |
* Therefore, the present syntax should not be parsed by applications.) |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1396 |
* |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1397 |
* @return a string representation of the method handle |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1398 |
*/ |
8345
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1399 |
@Override |
9e2483e6cfab
7013417: JSR 292 needs to support variadic method handle calls
jrose
parents:
7562
diff
changeset
|
1400 |
public String toString() { |
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1401 |
if (DEBUG_METHOD_HANDLE_NAMES) return "MethodHandle"+debugString(); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1402 |
return standardString(); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1403 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1404 |
String standardString() { |
9731
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
1405 |
return "MethodHandle"+type; |
d0f7a3e441c4
7044892: JSR 292: API entry points sometimes throw the wrong exceptions or doesn't throw the expected one
jrose
parents:
9730
diff
changeset
|
1406 |
} |
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1407 |
/** Return a string with a several lines describing the method handle structure. |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1408 |
* This string would be suitable for display in an IDE debugger. |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1409 |
*/ |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1410 |
String debugString() { |
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1411 |
return type+" : "+internalForm()+internalProperties(); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1412 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1413 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1414 |
//// Implementation methods. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1415 |
//// Sub-classes can override these default implementations. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1416 |
//// All these methods assume arguments are already validated. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1417 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1418 |
// Other transforms to do: convert, explicitCast, permute, drop, filter, fold, GWT, catch |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1419 |
|
26469
e6bc14fae1cf
8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents:
26468
diff
changeset
|
1420 |
BoundMethodHandle bindArgumentL(int pos, Object value) { |
e6bc14fae1cf
8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents:
26468
diff
changeset
|
1421 |
return rebind().bindArgumentL(pos, value); |
e6bc14fae1cf
8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents:
26468
diff
changeset
|
1422 |
} |
e6bc14fae1cf
8050166: Get rid of some package-private methods on arguments in j.l.i.MethodHandle
vlivanov
parents:
26468
diff
changeset
|
1423 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1424 |
/*non-public*/ |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1425 |
MethodHandle setVarargs(MemberName member) throws IllegalAccessException { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1426 |
if (!member.isVarargs()) return this; |
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1427 |
Class<?> arrayType = type().lastParameterType(); |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1428 |
if (arrayType.isArray()) { |
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1429 |
return MethodHandleImpl.makeVarargsCollector(this, arrayType); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1430 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1431 |
throw member.makeAccessException("cannot make variable arity", null); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1432 |
} |
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1433 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1434 |
/*non-public*/ |
26470
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1435 |
MethodHandle viewAsType(MethodType newType, boolean strict) { |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1436 |
// No actual conversions, just a new view of the same method. |
26470
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1437 |
// Note that this operation must not produce a DirectMethodHandle, |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1438 |
// because retyped DMHs, like any transformed MHs, |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1439 |
// cannot be cracked into MethodHandleInfo. |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1440 |
assert viewAsTypeChecks(newType, strict); |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1441 |
BoundMethodHandle mh = rebind(); |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1442 |
return mh.copyWith(newType, mh.form); |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1443 |
} |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1444 |
|
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1445 |
/*non-public*/ |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1446 |
boolean viewAsTypeChecks(MethodType newType, boolean strict) { |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1447 |
if (strict) { |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1448 |
assert(type().isViewableAs(newType, true)) |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1449 |
: Arrays.asList(this, newType); |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1450 |
} else { |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1451 |
assert(type().basicType().isViewableAs(newType.basicType(), true)) |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1452 |
: Arrays.asList(this, newType); |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1453 |
} |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1454 |
return true; |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1455 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1456 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1457 |
// Decoding |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1458 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1459 |
/*non-public*/ |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1460 |
LambdaForm internalForm() { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1461 |
return form; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1462 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1463 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1464 |
/*non-public*/ |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1465 |
MemberName internalMemberName() { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1466 |
return null; // DMH returns DMH.member |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1467 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1468 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1469 |
/*non-public*/ |
20534
da86f7904e6d
8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents:
20530
diff
changeset
|
1470 |
Class<?> internalCallerClass() { |
da86f7904e6d
8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents:
20530
diff
changeset
|
1471 |
return null; // caller-bound MH for @CallerSensitive method returns caller |
da86f7904e6d
8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents:
20530
diff
changeset
|
1472 |
} |
da86f7904e6d
8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents:
20530
diff
changeset
|
1473 |
|
da86f7904e6d
8025112: JSR 292 spec updates for security manager and caller sensitivity
jrose
parents:
20530
diff
changeset
|
1474 |
/*non-public*/ |
26473
0abc9399a1a6
8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents:
26472
diff
changeset
|
1475 |
MethodHandleImpl.Intrinsic intrinsicName() { |
0abc9399a1a6
8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents:
26472
diff
changeset
|
1476 |
// no special intrinsic meaning to most MHs |
0abc9399a1a6
8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents:
26472
diff
changeset
|
1477 |
return MethodHandleImpl.Intrinsic.NONE; |
0abc9399a1a6
8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents:
26472
diff
changeset
|
1478 |
} |
0abc9399a1a6
8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents:
26472
diff
changeset
|
1479 |
|
0abc9399a1a6
8050200: Make LambdaForm intrinsics detection more robust
vlivanov
parents:
26472
diff
changeset
|
1480 |
/*non-public*/ |
26471
1de6be0c6945
8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents:
26470
diff
changeset
|
1481 |
MethodHandle withInternalMemberName(MemberName member, boolean isInvokeSpecial) { |
19817
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1482 |
if (member != null) { |
26471
1de6be0c6945
8050174: Support overriding of isInvokeSpecial flag in WrappedMember
vlivanov
parents:
26470
diff
changeset
|
1483 |
return MethodHandleImpl.makeWrappedMember(this, member, isInvokeSpecial); |
19817
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1484 |
} else if (internalMemberName() == null) { |
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1485 |
// The required internaMemberName is null, and this MH (like most) doesn't have one. |
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1486 |
return this; |
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1487 |
} else { |
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1488 |
// The following case is rare. Mask the internalMemberName by wrapping the MH in a BMH. |
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1489 |
MethodHandle result = rebind(); |
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1490 |
assert (result.internalMemberName() == null); |
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1491 |
return result; |
72338a6a1a4b
8024260: 10 closed/java/lang/invoke/* tests failing after overhaul to MethodHandleInfo
rfield
parents:
19804
diff
changeset
|
1492 |
} |
19804 | 1493 |
} |
1494 |
||
1495 |
/*non-public*/ |
|
16030
265a0d86c9ff
7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents:
14342
diff
changeset
|
1496 |
boolean isInvokeSpecial() { |
265a0d86c9ff
7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents:
14342
diff
changeset
|
1497 |
return false; // DMH.Special returns true |
265a0d86c9ff
7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents:
14342
diff
changeset
|
1498 |
} |
265a0d86c9ff
7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents:
14342
diff
changeset
|
1499 |
|
265a0d86c9ff
7087570: java.lang.invoke.MemberName information wrong for method handles created with findConstructor
kmo
parents:
14342
diff
changeset
|
1500 |
/*non-public*/ |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1501 |
Object internalValues() { |
13610
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
1502 |
return null; |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
1503 |
} |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
1504 |
|
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
1505 |
/*non-public*/ |
28122b96858e
7191102: nightly failures after JSR 292 lazy method handle update (round 3)
jrose
parents:
13423
diff
changeset
|
1506 |
Object internalProperties() { |
26467
d69abed3a07d
8050052: Small cleanups in java.lang.invoke code
vlivanov
parents:
26466
diff
changeset
|
1507 |
// Override to something to follow this.form, like "\n& FOO=bar" |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1508 |
return ""; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1509 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1510 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1511 |
//// Method handle implementation methods. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1512 |
//// Sub-classes can override these default implementations. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1513 |
//// All these methods assume arguments are already validated. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1514 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1515 |
/*non-public*/ |
26470
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1516 |
abstract MethodHandle copyWith(MethodType mt, LambdaForm lf); |
1586df597397
8050173: Add j.l.i.MethodHandle.copyWith(MethodType, LambdaForm)
vlivanov
parents:
26469
diff
changeset
|
1517 |
|
26472
71b6a6f208c0
8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents:
26471
diff
changeset
|
1518 |
/** Require this method handle to be a BMH, or else replace it with a "wrapper" BMH. |
71b6a6f208c0
8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents:
26471
diff
changeset
|
1519 |
* Many transforms are implemented only for BMHs. |
71b6a6f208c0
8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents:
26471
diff
changeset
|
1520 |
* @return a behaviorally equivalent BMH |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1521 |
*/ |
26472
71b6a6f208c0
8050057: Improve caching of MethodHandle reinvokers
vlivanov
parents:
26471
diff
changeset
|
1522 |
abstract BoundMethodHandle rebind(); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1523 |
|
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1524 |
/** |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1525 |
* Replace the old lambda form of this method handle with a new one. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1526 |
* The new one must be functionally equivalent to the old one. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1527 |
* Threads may continue running the old form indefinitely, |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1528 |
* but it is likely that the new one will be preferred for new executions. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1529 |
* Use with discretion. |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1530 |
*/ |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1531 |
/*non-public*/ |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1532 |
void updateForm(LambdaForm newForm) { |
29020
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1533 |
assert(newForm.customized == null || newForm.customized == this); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1534 |
if (form == newForm) return; |
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26482
diff
changeset
|
1535 |
newForm.prepare(); // as in MethodHandle.<init> |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1536 |
UNSAFE.putObject(this, FORM_OFFSET, newForm); |
27295
8ed31033bf48
8059877: GWT branch frequencies pollution due to LF sharing
vlivanov
parents:
26482
diff
changeset
|
1537 |
UNSAFE.fullFence(); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1538 |
} |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1539 |
|
29020
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1540 |
/** Craft a LambdaForm customized for this particular MethodHandle */ |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1541 |
/*non-public*/ |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1542 |
void customize() { |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1543 |
if (form.customized == null) { |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1544 |
LambdaForm newForm = form.customize(this); |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1545 |
updateForm(newForm); |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1546 |
} else { |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1547 |
assert(form.customized == this); |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1548 |
} |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1549 |
} |
9f6d43586ccb
8069591: Customize LambdaForms which are invoked using MH.invoke/invokeExact
vlivanov
parents:
27755
diff
changeset
|
1550 |
|
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1551 |
private static final long FORM_OFFSET; |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1552 |
static { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1553 |
try { |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1554 |
FORM_OFFSET = UNSAFE.objectFieldOffset(MethodHandle.class.getDeclaredField("form")); |
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1555 |
} catch (ReflectiveOperationException ex) { |
14089
0a41b980d62a
8000989: smaller code changes to make future JSR 292 backports easier
twisti
parents:
13610
diff
changeset
|
1556 |
throw newInternalError(ex); |
13423
17843fff200d
7023639: JSR 292 method handle invocation needs a fast path for compiled code
twisti
parents:
11513
diff
changeset
|
1557 |
} |
7555
a279ebc3b25c
6981777: implement JSR 292 EG adjustments from summer 2010
jrose
parents:
7554
diff
changeset
|
1558 |
} |
2707
5a17df307cbc
6829144: JSR 292 JVM features need a provisional Java API
jrose
parents:
diff
changeset
|
1559 |
} |