Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/runtime/defmeth/DefaultVsAbstractTest.java
41159 views
1
/*
2
* Copyright (c) 2013, 2021, 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
*
27
* @modules java.base/jdk.internal.org.objectweb.asm:+open java.base/jdk.internal.org.objectweb.asm.util:+open
28
* @library /vmTestbase /test/lib
29
*
30
* @comment build retransform.jar in current dir
31
* @run driver vm.runtime.defmeth.shared.BuildJar
32
*
33
* @run driver jdk.test.lib.FileInstaller . .
34
* @run main/othervm/native
35
* -agentlib:redefineClasses
36
* -javaagent:retransform.jar
37
* vm.runtime.defmeth.DefaultVsAbstractTest
38
*/
39
package vm.runtime.defmeth;
40
41
import java.util.Set;
42
43
import vm.runtime.defmeth.shared.DefMethTest;
44
import vm.runtime.defmeth.shared.data.*;
45
import vm.runtime.defmeth.shared.builder.TestBuilder;
46
47
import static jdk.internal.org.objectweb.asm.Opcodes.ACC_SYNCHRONIZED;
48
import static vm.runtime.defmeth.shared.ExecutionMode.*;
49
50
/**
51
* Tests on interaction of default methods with abstract methods
52
*
53
* The rule: "the superclass always wins."
54
*
55
* In searching the superclass hierarchy, a declaration in a superclass is
56
* preferred to a default in an interface. This preference includes abstract
57
* methods in superclasses as well; the defaults are only considered when
58
* the entire implementation hierarchy is silent on the status of the method
59
* in question.
60
*/
61
public class DefaultVsAbstractTest extends DefMethTest {
62
63
public static void main(String[] args) {
64
DefMethTest.runTest(DefaultVsAbstractTest.class,
65
/* majorVer */ Set.of(MIN_MAJOR_VER, MAX_MAJOR_VER),
66
/* flags */ Set.of(0, ACC_SYNCHRONIZED),
67
/* redefine */ Set.of(false, true),
68
/* execMode */ Set.of(DIRECT, REFLECTION, INVOKE_EXACT, INVOKE_GENERIC, INVOKE_WITH_ARGS, INDY));
69
}
70
71
/*
72
* interface I { public int m() default { return 1; } }
73
* class C implements I { public abstract int m(); }
74
*
75
* TEST: new C() throws InstantiationError
76
*/
77
public void test0(TestBuilder b) {
78
Interface I = b.intf("I")
79
.defaultMethod("m", "()I").returns(1).build()
80
.build();
81
82
ConcreteClass C = b.clazz("C").implement(I)
83
.abstractMethod("m", "()I").build()
84
.build();
85
86
b.test()
87
.callSite(I, C, "m", "()I")
88
.throws_(InstantiationError.class)
89
.done();
90
}
91
92
/*
93
* interface I {
94
* public int m() default { return 1; }
95
* }
96
* class C implements I {
97
* public abstract int m();
98
* }
99
* class D extends C {}
100
*
101
* TEST: I i = new D(); i.m() ==> AME
102
* TEST: C c = new D(); c.m() ==> AME
103
* TEST: D d = new D(); d.m() ==> AME
104
*/
105
public void test1(TestBuilder b) {
106
Interface I = b.intf("I")
107
.defaultMethod("m", "()I").returns(1).build()
108
.build();
109
110
ConcreteClass C = b.clazz("C").implement(I)
111
.abstractMethod("m", "()I").build()
112
.build();
113
114
ConcreteClass D = b.clazz("D").extend(C).build();
115
116
b.test()
117
.callSite(I, D, "m", "()I")
118
.throws_(AbstractMethodError.class)
119
.done()
120
.test()
121
.callSite(C, D, "m", "()I")
122
.throws_(AbstractMethodError.class)
123
.done()
124
.test()
125
.callSite(D, D, "m", "()I")
126
.throws_(AbstractMethodError.class)
127
.done();
128
}
129
130
/*
131
* interface I {
132
* default public int m() { return 1; }
133
* }
134
* class C {
135
* abstract public int m();
136
* }
137
* class D extends C implements I {}
138
*
139
* TEST: I o = new D(); o.m()I throws AME
140
* TEST: C o = new D(); o.m()I throws AME
141
* TEST: D o = new D(); o.m()I throws AME
142
*/
143
public void test2(TestBuilder b) {
144
Interface I = b.intf("I")
145
.defaultMethod("m", "()I").returns(1).build()
146
.build();
147
148
ConcreteClass C = b.clazz("C")
149
.abstractMethod("m", "()I").build()
150
.build();
151
152
ConcreteClass D = b.clazz("D").extend(C).implement(I).build();
153
154
b.test()
155
.callSite(I, D, "m", "()I")
156
.throws_(AbstractMethodError.class)
157
.done()
158
.test()
159
.callSite(C, D, "m", "()I")
160
.throws_(AbstractMethodError.class)
161
.done()
162
.test()
163
.callSite(D, D, "m", "()I")
164
.throws_(AbstractMethodError.class)
165
.done();
166
}
167
168
/*
169
* interface I {
170
* default public int m() { return 1; }
171
* }
172
* class C {
173
* abstract public int m();
174
* }
175
* class D extends C implements I {
176
* public int m() { return 2; }
177
* }
178
*
179
* TEST: I o = new D(); o.m()I == 2
180
* TEST: C o = new D(); o.m()I == 2
181
* TEST: D o = new D(); o.m()I == 2
182
*/
183
public void test3(TestBuilder b) {
184
Interface I = b.intf("I")
185
.defaultMethod("m", "()I").returns(1).build()
186
.build();
187
188
ConcreteClass C = b.clazz("C")
189
.abstractMethod("m", "()I").build()
190
.build();
191
192
ConcreteClass D = b.clazz("D").extend(C).implement(I)
193
.concreteMethod("m", "()I").returns(2)
194
.build()
195
.build();
196
197
b.test() // I i = new D(); ...
198
.callSite(I, D, "m", "()I").returns(2)
199
.done()
200
.test() // C c = new D(); ...
201
.callSite(C, D, "m", "()I").returns(2)
202
.done()
203
.test() // D d = new C(); ...
204
.callSite(D, D, "m", "()I").returns(2)
205
.done();
206
}
207
208
/*
209
* interface I {
210
* default public int m() { return 1; }
211
* }
212
* class E {
213
* abstract public int m();
214
* }
215
* class D extends E {}
216
* class C extends D implements I {}
217
*
218
* TEST: I o = new C(); o.m()I throws AME
219
* TEST: E o = new C(); o.m()I throws AME
220
* TEST: D o = new C(); o.m()I throws AME
221
* TEST: C o = new C(); o.m()I throws AME
222
*/
223
public void test4(TestBuilder b) {
224
Interface I = b.intf("I")
225
.defaultMethod("m", "()I").returns(1).build()
226
.build();
227
228
ConcreteClass E = b.clazz("E")
229
.abstractMethod("m", "()I").build()
230
.build();
231
232
ConcreteClass D = b.clazz("D").extend(E).build();
233
234
ConcreteClass C = b.clazz("C").extend(D).implement(I).build();
235
236
b.test() // I i = new C(); ...
237
.callSite(I, C, "m", "()I")
238
.throws_(AbstractMethodError.class)
239
.done()
240
.test() // E e = new C(); ...
241
.callSite(E, C, "m", "()I")
242
.throws_(AbstractMethodError.class)
243
.done()
244
.test() // D d = new C(); ...
245
.callSite(D, C, "m", "()I")
246
.throws_(AbstractMethodError.class)
247
.done()
248
.test() // C c = new C(); ...
249
.callSite(C, C, "m", "()I")
250
.throws_(AbstractMethodError.class)
251
.done();
252
}
253
254
/*
255
* interface I {
256
* default public int m() { return 1; }
257
* }
258
* class E {
259
* abstract public int m();
260
* }
261
* class D extends E {
262
* public int m() { return 2; }
263
* }
264
* class C extends D implements I {}
265
*
266
* TEST: I o = new C(); o.m()I == 2
267
* TEST: I o = new C(); o.m()I == 2
268
* TEST: I o = new C(); o.m()I == 2
269
* TEST: I o = new C(); o.m()I == 2
270
*/
271
public void test5(TestBuilder b) {
272
Interface I = b.intf("I")
273
.defaultMethod("m", "()I").returns(1).build()
274
.build();
275
276
ConcreteClass E = b.clazz("E")
277
.abstractMethod("m", "()I").build()
278
.build();
279
280
ConcreteClass D = b.clazz("D").extend(E)
281
.concreteMethod("m", "()I").returns(2).build()
282
.build();
283
284
ConcreteClass C = b.clazz("C").extend(D).implement(I).build();
285
286
b.test() // I i = new C(); ...
287
.callSite(I, C, "m", "()I")
288
.returns(2)
289
.done()
290
.test() // E e = new C(); ...
291
.callSite(I, C, "m", "()I")
292
.returns(2)
293
.done()
294
.test() // D d = new C(); ...
295
.callSite(I, C, "m", "()I")
296
.returns(2)
297
.done()
298
.test() // C c = new C(); ...
299
.callSite(I, C, "m", "()I")
300
.returns(2)
301
.done();
302
}
303
304
/*
305
* interface I {
306
* default public int m() { return 1; }
307
* }
308
* interface J {
309
* default public int m() { return 2; }
310
* }
311
* class E {
312
* abstract public int m();
313
* }
314
* class D extends E {
315
* public int m() { return 3; }
316
* }
317
* class C extends D implements I, J {}
318
*
319
* TEST: I o = new C(); o.m()I == 3
320
* TEST: J o = new C(); o.m()I == 3
321
* TEST: E o = new C(); o.m()I == 3
322
* TEST: D o = new C(); o.m()I == 3
323
* TEST: J o = new C(); o.m()I == 3
324
*/
325
public void test6(TestBuilder b) {
326
Interface I = b.intf("I")
327
.defaultMethod("m", "()I").returns(1).build()
328
.build();
329
330
Interface J = b.intf("J")
331
.defaultMethod("m", "()I").returns(2).build()
332
.build();
333
334
ConcreteClass E = b.clazz("E")
335
.abstractMethod("m", "()I").build()
336
.build();
337
338
ConcreteClass D = b.clazz("D").extend(E)
339
.concreteMethod("m", "()I").returns(3).build()
340
.build();
341
342
ConcreteClass C = b.clazz("C").extend(D).implement(I, J).build();
343
344
345
b.test() // I i = new C(); ...
346
.callSite(I, C, "m", "()I").returns(3)
347
.done()
348
.test() // J j = new C(); ...
349
.callSite(J, C, "m", "()I").returns(3)
350
.done()
351
.test() // E e = new C(); ...
352
.callSite(E, C, "m", "()I").returns(3)
353
.done()
354
.test() // D d = new C(); ...
355
.callSite(D, C, "m", "()I").returns(3)
356
.done()
357
.test() // C c = new C(); ...
358
.callSite(J, C, "m", "()I").returns(3)
359
.done();
360
}
361
362
/*
363
* interface I {
364
* abstract public int m();
365
* }
366
*
367
* interface J {
368
* default public int m() { return 1; }
369
* }
370
*
371
* class A implements I;
372
*
373
* class B extends A implements J;
374
*
375
* TEST: A o = new B(); o.m()I
376
* returns 1 for REFLECTION and INVOKE_WITH_ARGS
377
* ICCE for other modes
378
*/
379
public void testInvokeInterfaceClassDefaultMethod(TestBuilder b) {
380
Interface I = b.intf("I")
381
.abstractMethod("m", "()I").build()
382
.build();
383
384
Interface J = b.intf("J")
385
.extend(I)
386
.defaultMethod("m", "()I").returns(1).build()
387
.build();
388
389
ConcreteClass A = b.clazz("A").implement(I).build();
390
391
ConcreteClass B = b.clazz("B").extend(A).implement(J).build();
392
393
String exeMode = factory.getExecutionMode();
394
395
// the test passes in the reflection mode because there's no way to
396
// express invokeinterface on a class using Reflection API
397
// In the test generator, vm.runtime.defmeth.shared.executor.ReflectionTest,
398
// the invokeinterface is switched to invokevirtual.
399
//
400
// the test passes in the INVOKE_WITH_ARGS mode due to the fix for
401
// JDK-8032010 to conform with the removal of the following check
402
// during method resolution in JVMS-5.4.3.3 Method Resolution
403
// "If method lookup succeeds and the method is abstract, but C is not
404
// abstract, method resolution throws an AbstractMethodError."
405
if (exeMode.equals("REFLECTION") ||
406
exeMode.equals("INVOKE_WITH_ARGS")) {
407
b.test().interfaceCallSite(A, B, "m", "()I")
408
.returns(1).done();
409
} else {
410
// ICCE in other modes due to
411
// JVMS-5.4.3.4. Interface Method Resolution
412
// When resolving an interface method reference:
413
// If C is not an interface, interface method resolution throws an IncompatibleClassChangeError.
414
b.test().interfaceCallSite(A, B, "m", "()I")
415
.throws_(IncompatibleClassChangeError.class).done();
416
}
417
}
418
419
/*
420
* interface I {
421
* abstract public int m();
422
* }
423
*
424
* interface J {
425
* abstract public int m();
426
* }
427
*
428
* class A implements I;
429
*
430
* class B extends A implements J;
431
*
432
* TEST: A o = new B(); o.m()I throws ICCE
433
*/
434
public void testInvokeInterfaceClassAbstractMethod(TestBuilder b) {
435
Interface I = b.intf("I")
436
.abstractMethod("m", "()I").build()
437
.build();
438
439
Interface J = b.intf("J")
440
.abstractMethod("m", "()I").build()
441
.build();
442
443
ConcreteClass A = b.clazz("A").implement(I).build();
444
445
ConcreteClass B = b.clazz("B").extend(A).implement(J).build();
446
447
// JVMS-5.4.3.4. Interface Method Resolution
448
// When resolving an interface method reference:
449
// If C is not an interface, interface method resolution throws an IncompatibleClassChangeError.
450
b.test().interfaceCallSite(A, B, "m", "()I")
451
.throws_(IncompatibleClassChangeError.class).done();
452
}
453
454
/*
455
* interface I {
456
* public int m() default { return 1; }
457
* }
458
*
459
* interface J {
460
* public int m() default { return 1; }
461
* }
462
*
463
* class A implements I;
464
*
465
* class B extends A implements J;
466
*
467
* TEST: A o = new B(); o.m()I throws ICCE
468
*/
469
public void testInvokeInterfaceMultipleDefinedClassDefaultMethod(TestBuilder b) {
470
Interface I = b.intf("I")
471
.defaultMethod("m", "()I").returns(1).build()
472
.build();
473
474
Interface J = b.intf("J")
475
.defaultMethod("m", "()I").returns(1).build()
476
.build();
477
478
ConcreteClass A = b.clazz("A").implement(I).build();
479
480
ConcreteClass B = b.clazz("B").extend(A).implement(J).build();
481
482
// JVMS-5.4.3.4. Interface Method Resolution
483
// When resolving an interface method reference:
484
// If C is not an interface, interface method resolution throws an IncompatibleClassChangeError.
485
b.test().interfaceCallSite(A, B, "m", "()I")
486
.throws_(IncompatibleClassChangeError.class).done();
487
}
488
489
}
490
491