53595
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
|
26 |
* @requires !vm.graal.enabled
|
|
27 |
* @modules java.base/jdk.internal.org.objectweb.asm
|
|
28 |
* java.base/jdk.internal.misc
|
|
29 |
* java.base/jdk.internal.vm.annotation
|
|
30 |
* @library /test/lib /
|
|
31 |
* @build sun.hotspot.WhiteBox
|
|
32 |
* @run driver ClassFileInstaller sun.hotspot.WhiteBox
|
|
33 |
* sun.hotspot.WhiteBox$WhiteBoxPermission
|
|
34 |
*
|
|
35 |
* @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
|
|
36 |
* -XX:+PrintCompilation -XX:+PrintInlining -XX:+TraceDependencies -verbose:class -XX:CompileCommand=quiet
|
|
37 |
* -XX:CompileCommand=compileonly,*::test -XX:CompileCommand=compileonly,*::m -XX:CompileCommand=dontinline,*::test
|
|
38 |
* -Xbatch -XX:+WhiteBoxAPI -Xmixed
|
|
39 |
* -XX:-TieredCompilation
|
|
40 |
* compiler.cha.StrengthReduceInterfaceCall
|
|
41 |
*
|
|
42 |
* @run main/othervm -Xbootclasspath/a:. -XX:+IgnoreUnrecognizedVMOptions -XX:+UnlockDiagnosticVMOptions
|
|
43 |
* -XX:+PrintCompilation -XX:+PrintInlining -XX:+TraceDependencies -verbose:class -XX:CompileCommand=quiet
|
|
44 |
* -XX:CompileCommand=compileonly,*::test -XX:CompileCommand=compileonly,*::m -XX:CompileCommand=dontinline,*::test
|
|
45 |
* -Xbatch -XX:+WhiteBoxAPI -Xmixed
|
|
46 |
* -XX:+TieredCompilation -XX:TieredStopAtLevel=1
|
|
47 |
* compiler.cha.StrengthReduceInterfaceCall
|
|
48 |
*/
|
|
49 |
package compiler.cha;
|
|
50 |
|
|
51 |
import jdk.internal.misc.Unsafe;
|
|
52 |
import jdk.internal.org.objectweb.asm.ClassWriter;
|
|
53 |
import jdk.internal.org.objectweb.asm.MethodVisitor;
|
|
54 |
import jdk.internal.vm.annotation.DontInline;
|
|
55 |
import sun.hotspot.WhiteBox;
|
|
56 |
|
|
57 |
import java.io.IOException;
|
|
58 |
import java.lang.annotation.Retention;
|
|
59 |
import java.lang.annotation.RetentionPolicy;
|
|
60 |
import java.lang.invoke.MethodHandle;
|
|
61 |
import java.lang.invoke.MethodHandles;
|
|
62 |
import java.lang.reflect.Method;
|
|
63 |
import java.util.HashMap;
|
|
64 |
import java.util.concurrent.Callable;
|
|
65 |
|
|
66 |
import static jdk.test.lib.Asserts.*;
|
|
67 |
import static jdk.internal.org.objectweb.asm.ClassWriter.*;
|
|
68 |
import static jdk.internal.org.objectweb.asm.Opcodes.*;
|
|
69 |
|
|
70 |
public class StrengthReduceInterfaceCall {
|
|
71 |
public static void main(String[] args) {
|
|
72 |
run(ObjectToString.class);
|
|
73 |
run(ObjectHashCode.class);
|
|
74 |
run(TwoLevelHierarchyLinear.class);
|
|
75 |
run(ThreeLevelHierarchyLinear.class);
|
|
76 |
run(ThreeLevelHierarchyAbstractVsDefault.class);
|
|
77 |
run(ThreeLevelDefaultHierarchy.class);
|
|
78 |
run(ThreeLevelDefaultHierarchy1.class);
|
|
79 |
}
|
|
80 |
|
|
81 |
public static class ObjectToString extends ATest<ObjectToString.I> {
|
|
82 |
public ObjectToString() { super(I.class, C.class); }
|
|
83 |
|
|
84 |
interface J { String toString(); }
|
|
85 |
interface I extends J {}
|
|
86 |
|
|
87 |
static class C implements I {}
|
|
88 |
|
|
89 |
interface K1 extends I {}
|
|
90 |
interface K2 extends I { String toString(); } // K2.tS() ABSTRACT
|
|
91 |
// interface K3 extends I { default String toString() { return "K3"; } // K2.tS() DEFAULT
|
|
92 |
|
|
93 |
static class D implements I { public String toString() { return "D"; }}
|
|
94 |
|
|
95 |
static class DJ1 implements J {}
|
|
96 |
static class DJ2 implements J { public String toString() { return "DJ2"; }}
|
|
97 |
|
|
98 |
@Override
|
|
99 |
public Object test(I i) { return ObjectToStringHelper.test(i); /* invokeinterface I.toString() */ }
|
|
100 |
|
|
101 |
@TestCase
|
|
102 |
public void testMono() {
|
|
103 |
// 0. Trigger compilation of a monomorphic call site
|
|
104 |
compile(monomophic()); // C1 <: C <: intf I <: intf J <: Object.toString()
|
|
105 |
assertCompiled();
|
|
106 |
|
|
107 |
// Dependency: none
|
|
108 |
|
|
109 |
call(new C() { public String toString() { return "Cn"; }}); // Cn.tS <: C.tS <: intf I
|
|
110 |
assertCompiled();
|
|
111 |
}
|
|
112 |
|
|
113 |
@TestCase
|
|
114 |
public void testBi() {
|
|
115 |
// 0. Trigger compilation of a bimorphic call site
|
|
116 |
compile(bimorphic()); // C1 <: C <: intf I <: intf J <: Object.toString()
|
|
117 |
assertCompiled();
|
|
118 |
|
|
119 |
// Dependency: none
|
|
120 |
|
|
121 |
call(new C() { public String toString() { return "Cn"; }}); // Cn.tS <: C.tS <: intf I
|
|
122 |
assertCompiled();
|
|
123 |
}
|
|
124 |
|
|
125 |
@TestCase
|
|
126 |
public void testMega() {
|
|
127 |
// 0. Trigger compilation of a megamorphic call site
|
|
128 |
compile(megamorphic()); // C1,C2,C3 <: C <: intf I <: intf J <: Object.toString()
|
|
129 |
assertCompiled();
|
|
130 |
|
|
131 |
// Dependency: none
|
|
132 |
// compiler.cha.StrengthReduceInterfaceCall$ObjectToString::test (5 bytes)
|
|
133 |
// @ 1 compiler.cha.StrengthReduceInterfaceCall$ObjectToStringHelper::test (7 bytes) inline (hot)
|
|
134 |
// @ 1 java.lang.Object::toString (36 bytes) virtual call
|
|
135 |
|
|
136 |
// No dependency - no invalidation
|
|
137 |
repeat(100, () -> call(new C(){})); // Cn <: C <: intf I
|
|
138 |
assertCompiled();
|
|
139 |
|
|
140 |
initialize(K1.class, // intf K1 <: intf I <: intf J
|
|
141 |
K2.class, // intf K2.tS ABSTRACT <: intf I <: intf J
|
|
142 |
DJ1.class, // DJ1 <: intf J
|
|
143 |
DJ2.class); // DJ2.tS <: intf J
|
|
144 |
assertCompiled();
|
|
145 |
|
|
146 |
initialize(D.class); // D.tS <: intf I <: intf J
|
|
147 |
assertCompiled();
|
|
148 |
|
|
149 |
call(new C() { public String toString() { return "Cn"; }}); // Cn.tS <: C.tS <: intf I
|
|
150 |
assertCompiled();
|
|
151 |
}
|
|
152 |
|
|
153 |
@Override
|
|
154 |
public void checkInvalidReceiver() {
|
|
155 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
156 |
I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
|
|
157 |
test(o);
|
|
158 |
});
|
|
159 |
assertCompiled();
|
|
160 |
|
|
161 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
162 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J() {}); // super interface
|
|
163 |
test(j);
|
|
164 |
});
|
|
165 |
assertCompiled();
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
|
169 |
public static class ObjectHashCode extends ATest<ObjectHashCode.I> {
|
|
170 |
public ObjectHashCode() { super(I.class, C.class); }
|
|
171 |
|
|
172 |
interface J {}
|
|
173 |
interface I extends J {}
|
|
174 |
|
|
175 |
static class C implements I {}
|
|
176 |
|
|
177 |
interface K1 extends I {}
|
|
178 |
interface K2 extends I { int hashCode(); } // K2.hC() ABSTRACT
|
|
179 |
// interface K3 extends I { default int hashCode() { return CORRECT; } // K2.hC() DEFAULT
|
|
180 |
|
|
181 |
static class D implements I { public int hashCode() { return super.hashCode(); }}
|
|
182 |
|
|
183 |
static class DJ1 implements J {}
|
|
184 |
static class DJ2 implements J { public int hashCode() { return super.hashCode(); }}
|
|
185 |
|
|
186 |
@Override
|
|
187 |
public Object test(I i) {
|
|
188 |
return ObjectHashCodeHelper.test(i); /* invokeinterface I.hashCode() */
|
|
189 |
}
|
|
190 |
|
|
191 |
@TestCase
|
|
192 |
public void testMono() {
|
|
193 |
// 0. Trigger compilation of a monomorphic call site
|
|
194 |
compile(monomophic()); // C1 <: C <: intf I <: intf J <: Object.hashCode()
|
|
195 |
assertCompiled();
|
|
196 |
|
|
197 |
// Dependency: none
|
|
198 |
|
|
199 |
call(new C() { public int hashCode() { return super.hashCode(); }}); // Cn.hC <: C.hC <: intf I
|
|
200 |
assertCompiled();
|
|
201 |
}
|
|
202 |
|
|
203 |
@TestCase
|
|
204 |
public void testBi() {
|
|
205 |
// 0. Trigger compilation of a bimorphic call site
|
|
206 |
compile(bimorphic()); // C1 <: C <: intf I <: intf J <: Object.toString()
|
|
207 |
assertCompiled();
|
|
208 |
|
|
209 |
// Dependency: none
|
|
210 |
|
|
211 |
call(new C() { public int hashCode() { return super.hashCode(); }}); // Cn.hC <: C.hC <: intf I
|
|
212 |
assertCompiled();
|
|
213 |
}
|
|
214 |
|
|
215 |
@TestCase
|
|
216 |
public void testMega() {
|
|
217 |
// 0. Trigger compilation of a megamorphic call site
|
|
218 |
compile(megamorphic()); // C1,C2,C3 <: C <: intf I <: intf J <: Object.hashCode()
|
|
219 |
assertCompiled();
|
|
220 |
|
|
221 |
// Dependency: none
|
|
222 |
|
|
223 |
// No dependency - no invalidation
|
|
224 |
repeat(100, () -> call(new C(){})); // Cn <: C <: intf I
|
|
225 |
assertCompiled();
|
|
226 |
|
|
227 |
initialize(K1.class, // intf K1 <: intf I <: intf J
|
|
228 |
K2.class, // intf K2.hC ABSTRACT <: intf I <: intf J
|
|
229 |
DJ1.class, // DJ1 <: intf J
|
|
230 |
DJ2.class); // DJ2.hC <: intf J
|
|
231 |
assertCompiled();
|
|
232 |
|
|
233 |
initialize(D.class); // D.hC <: intf I <: intf J
|
|
234 |
assertCompiled();
|
|
235 |
|
|
236 |
call(new C() { public int hashCode() { return super.hashCode(); }}); // Cn.hC <: C.hC <: intf I
|
|
237 |
assertCompiled();
|
|
238 |
}
|
|
239 |
|
|
240 |
@Override
|
|
241 |
public void checkInvalidReceiver() {
|
|
242 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
243 |
I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
|
|
244 |
test(o);
|
|
245 |
});
|
|
246 |
assertCompiled();
|
|
247 |
|
|
248 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
249 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J() {}); // super interface
|
|
250 |
test(j);
|
|
251 |
});
|
|
252 |
assertCompiled();
|
|
253 |
}
|
|
254 |
}
|
|
255 |
|
|
256 |
public static class TwoLevelHierarchyLinear extends ATest<TwoLevelHierarchyLinear.I> {
|
|
257 |
public TwoLevelHierarchyLinear() { super(I.class, C.class); }
|
|
258 |
|
|
259 |
interface J { default Object m() { return WRONG; } }
|
|
260 |
|
|
261 |
interface I extends J { Object m(); }
|
|
262 |
static class C implements I { public Object m() { return CORRECT; }}
|
|
263 |
|
|
264 |
interface K1 extends I {}
|
|
265 |
interface K2 extends I { Object m(); }
|
|
266 |
interface K3 extends I { default Object m() { return WRONG; }}
|
|
267 |
|
|
268 |
static class D implements I { public Object m() { return WRONG; }}
|
|
269 |
|
|
270 |
static class DJ1 implements J {}
|
|
271 |
static class DJ2 implements J { public Object m() { return WRONG; }}
|
|
272 |
|
|
273 |
@DontInline
|
|
274 |
public Object test(I i) {
|
|
275 |
return i.m();
|
|
276 |
}
|
|
277 |
|
|
278 |
@TestCase
|
|
279 |
public void testMega1() {
|
|
280 |
// 0. Trigger compilation of a megamorphic call site
|
|
281 |
compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I.m ABSTRACT <: intf J.m ABSTRACT
|
|
282 |
assertCompiled();
|
|
283 |
|
|
284 |
// Dependency: type = unique_concrete_method, context = I, method = C.m
|
|
285 |
|
|
286 |
checkInvalidReceiver(); // ensure proper type check is preserved
|
|
287 |
|
|
288 |
// 1. No deoptimization/invalidation on not-yet-seen receiver
|
|
289 |
repeat(100, () -> call(new C(){})); // Cn <: C.m <: intf I.m ABSTRACT <: intf J.m DEFAULT
|
|
290 |
assertCompiled();
|
|
291 |
|
|
292 |
// 2. No dependency invalidation on class loading of unrelated classes: different context
|
|
293 |
initialize(K1.class, // intf K1 <: intf I.m ABSTRACT <: intf J.m DEFAULT
|
|
294 |
K2.class, // intf K2.m ABSTRACT <: intf I.m ABSTRACT <: intf J.m DEFAULT
|
|
295 |
DJ1.class, // DJ1 <: intf J.m DEFAULT
|
|
296 |
DJ2.class); // DJ2.m <: intf J.m DEFAULT
|
|
297 |
assertCompiled();
|
|
298 |
|
|
299 |
// 3. Dependency invalidation on D <: I
|
|
300 |
initialize(D.class); // D.m <: intf I.m ABSTRACT <: intf J.m DEFAULT
|
|
301 |
assertNotCompiled();
|
|
302 |
|
|
303 |
// 4. Recompilation: no inlining, no dependencies
|
|
304 |
compile(megamorphic());
|
|
305 |
call(new C() { public Object m() { return CORRECT; }}); // Cn.m <: C.m <: intf I.m ABSTRACT <: intf J.m DEFAULT
|
|
306 |
assertCompiled();
|
|
307 |
|
|
308 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
309 |
}
|
|
310 |
|
|
311 |
@TestCase
|
|
312 |
public void testMega2() {
|
|
313 |
// 0. Trigger compilation of a megamorphic call site
|
|
314 |
compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I.m ABSTRACT <: intf J.m DEFAULT
|
|
315 |
assertCompiled();
|
|
316 |
|
|
317 |
// Dependency: type = unique_concrete_method, context = I, method = C.m
|
|
318 |
|
|
319 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
320 |
|
|
321 |
// 1. Dependency invalidation
|
|
322 |
initialize(K3.class); // intf K3.m DEFAULT <: intf I.m ABSTRACT <: intf J.m DEFAULT
|
|
323 |
assertNotCompiled();
|
|
324 |
|
|
325 |
// 2. Recompilation: still inlines
|
|
326 |
// FIXME: no default method support in CHA yet
|
|
327 |
compile(megamorphic());
|
|
328 |
call(new K3() { public Object m() { return CORRECT; }}); // K3n.m <: intf K3.m DEFAULT <: intf I.m ABSTRACT <: intf J.m ABSTRACT
|
|
329 |
assertNotCompiled();
|
|
330 |
|
|
331 |
// 3. Recompilation: no inlining, no dependencies
|
|
332 |
compile(megamorphic());
|
|
333 |
call(new K3() { public Object m() { return CORRECT; }}); // Kn.m <: intf K3.m DEFAULT <: intf I.m ABSTRACT <: intf J.m DEFAULT
|
|
334 |
assertCompiled();
|
|
335 |
|
|
336 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
337 |
}
|
|
338 |
|
|
339 |
@Override
|
|
340 |
public void checkInvalidReceiver() {
|
|
341 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
342 |
I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
|
|
343 |
test(o);
|
|
344 |
});
|
|
345 |
assertCompiled();
|
|
346 |
|
|
347 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
348 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J() {}); // super interface
|
|
349 |
test(j);
|
|
350 |
});
|
|
351 |
assertCompiled();
|
|
352 |
}
|
|
353 |
}
|
|
354 |
|
|
355 |
public static class ThreeLevelHierarchyLinear extends ATest<ThreeLevelHierarchyLinear.I> {
|
|
356 |
public ThreeLevelHierarchyLinear() { super(I.class, C.class); }
|
|
357 |
|
|
358 |
interface J { Object m(); }
|
|
359 |
interface I extends J {}
|
|
360 |
|
|
361 |
interface K1 extends I {}
|
|
362 |
interface K2 extends I { Object m(); }
|
|
363 |
interface K3 extends I { default Object m() { return WRONG; }}
|
|
364 |
|
|
365 |
static class C implements I { public Object m() { return CORRECT; }}
|
|
366 |
|
|
367 |
static class DI implements I { public Object m() { return WRONG; }}
|
|
368 |
static class DJ implements J { public Object m() { return WRONG; }}
|
|
369 |
|
|
370 |
@DontInline
|
|
371 |
public Object test(I i) {
|
|
372 |
return i.m(); // I <: J.m ABSTRACT
|
|
373 |
}
|
|
374 |
|
|
375 |
@TestCase
|
|
376 |
public void testMega1() {
|
|
377 |
// 0. Trigger compilation of a megamorphic call site
|
|
378 |
compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I <: intf J.m ABSTRACT
|
|
379 |
assertCompiled();
|
|
380 |
|
|
381 |
// Dependency: type = unique_concrete_method, context = I, method = C.m
|
|
382 |
|
|
383 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
384 |
|
|
385 |
// 1. No deoptimization/invalidation on not-yet-seen receiver
|
|
386 |
repeat(100, () -> call(new C(){})); // Cn <: C.m <: intf I
|
|
387 |
assertCompiled(); // No deopt on not-yet-seen receiver
|
|
388 |
|
|
389 |
// 2. No dependency invalidation: different context
|
|
390 |
initialize(DJ.class, // DJ.m <: intf J.m ABSTRACT
|
|
391 |
K1.class, // intf K1 <: intf I <: intf J.m ABSTRACT
|
|
392 |
K2.class); // intf K2.m ABSTRACT <: intf I <: intf J.m ABSTRACT
|
|
393 |
assertCompiled();
|
|
394 |
|
|
395 |
// 3. Dependency invalidation: DI.m <: I
|
|
396 |
initialize(DI.class); // DI.m <: intf I <: intf J.m ABSTRACT
|
|
397 |
assertNotCompiled();
|
|
398 |
|
|
399 |
// 4. Recompilation w/o a dependency
|
|
400 |
compile(megamorphic());
|
|
401 |
call(new C() { public Object m() { return CORRECT; }}); // Cn.m <: C.m <: intf I <: intf J.m ABSTRACT
|
|
402 |
assertCompiled(); // no dependency
|
|
403 |
|
|
404 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
405 |
}
|
|
406 |
|
|
407 |
@TestCase
|
|
408 |
public void testMega2() {
|
|
409 |
compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I <: intf J.m ABSTRACT
|
|
410 |
assertCompiled();
|
|
411 |
|
|
412 |
// Dependency: type = unique_concrete_method, context = I, method = C.m
|
|
413 |
|
|
414 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
415 |
|
|
416 |
// Dependency invalidation
|
|
417 |
initialize(K3.class); // intf K3.m DEFAULT <: intf I;
|
|
418 |
assertNotCompiled(); // FIXME: default methods in sub-interfaces shouldn't be taken into account by CHA
|
|
419 |
|
|
420 |
// Recompilation with a dependency
|
|
421 |
compile(megamorphic());
|
|
422 |
assertCompiled();
|
|
423 |
|
|
424 |
// Dependency: type = unique_concrete_method, context = I, method = C.m
|
|
425 |
|
|
426 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
427 |
|
|
428 |
call(new K3() { public Object m() { return CORRECT; }}); // Kn.m <: K3.m DEFAULT <: intf I <: intf J.m ABSTRACT
|
|
429 |
assertNotCompiled();
|
|
430 |
|
|
431 |
// Recompilation w/o a dependency
|
|
432 |
compile(megamorphic());
|
|
433 |
// Dependency: none
|
|
434 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
435 |
call(new C() { public Object m() { return CORRECT; }}); // Cn.m <: C.m <: intf I <: intf J.m ABSTRACT
|
|
436 |
assertCompiled();
|
|
437 |
}
|
|
438 |
|
|
439 |
@Override
|
|
440 |
public void checkInvalidReceiver() {
|
|
441 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
442 |
I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
|
|
443 |
test(o);
|
|
444 |
});
|
|
445 |
assertCompiled();
|
|
446 |
|
|
447 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
448 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J() { public Object m() { return WRONG; }}); // super interface
|
|
449 |
test(j);
|
|
450 |
});
|
|
451 |
assertCompiled();
|
|
452 |
}
|
|
453 |
}
|
|
454 |
|
|
455 |
public static class ThreeLevelHierarchyAbstractVsDefault extends ATest<ThreeLevelHierarchyAbstractVsDefault.I> {
|
|
456 |
public ThreeLevelHierarchyAbstractVsDefault() { super(I.class, C.class); }
|
|
457 |
|
|
458 |
interface J1 { default Object m() { return WRONG; } } // intf J1.m DEFAULT
|
|
459 |
interface J2 extends J1 { Object m(); } // intf J2.m ABSTRACT <: intf J1
|
|
460 |
interface I extends J1, J2 {} // intf I.m OVERPASS <: intf J1,J2
|
|
461 |
|
|
462 |
static class C implements I { public Object m() { return CORRECT; }}
|
|
463 |
|
|
464 |
@DontInline
|
|
465 |
public Object test(I i) {
|
|
466 |
return i.m(); // intf I.m OVERPASS
|
|
467 |
}
|
|
468 |
|
|
469 |
static class DI implements I { public Object m() { return WRONG; }}
|
|
470 |
|
|
471 |
static class DJ11 implements J1 {}
|
|
472 |
static class DJ12 implements J1 { public Object m() { return WRONG; }}
|
|
473 |
|
|
474 |
static class DJ2 implements J2 { public Object m() { return WRONG; }}
|
|
475 |
|
|
476 |
interface K11 extends J1 {}
|
|
477 |
interface K12 extends J1 { Object m(); }
|
|
478 |
interface K13 extends J1 { default Object m() { return WRONG; }}
|
|
479 |
interface K21 extends J2 {}
|
|
480 |
interface K22 extends J2 { Object m(); }
|
|
481 |
interface K23 extends J2 { default Object m() { return WRONG; }}
|
|
482 |
|
|
483 |
|
|
484 |
public void testMega1() {
|
|
485 |
// 0. Trigger compilation of megamorphic call site
|
|
486 |
compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I.m OVERPASS <: intf J2.m ABSTRACT <: intf J1.m DEFAULT
|
|
487 |
assertCompiled();
|
|
488 |
|
|
489 |
// Dependency: type = unique_concrete_method, context = I, method = C.m
|
|
490 |
|
|
491 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
492 |
|
|
493 |
// 1. No deopt/invalidation on not-yet-seen receiver
|
|
494 |
repeat(100, () -> call(new C(){})); // Cn <: C.m <: intf I.m OVERPASS <: intf J2.m ABSTRACT <: intf J1.m DEFAULT
|
|
495 |
assertCompiled();
|
|
496 |
|
|
497 |
// 2. No dependency invalidation: different context
|
|
498 |
initialize(K11.class, K12.class, K13.class,
|
|
499 |
K21.class, K22.class, K23.class);
|
|
500 |
|
|
501 |
// 3. Dependency invalidation: Cn.m <: C <: I
|
|
502 |
call(new C() { public Object m() { return CORRECT; }}); // Cn.m <: C.m <: intf I.m OVERPASS <: intf J2.m ABSTRACT <: intf J1.m DEFAULT
|
|
503 |
assertNotCompiled();
|
|
504 |
|
|
505 |
// 4. Recompilation w/o a dependency
|
|
506 |
compile(megamorphic());
|
|
507 |
call(new C() { public Object m() { return CORRECT; }});
|
|
508 |
assertCompiled(); // no inlining
|
|
509 |
|
|
510 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
511 |
}
|
|
512 |
|
|
513 |
public void testMega2() {
|
|
514 |
// 0. Trigger compilation of a megamorphic call site
|
|
515 |
compile(megamorphic());
|
|
516 |
assertCompiled();
|
|
517 |
|
|
518 |
// Dependency: type = unique_concrete_method, context = I, method = C.m
|
|
519 |
|
|
520 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
521 |
|
|
522 |
// 1. No dependency invalidation: different context
|
|
523 |
initialize(DJ11.class,
|
|
524 |
DJ12.class,
|
|
525 |
DJ2.class);
|
|
526 |
assertCompiled();
|
|
527 |
|
|
528 |
// 2. Dependency invalidation: DI.m <: I
|
|
529 |
initialize(DI.class);
|
|
530 |
assertNotCompiled();
|
|
531 |
|
|
532 |
// 3. Recompilation w/o a dependency
|
|
533 |
compile(megamorphic());
|
|
534 |
call(new C() { public Object m() { return CORRECT; }});
|
|
535 |
assertCompiled(); // no inlining
|
|
536 |
|
|
537 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
538 |
}
|
|
539 |
|
|
540 |
@Override
|
|
541 |
public void checkInvalidReceiver() {
|
|
542 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
543 |
I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
|
|
544 |
test(o);
|
|
545 |
});
|
|
546 |
assertCompiled();
|
|
547 |
|
|
548 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
549 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J1() {}); // super interface
|
|
550 |
test(j);
|
|
551 |
});
|
|
552 |
assertCompiled();
|
|
553 |
|
|
554 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
555 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J2() { public Object m() { return WRONG; }}); // super interface
|
|
556 |
test(j);
|
|
557 |
});
|
|
558 |
assertCompiled();
|
|
559 |
}
|
|
560 |
}
|
|
561 |
|
|
562 |
public static class ThreeLevelDefaultHierarchy extends ATest<ThreeLevelDefaultHierarchy.I> {
|
|
563 |
public ThreeLevelDefaultHierarchy() { super(I.class, C.class); }
|
|
564 |
|
|
565 |
interface J { default Object m() { return WRONG; }}
|
|
566 |
interface I extends J {}
|
|
567 |
|
|
568 |
static class C implements I { public Object m() { return CORRECT; }}
|
|
569 |
|
|
570 |
interface K1 extends I {}
|
|
571 |
interface K2 extends I { Object m(); }
|
|
572 |
interface K3 extends I { default Object m() { return WRONG; }}
|
|
573 |
|
|
574 |
static class DI implements I { public Object m() { return WRONG; }}
|
|
575 |
static class DJ implements J { public Object m() { return WRONG; }}
|
|
576 |
|
|
577 |
@DontInline
|
|
578 |
public Object test(I i) {
|
|
579 |
return i.m(); // no inlining since J.m is a default method
|
|
580 |
}
|
|
581 |
|
|
582 |
@TestCase
|
|
583 |
public void testMega() {
|
|
584 |
// 0. Trigger compilation of a megamorphic call site
|
|
585 |
compile(megamorphic()); // C1,C2,C3 <: C.m <: intf I <: intf J.m ABSTRACT
|
|
586 |
assertCompiled();
|
|
587 |
|
|
588 |
// Dependency: none
|
|
589 |
|
|
590 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
591 |
|
|
592 |
// 1. No deoptimization/invalidation on not-yet-seen receiver
|
|
593 |
repeat(100, () -> call(new C() {}));
|
|
594 |
assertCompiled();
|
|
595 |
|
|
596 |
// 2. No dependency and no inlining
|
|
597 |
initialize(DJ.class, // DJ.m <: intf J.m ABSTRACT
|
|
598 |
DI.class, // DI.m <: intf I <: intf J.m ABSTRACT
|
|
599 |
K1.class, // intf K1 <: intf I <: intf J.m ABSTRACT
|
|
600 |
K2.class); // intf K2.m ABSTRACT <: intf I <: intf J.m ABSTRACT
|
|
601 |
assertCompiled();
|
|
602 |
}
|
|
603 |
|
|
604 |
@Override
|
|
605 |
public void checkInvalidReceiver() {
|
|
606 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
607 |
I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
|
|
608 |
test(o);
|
|
609 |
});
|
|
610 |
assertCompiled();
|
|
611 |
|
|
612 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
613 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J() {}); // super interface
|
|
614 |
test(j);
|
|
615 |
});
|
|
616 |
assertCompiled();
|
|
617 |
}
|
|
618 |
}
|
|
619 |
|
|
620 |
public static class ThreeLevelDefaultHierarchy1 extends ATest<ThreeLevelDefaultHierarchy1.I> {
|
|
621 |
public ThreeLevelDefaultHierarchy1() { super(I.class, C.class); }
|
|
622 |
|
|
623 |
interface J1 { Object m();}
|
|
624 |
interface J2 extends J1 { default Object m() { return WRONG; } }
|
|
625 |
interface I extends J1, J2 {}
|
|
626 |
|
|
627 |
static class C implements I { public Object m() { return CORRECT; }}
|
|
628 |
|
|
629 |
interface K1 extends I {}
|
|
630 |
interface K2 extends I { Object m(); }
|
|
631 |
interface K3 extends I { default Object m() { return WRONG; }}
|
|
632 |
|
|
633 |
static class DI implements I { public Object m() { return WRONG; }}
|
|
634 |
static class DJ1 implements J1 { public Object m() { return WRONG; }}
|
|
635 |
static class DJ2 implements J2 { public Object m() { return WRONG; }}
|
|
636 |
|
|
637 |
@DontInline
|
|
638 |
public Object test(I i) {
|
|
639 |
return i.m(); // no inlining since J.m is a default method
|
|
640 |
}
|
|
641 |
|
|
642 |
@TestCase
|
|
643 |
public void testMega() {
|
|
644 |
// 0. Trigger compilation of a megamorphic call site
|
|
645 |
compile(megamorphic());
|
|
646 |
assertCompiled();
|
|
647 |
|
|
648 |
// Dependency: none
|
|
649 |
|
|
650 |
checkInvalidReceiver(); // ensure proper type check on receiver is preserved
|
|
651 |
|
|
652 |
// 1. No deoptimization/invalidation on not-yet-seen receiver
|
|
653 |
repeat(100, () -> call(new C() {}));
|
|
654 |
assertCompiled();
|
|
655 |
|
|
656 |
// 2. No dependency, no inlining
|
|
657 |
// CHA doesn't support default methods yet.
|
|
658 |
initialize(DJ1.class,
|
|
659 |
DJ2.class,
|
|
660 |
DI.class,
|
|
661 |
K1.class,
|
|
662 |
K2.class,
|
|
663 |
K3.class);
|
|
664 |
assertCompiled();
|
|
665 |
}
|
|
666 |
|
|
667 |
@Override
|
|
668 |
public void checkInvalidReceiver() {
|
|
669 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
670 |
I o = (I) unsafeCastMH(I.class).invokeExact(new Object()); // unrelated
|
|
671 |
test(o);
|
|
672 |
});
|
|
673 |
assertCompiled();
|
|
674 |
|
|
675 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
676 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J1() { public Object m() { return WRONG; } }); // super interface
|
|
677 |
test(j);
|
|
678 |
});
|
|
679 |
assertCompiled();
|
|
680 |
|
|
681 |
shouldThrow(IncompatibleClassChangeError.class, () -> {
|
|
682 |
I j = (I) unsafeCastMH(I.class).invokeExact((Object)new J2() {}); // super interface
|
|
683 |
test(j);
|
|
684 |
});
|
|
685 |
assertCompiled();
|
|
686 |
}
|
|
687 |
}
|
|
688 |
|
|
689 |
/* =========================================================== */
|
|
690 |
|
|
691 |
interface Action {
|
|
692 |
int run();
|
|
693 |
}
|
|
694 |
|
|
695 |
public static final Unsafe U = Unsafe.getUnsafe();
|
|
696 |
|
|
697 |
interface Test<T> {
|
|
698 |
boolean isCompiled();
|
|
699 |
void assertNotCompiled();
|
|
700 |
void assertCompiled();
|
|
701 |
|
|
702 |
void call(T o);
|
|
703 |
T receiver(int id);
|
|
704 |
|
|
705 |
default Runnable monomophic() {
|
|
706 |
return () -> {
|
|
707 |
call(receiver(0)); // 100%
|
|
708 |
};
|
|
709 |
}
|
|
710 |
|
|
711 |
default Runnable bimorphic() {
|
|
712 |
return () -> {
|
|
713 |
call(receiver(0)); // 50%
|
|
714 |
call(receiver(1)); // 50%
|
|
715 |
};
|
|
716 |
}
|
|
717 |
|
|
718 |
default Runnable polymorphic() {
|
|
719 |
return () -> {
|
|
720 |
for (int i = 0; i < 23; i++) {
|
|
721 |
call(receiver(0)); // 92%
|
|
722 |
}
|
|
723 |
call(receiver(1)); // 4%
|
|
724 |
call(receiver(2)); // 4%
|
|
725 |
};
|
|
726 |
}
|
|
727 |
|
|
728 |
default Runnable megamorphic() {
|
|
729 |
return () -> {
|
|
730 |
call(receiver(0)); // 33%
|
|
731 |
call(receiver(1)); // 33%
|
|
732 |
call(receiver(2)); // 33%
|
|
733 |
};
|
|
734 |
}
|
|
735 |
|
|
736 |
default void compile(Runnable r) {
|
|
737 |
assertNotCompiled();
|
|
738 |
while(!isCompiled()) {
|
|
739 |
r.run();
|
|
740 |
}
|
|
741 |
assertCompiled();
|
|
742 |
}
|
|
743 |
|
|
744 |
default void initialize(Class<?>... cs) {
|
|
745 |
for (Class<?> c : cs) {
|
|
746 |
U.ensureClassInitialized(c);
|
|
747 |
}
|
|
748 |
}
|
|
749 |
|
|
750 |
default void repeat(int cnt, Runnable r) {
|
|
751 |
for (int i = 0; i < cnt; i++) {
|
|
752 |
r.run();
|
|
753 |
}
|
|
754 |
}
|
|
755 |
}
|
|
756 |
|
|
757 |
public static abstract class ATest<T> implements Test<T> {
|
|
758 |
public static final WhiteBox WB = WhiteBox.getWhiteBox();
|
|
759 |
|
|
760 |
public static final Object CORRECT = new Object();
|
|
761 |
public static final Object WRONG = new Object();
|
|
762 |
|
|
763 |
final Method TEST;
|
|
764 |
private final Class<T> declared;
|
|
765 |
private final Class<?> receiver;
|
|
766 |
|
|
767 |
private final HashMap<Integer, T> receivers = new HashMap<>();
|
|
768 |
|
|
769 |
public ATest(Class<T> declared, Class<?> receiver) {
|
|
770 |
this.declared = declared;
|
|
771 |
this.receiver = receiver;
|
|
772 |
TEST = compute(() -> this.getClass().getDeclaredMethod("test", declared));
|
|
773 |
}
|
|
774 |
|
|
775 |
@DontInline
|
|
776 |
public abstract Object test(T i);
|
|
777 |
|
|
778 |
public abstract void checkInvalidReceiver();
|
|
779 |
|
|
780 |
public T receiver(int id) {
|
|
781 |
return receivers.computeIfAbsent(id, (i -> {
|
|
782 |
try {
|
|
783 |
MyClassLoader cl = (MyClassLoader) receiver.getClassLoader();
|
|
784 |
Class<?> sub = cl.subclass(receiver, i);
|
|
785 |
return (T)sub.getDeclaredConstructor().newInstance();
|
|
786 |
} catch (Exception e) {
|
|
787 |
throw new Error(e);
|
|
788 |
}
|
|
789 |
}));
|
|
790 |
}
|
|
791 |
|
|
792 |
@Override
|
|
793 |
public boolean isCompiled() { return WB.isMethodCompiled(TEST); }
|
|
794 |
|
|
795 |
@Override
|
|
796 |
public void assertNotCompiled() { assertFalse(isCompiled()); }
|
|
797 |
|
|
798 |
@Override
|
|
799 |
public void assertCompiled() { assertTrue(isCompiled()); }
|
|
800 |
|
|
801 |
@Override
|
|
802 |
public void call(T i) {
|
|
803 |
assertTrue(test(i) != WRONG);
|
|
804 |
}
|
|
805 |
}
|
|
806 |
|
|
807 |
@Retention(value = RetentionPolicy.RUNTIME)
|
|
808 |
public @interface TestCase {}
|
|
809 |
|
|
810 |
static void run(Class<?> test) {
|
|
811 |
try {
|
|
812 |
for (Method m : test.getDeclaredMethods()) {
|
|
813 |
if (m.isAnnotationPresent(TestCase.class)) {
|
|
814 |
System.out.println(m.toString());
|
|
815 |
ClassLoader cl = new MyClassLoader(test);
|
|
816 |
Class<?> c = cl.loadClass(test.getName());
|
|
817 |
c.getMethod(m.getName()).invoke(c.getDeclaredConstructor().newInstance());
|
|
818 |
}
|
|
819 |
}
|
|
820 |
} catch (Exception e) {
|
|
821 |
throw new Error(e);
|
|
822 |
}
|
|
823 |
}
|
|
824 |
|
|
825 |
static class ObjectToStringHelper {
|
|
826 |
static Object test(Object o) {
|
|
827 |
throw new Error("not used");
|
|
828 |
}
|
|
829 |
}
|
|
830 |
static class ObjectHashCodeHelper {
|
|
831 |
static int test(Object o) {
|
|
832 |
throw new Error("not used");
|
|
833 |
}
|
|
834 |
}
|
|
835 |
|
|
836 |
static final class MyClassLoader extends ClassLoader {
|
|
837 |
private final Class<?> test;
|
|
838 |
|
|
839 |
MyClassLoader(Class<?> test) {
|
|
840 |
this.test = test;
|
|
841 |
}
|
|
842 |
|
|
843 |
static String intl(String s) {
|
|
844 |
return s.replace('.', '/');
|
|
845 |
}
|
|
846 |
|
|
847 |
Class<?> subclass(Class<?> c, int id) {
|
|
848 |
String name = c.getName() + id;
|
|
849 |
Class<?> sub = findLoadedClass(name);
|
|
850 |
if (sub == null) {
|
|
851 |
ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES);
|
|
852 |
cw.visit(52, ACC_PUBLIC | ACC_SUPER, intl(name), null, intl(c.getName()), null);
|
|
853 |
|
|
854 |
{ // Default constructor: <init>()V
|
|
855 |
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
|
|
856 |
mv.visitCode();
|
|
857 |
mv.visitVarInsn(ALOAD, 0);
|
|
858 |
mv.visitMethodInsn(INVOKESPECIAL, intl(c.getName()), "<init>", "()V", false);
|
|
859 |
mv.visitInsn(RETURN);
|
|
860 |
mv.visitMaxs(0, 0);
|
|
861 |
mv.visitEnd();
|
|
862 |
}
|
|
863 |
|
|
864 |
byte[] classFile = cw.toByteArray();
|
|
865 |
return defineClass(name, classFile, 0, classFile.length);
|
|
866 |
}
|
|
867 |
return sub;
|
|
868 |
}
|
|
869 |
|
|
870 |
protected Class<?> loadClass(String name, boolean resolve)
|
|
871 |
throws ClassNotFoundException
|
|
872 |
{
|
|
873 |
// First, check if the class has already been loaded
|
|
874 |
Class<?> c = findLoadedClass(name);
|
|
875 |
if (c == null) {
|
|
876 |
try {
|
|
877 |
c = getParent().loadClass(name);
|
|
878 |
if (name.endsWith("ObjectToStringHelper")) {
|
|
879 |
ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES);
|
|
880 |
cw.visit(52, ACC_PUBLIC | ACC_SUPER, intl(name), null, "java/lang/Object", null);
|
|
881 |
|
|
882 |
{
|
|
883 |
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "test", "(Ljava/lang/Object;)Ljava/lang/Object;", null, null);
|
|
884 |
mv.visitCode();
|
|
885 |
mv.visitVarInsn(ALOAD, 0);
|
|
886 |
mv.visitMethodInsn(INVOKEINTERFACE, intl(test.getName()) + "$I", "toString", "()Ljava/lang/String;", true);
|
|
887 |
mv.visitInsn(ARETURN);
|
|
888 |
mv.visitMaxs(0, 0);
|
|
889 |
mv.visitEnd();
|
|
890 |
}
|
|
891 |
|
|
892 |
byte[] classFile = cw.toByteArray();
|
|
893 |
return defineClass(name, classFile, 0, classFile.length);
|
|
894 |
} else if (name.endsWith("ObjectHashCodeHelper")) {
|
|
895 |
ClassWriter cw = new ClassWriter(COMPUTE_MAXS | COMPUTE_FRAMES);
|
|
896 |
cw.visit(52, ACC_PUBLIC | ACC_SUPER, intl(name), null, "java/lang/Object", null);
|
|
897 |
|
|
898 |
{
|
|
899 |
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "test", "(Ljava/lang/Object;)I", null, null);
|
|
900 |
mv.visitCode();
|
|
901 |
mv.visitVarInsn(ALOAD, 0);
|
|
902 |
mv.visitMethodInsn(INVOKEINTERFACE, intl(test.getName()) + "$I", "hashCode", "()I", true);
|
|
903 |
mv.visitInsn(IRETURN);
|
|
904 |
mv.visitMaxs(0, 0);
|
|
905 |
mv.visitEnd();
|
|
906 |
}
|
|
907 |
|
|
908 |
byte[] classFile = cw.toByteArray();
|
|
909 |
return defineClass(name, classFile, 0, classFile.length);
|
|
910 |
} else if (c == test || name.startsWith(test.getName())) {
|
|
911 |
try {
|
|
912 |
String path = name.replace('.', '/') + ".class";
|
|
913 |
byte[] classFile = getParent().getResourceAsStream(path).readAllBytes();
|
|
914 |
return defineClass(name, classFile, 0, classFile.length);
|
|
915 |
} catch (IOException e) {
|
|
916 |
throw new Error(e);
|
|
917 |
}
|
|
918 |
}
|
|
919 |
} catch (ClassNotFoundException e) {
|
|
920 |
// ClassNotFoundException thrown if class not found
|
|
921 |
// from the non-null parent class loader
|
|
922 |
}
|
|
923 |
|
|
924 |
if (c == null) {
|
|
925 |
// If still not found, then invoke findClass in order
|
|
926 |
// to find the class.
|
|
927 |
c = findClass(name);
|
|
928 |
}
|
|
929 |
}
|
|
930 |
if (resolve) {
|
|
931 |
resolveClass(c);
|
|
932 |
}
|
|
933 |
return c;
|
|
934 |
}
|
|
935 |
}
|
|
936 |
|
|
937 |
public interface RunnableWithException {
|
|
938 |
void run() throws Throwable;
|
|
939 |
}
|
|
940 |
|
|
941 |
public static void shouldThrow(Class<? extends Throwable> expectedException, RunnableWithException r) {
|
|
942 |
try {
|
|
943 |
r.run();
|
|
944 |
throw new AssertionError("Exception not thrown: " + expectedException.getName());
|
|
945 |
} catch(Throwable e) {
|
|
946 |
if (expectedException == e.getClass()) {
|
|
947 |
// success: proper exception is thrown
|
|
948 |
} else {
|
|
949 |
throw new Error(expectedException.getName() + " is expected", e);
|
|
950 |
}
|
|
951 |
}
|
|
952 |
}
|
|
953 |
|
|
954 |
public static MethodHandle unsafeCastMH(Class<?> cls) {
|
|
955 |
try {
|
|
956 |
MethodHandle mh = MethodHandles.identity(Object.class);
|
|
957 |
return MethodHandles.explicitCastArguments(mh, mh.type().changeReturnType(cls));
|
|
958 |
} catch (Throwable e) {
|
|
959 |
throw new Error(e);
|
|
960 |
}
|
|
961 |
}
|
|
962 |
|
|
963 |
static <T> T compute(Callable<T> c) {
|
|
964 |
try {
|
|
965 |
return c.call();
|
|
966 |
} catch (Exception e) {
|
|
967 |
throw new Error(e);
|
|
968 |
}
|
|
969 |
}
|
|
970 |
}
|