author | psandoz |
Wed, 11 Apr 2018 11:11:13 -0700 | |
changeset 49576 | 535498e7602f |
parent 48826 | c4d9d1b08e2e |
permissions | -rw-r--r-- |
48826 | 1 |
/* |
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48826 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
26 |
* @bug 8186046 8199875 |
48826 | 27 |
* @summary Test basic invocation of bootstrap methods |
28 |
* @library /lib/testlibrary/bytecode /java/lang/invoke/common |
|
29 |
* @build jdk.experimental.bytecode.BasicClassBuilder test.java.lang.invoke.lib.InstructionHelper |
|
30 |
* @run testng CondyBSMInvocation |
|
31 |
* @run testng/othervm -XX:+UnlockDiagnosticVMOptions -XX:UseBootstrapCallInfo=3 CondyBSMInvocation |
|
32 |
*/ |
|
33 |
||
34 |
||
35 |
import org.testng.Assert; |
|
36 |
import org.testng.annotations.Test; |
|
37 |
import test.java.lang.invoke.lib.InstructionHelper; |
|
38 |
||
39 |
import java.lang.invoke.MethodHandle; |
|
40 |
import java.lang.invoke.MethodHandles; |
|
41 |
import java.lang.invoke.MethodType; |
|
42 |
import java.lang.invoke.WrongMethodTypeException; |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
43 |
import java.util.Arrays; |
48826 | 44 |
import java.util.Collections; |
45 |
import java.util.stream.IntStream; |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
46 |
import java.util.stream.Stream; |
48826 | 47 |
|
48 |
import static java.lang.invoke.MethodType.methodType; |
|
49 |
||
50 |
public class CondyBSMInvocation { |
|
51 |
static final MethodHandles.Lookup L = MethodHandles.lookup(); |
|
52 |
||
53 |
||
54 |
@Test |
|
55 |
public void testNonexistent() throws Throwable { |
|
56 |
MethodHandle mh = InstructionHelper.ldcDynamicConstant( |
|
57 |
L, "name", Object.class, |
|
58 |
"bsm", methodType(Object.class), |
|
59 |
S -> {}); |
|
60 |
||
61 |
try { |
|
62 |
mh.invoke(); |
|
63 |
Assert.fail("NoSuchMethodError expected to be thrown"); |
|
64 |
} catch (NoSuchMethodError e) { |
|
65 |
} |
|
66 |
} |
|
67 |
||
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
68 |
static MethodHandle[] bsms(String bsmName) { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
69 |
return Stream.of(CondyBSMInvocation.class.getDeclaredMethods()). |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
70 |
filter(m -> m.getName().equals(bsmName)). |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
71 |
map(m -> { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
72 |
try { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
73 |
return MethodHandles.lookup().unreflect(m); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
74 |
} catch (IllegalAccessException e) { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
75 |
throw new RuntimeException(); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
76 |
} |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
77 |
}).toArray(MethodHandle[]::new); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
78 |
} |
48826 | 79 |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
80 |
public static Object shape_bsm() { |
48826 | 81 |
return "0"; |
82 |
} |
|
83 |
||
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
84 |
public static Object shape_bsm(Object a1) { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
85 |
return "0"; |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
86 |
} |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
87 |
|
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
88 |
public static Object shape_bsm(Object... args) { |
48826 | 89 |
return "0"; |
90 |
} |
|
91 |
||
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
92 |
public static Object shape_bsm(Object a1, Object a2) { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
93 |
return "0"; |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
94 |
} |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
95 |
|
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
96 |
public static Object shape_bsm(Object a1, Object... args) { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
97 |
return "0"; |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
98 |
} |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
99 |
|
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
100 |
public static Object shape_bsm(Object a1, Object a2, Object a3) { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
101 |
return "0"; |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
102 |
} |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
103 |
|
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
104 |
public static Object shape_bsm(MethodHandles.Lookup a1) { |
48826 | 105 |
return "0"; |
106 |
} |
|
107 |
||
108 |
@Test |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
109 |
public void testWrongShape() throws Throwable { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
110 |
for (MethodHandle bsm : bsms("shape_bsm")) { |
48826 | 111 |
MethodHandle mh = InstructionHelper.ldcDynamicConstant( |
112 |
L, "name", Object.class, |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
113 |
"shape_bsm", bsm.type(), |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
114 |
S -> {} |
48826 | 115 |
); |
116 |
||
117 |
try { |
|
118 |
Object r = mh.invoke(); |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
119 |
Assert.fail("BootstrapMethodError expected to be thrown for " + bsm); |
48826 | 120 |
} catch (BootstrapMethodError e) { |
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
||
125 |
||
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
126 |
public static Object sig_bsm(MethodHandles.Lookup a1, String[] a2) { |
48826 | 127 |
return "0"; |
128 |
} |
|
129 |
||
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
130 |
public static Object sig_bsm(MethodHandles.Lookup a1, String a2, String a3) { |
48826 | 131 |
return "0"; |
132 |
} |
|
133 |
||
134 |
@Test |
|
135 |
public void testWrongSignature() throws Throwable { |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
136 |
for (MethodHandle bsm : bsms("sig_bsm")) { |
48826 | 137 |
MethodHandle mh = InstructionHelper.ldcDynamicConstant( |
138 |
L, "name", Object.class, |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
139 |
"sig_bsm", bsm.type(), |
48826 | 140 |
S -> {} |
141 |
); |
|
142 |
||
143 |
try { |
|
144 |
Object r = mh.invoke(); |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
145 |
Assert.fail("BootstrapMethodError expected to be thrown for " + bsm); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
146 |
} catch (BootstrapMethodError e) { |
48826 | 147 |
} |
148 |
} |
|
149 |
} |
|
150 |
||
151 |
||
152 |
public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type) { |
|
153 |
return "0"; |
|
154 |
} |
|
155 |
||
156 |
public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, |
|
157 |
Object a1) { |
|
158 |
assertAll(a1); |
|
159 |
return "1"; |
|
160 |
} |
|
161 |
||
162 |
public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, |
|
163 |
Object a1, Object a2) { |
|
164 |
assertAll(a1, a2); |
|
165 |
return "2"; |
|
166 |
} |
|
167 |
||
168 |
public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, |
|
169 |
Object a1, Object a2, Object a3) { |
|
170 |
assertAll(a1, a2, a3); |
|
171 |
return "3"; |
|
172 |
} |
|
173 |
||
174 |
public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, |
|
175 |
Object a1, Object a2, Object a3, Object a4) { |
|
176 |
assertAll(a1, a2, a3, a4); |
|
177 |
return "4"; |
|
178 |
} |
|
179 |
||
180 |
public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, |
|
181 |
Object a1, Object a2, Object a3, Object a4, Object a5) { |
|
182 |
assertAll(a1, a2, a3, a4, a5); |
|
183 |
return "5"; |
|
184 |
} |
|
185 |
||
186 |
public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, |
|
187 |
Object a1, Object a2, Object a3, Object a4, Object a5, Object a6) { |
|
188 |
assertAll(a1, a2, a3, a4, a5, a6); |
|
189 |
return "6"; |
|
190 |
} |
|
191 |
||
192 |
public static Object bsm(MethodHandles.Lookup l, String name, Class<?> type, |
|
193 |
Object a1, Object a2, Object a3, Object a4, Object a5, Object a6, Object a7) { |
|
194 |
assertAll(a1, a2, a3, a4, a5, a6, a7); |
|
195 |
return "7"; |
|
196 |
} |
|
197 |
||
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
198 |
public static Object bsm(MethodHandles.Lookup l, Object... args) { |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
199 |
Object[] staticArgs = Arrays.copyOfRange(args, 2, args.length); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
200 |
assertAll(staticArgs); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
201 |
return Integer.toString(staticArgs.length); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
202 |
} |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
203 |
|
48826 | 204 |
static void assertAll(Object... as) { |
205 |
for (int i = 0; i < as.length; i++) { |
|
206 |
Assert.assertEquals(as[i], i); |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
@Test |
|
211 |
public void testArity() throws Throwable { |
|
212 |
for (int i = 0; i < 8; i++) { |
|
213 |
final int n = i; |
|
214 |
MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class) |
|
215 |
.appendParameterTypes(Collections.nCopies(n, Object.class)); |
|
216 |
MethodHandle mh = InstructionHelper.ldcDynamicConstant( |
|
217 |
L, "name", Object.class, |
|
218 |
"bsm", mt, |
|
219 |
S -> IntStream.range(0, n).forEach(S::add) |
|
220 |
); |
|
221 |
||
222 |
Object r = mh.invoke(); |
|
223 |
Assert.assertEquals(r, Integer.toString(n)); |
|
224 |
} |
|
49576
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
225 |
|
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
226 |
{ |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
227 |
MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, Object[].class); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
228 |
MethodHandle mh = InstructionHelper.ldcDynamicConstant( |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
229 |
L, "name", Object.class, |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
230 |
"bsm", mt, |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
231 |
S -> IntStream.range(0, 9).forEach(S::add) |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
232 |
); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
233 |
|
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
234 |
Object r = mh.invoke(); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
235 |
Assert.assertEquals(r, Integer.toString(9)); |
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
236 |
|
535498e7602f
8199875: Require first parameter type of a condy bootstrap to be Lookup
psandoz
parents:
48826
diff
changeset
|
237 |
} |
48826 | 238 |
} |
239 |
||
240 |
@Test |
|
241 |
public void testWrongNumberOfStaticArguments() throws Throwable { |
|
242 |
for (int i = 1; i < 8; i++) { |
|
243 |
final int n = i; |
|
244 |
MethodType mt = methodType(Object.class, MethodHandles.Lookup.class, String.class, Class.class) |
|
245 |
.appendParameterTypes(Collections.nCopies(n, Object.class)); |
|
246 |
MethodHandle mh = InstructionHelper.ldcDynamicConstant( |
|
247 |
L, "name", Object.class, |
|
248 |
"bsm", mt, |
|
249 |
S -> IntStream.range(0, n - 1).forEach(S::add) |
|
250 |
); |
|
251 |
||
252 |
try { |
|
253 |
Object r = mh.invoke(); |
|
254 |
Assert.fail("BootstrapMethodError expected to be thrown for arrity " + n); |
|
255 |
} catch (BootstrapMethodError e) { |
|
256 |
Throwable t = e.getCause(); |
|
257 |
Assert.assertTrue(WrongMethodTypeException.class.isAssignableFrom(t.getClass())); |
|
258 |
} |
|
259 |
} |
|
260 |
} |
|
261 |
} |