Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/runtime/defmeth/BasicTest.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.BasicTest
38
*/
39
40
package vm.runtime.defmeth;
41
42
import java.util.Map;
43
import java.util.Set;
44
45
import nsk.share.TestFailure;
46
import vm.runtime.defmeth.shared.MemoryClassLoader;
47
import vm.runtime.defmeth.shared.annotation.NotApplicableFor;
48
import vm.runtime.defmeth.shared.builder.TestBuilder;
49
import vm.runtime.defmeth.shared.data.*;
50
import vm.runtime.defmeth.shared.DefMethTest;
51
import vm.runtime.defmeth.shared.executor.TestExecutor;
52
53
import static jdk.internal.org.objectweb.asm.Opcodes.*;
54
import static vm.runtime.defmeth.shared.ExecutionMode.*;
55
56
/**
57
* Basic tests on some of the assertions from JVMS.
58
*/
59
public class BasicTest extends DefMethTest {
60
61
public static void main(String[] args) {
62
DefMethTest.runTest(BasicTest.class,
63
/* majorVer */ Set.of(MIN_MAJOR_VER, MAX_MAJOR_VER),
64
/* flags */ Set.of(0, ACC_SYNCHRONIZED),
65
/* redefine */ Set.of(false, true),
66
/* execMode */ Set.of(DIRECT, REFLECTION, INVOKE_EXACT, INVOKE_GENERIC, INVOKE_WITH_ARGS, INDY));
67
}
68
69
/*
70
* JVMS 6.5 invokevirtual
71
*
72
* ...
73
*
74
* Runtime Exceptions
75
*
76
* Otherwise, if the resolved method is not signature polymorphic:
77
*
78
*/
79
80
/*
81
* ...
82
*
83
* If the resolved method is declared in an interface and the class of
84
* objectref does not implement the resolved interface, invokevirtual throws
85
* an IncompatibleClassChangeError.
86
*/
87
public void testInterfaceNotImplemented(TestBuilder b) {
88
Interface I = b.intf("I")
89
.defaultMethod("m", "()V").emptyBody().build()
90
.build();
91
92
ConcreteClass C = b.clazz("C").build();
93
94
Class expectedClass;
95
if (factory.getExecutionMode().equals("REFLECTION")) {
96
expectedClass = IllegalArgumentException.class;
97
} else if (factory.getExecutionMode().equals("INVOKE_WITH_ARGS")) {
98
// Notes from JDK-8029926 which details reasons behind CCE.
99
// The code below demonstrates the use of a MethodHandle
100
// of kind REF_invokeInterface pointing to method I.m.
101
// Because 'invoke' is called, this MethodHandle is effectively
102
// wrapped in the type adaptations necessary to accept a C
103
// as the first argument. ***Before we even get to the point
104
// of the invokeinterface call to I.m***, the adaptation
105
// code must run, and that is where the ClassCastException occurs.
106
// This exception can be thrown from code that is cleanly
107
// compiled and does no bytecode generation, so an ICCE would
108
// be inappropriate since no classes are changed.
109
expectedClass = ClassCastException.class;
110
} else {
111
expectedClass = IncompatibleClassChangeError.class;
112
}
113
114
b.test().callSite(I, C, "m", "()V").throws_(expectedClass).done();
115
}
116
117
/*
118
* ...
119
*
120
* Otherwise, if no method matching the resolved name and descriptor is
121
* selected, invokevirtual throws an NoSuchMethodError.
122
*
123
* ...
124
*/
125
public void testNoMatch(TestBuilder b) {
126
Interface I = b.intf("I")
127
.defaultMethod("m", "(Ljava/lang/Object;)V").emptyBody().build()
128
.build();
129
130
ConcreteClass C = b.clazz("C").implement(I).build();
131
132
b.test().callSite(C, C, "m", "()I").throws_(NoSuchMethodError.class).done()
133
.test().callSite(C, C, "m1", "()V").throws_(NoSuchMethodError.class).done()
134
.test().callSite(C, C, "m", "(I)V").throws_(NoSuchMethodError.class).done();
135
}
136
137
/*
138
* ...
139
*
140
* 8025604: Updated specification text for both 5.4.3.3 and 5.4.4.4
141
* "Otherwise, if any superinterface of C declares a method with the name and
142
* descriptor specified by the method reference that has set neither
143
* its ACC_PRIVATE flag nor its ACC_STATIC flag, one of these is arbitrarily
144
* chosen and method lookup succeeds."
145
* If method lookup fails, method resolution throws a NoSuchMethodError
146
*
147
* ...
148
*/
149
public void testNonPublic(TestBuilder b) {
150
Interface I = b.intf("I")
151
.defaultMethod("m1", "()V").private_().emptyBody().build()
152
.defaultMethod("m2", "()I").private_().returns(1).build()
153
.build();
154
155
ConcreteClass C = b.clazz("C").implement(I).build();
156
157
b.test().callSite(C, C, "m1", "()V").throws_(NoSuchMethodError.class).done()
158
.test().callSite(C, C, "m2", "()I").throws_(NoSuchMethodError.class).done();
159
}
160
161
/*
162
* Default method override attempt w/ non-public concrete method.
163
* Private methods never override any other method.
164
*
165
* interface I { int m() default { returns 1; } }
166
* class C/D/E implements I {
167
* [private/protected/package-private]
168
* int m() { returns 2;}
169
* }
170
*
171
*/
172
public void testNonPublicOverride(TestBuilder b) {
173
Interface I = b.intf("I")
174
.defaultMethod("m", "()I").returns(1).build()
175
.build();
176
177
ConcreteClass C = b.clazz("C").implement(I)
178
.concreteMethod("m", "()I").private_().returns(2).build()
179
.build();
180
181
ConcreteClass D = b.clazz("D").implement(I)
182
.concreteMethod("m", "()I").protected_().returns(2).build()
183
.build();
184
185
ConcreteClass E = b.clazz("E").implement(I)
186
.concreteMethod("m", "()I").package_private().returns(2).build()
187
.build();
188
189
b.test().callSite(I, C, "m", "()I").returns(1).done()
190
.test().callSite(I, D, "m", "()I").throws_(IllegalAccessError.class).done()
191
.test().callSite(I, E, "m", "()I").throws_(IllegalAccessError.class).done();
192
}
193
194
195
/**
196
* interface I {
197
* static { throw new RE(); }
198
* public default void m() {}
199
* }
200
*
201
* class C implements I {}
202
*
203
* TEST: C c = new C(); ==> ExceptionInInitializerError
204
* Static initialization of class C will trigger
205
* I's static initialization due to I's default method.
206
*/
207
public void testStaticInit(TestBuilder b) {
208
Interface I = b.intf("I")
209
.defaultMethod("<clinit>", "()V")
210
.flags(ACC_STATIC)
211
.throw_(RuntimeException.class)
212
.build()
213
214
.defaultMethod("m", "()V")
215
.emptyBody().build()
216
.build();
217
218
ConcreteClass C = b.clazz("C").implement(I).build();
219
220
boolean isReflectionMode = factory.getExecutionMode().equals("REFLECTION");
221
Class expectedError = isReflectionMode ? RuntimeException.class
222
: ExceptionInInitializerError.class;
223
224
b.test().new_(C).throws_(expectedError).done();
225
}
226
227
/**
228
* interface I {
229
* static { throw new RE(); }
230
* private default void m() {}
231
* }
232
*
233
* class C implements I {}
234
*
235
* TEST: C c = new C(); ==> ExceptionInInitializerError
236
* Static initialization of class C will trigger
237
* I's static initialization due to I's private concrete method.
238
*/
239
public void testStaticInitPrivate(TestBuilder b) {
240
Interface I = b.intf("I")
241
.defaultMethod("<clinit>", "()V")
242
.flags(ACC_STATIC)
243
.throw_(RuntimeException.class)
244
.build()
245
246
.defaultMethod("m", "()V")
247
.flags(ACC_PRIVATE)
248
.emptyBody().build()
249
.build();
250
251
ConcreteClass C = b.clazz("C").implement(I).build();
252
253
boolean isReflectionMode = factory.getExecutionMode().equals("REFLECTION");
254
Class expectedError = isReflectionMode ? RuntimeException.class
255
: ExceptionInInitializerError.class;
256
257
b.test().new_(C).throws_(expectedError).done();
258
}
259
260
/**
261
* interface I {
262
* static { throw new RE()}
263
* }
264
* interface J {
265
* public default int m() { return 1}
266
*
267
* class C implements I,J {}
268
*
269
* TEST: C c = new C()
270
* c.m() returns 1
271
* Static initialization of class C will not trigger
272
* I's static initialization since I has no concrete methods.
273
*/
274
public void testNotStaticInitNoDefault(TestBuilder b) {
275
Interface I = b.intf("I")
276
.defaultMethod("<clinit>", "()V")
277
.flags(ACC_STATIC)
278
.throw_(RuntimeException.class)
279
.build()
280
.build();
281
282
Interface J = b.intf("J")
283
.defaultMethod("m", "()I")
284
.returns(1).build()
285
.build();
286
287
ConcreteClass C = b.clazz("C").implement(I,J).build();
288
289
b.test().callSite(C, C, "m", "()I").returns(1).done();
290
}
291
292
/*
293
* Example A.10
294
*
295
* Let L1 and L2 be class loaders with different interpretations of the type "A".
296
*
297
* Loaded by L1:
298
* public interface I { public default A m() { return null; } }
299
*
300
* Loaded by L2:
301
* public class C implements I {}
302
*
303
* TEST: I o = new C(); o.m() ==> LinkageError;
304
* TEST: C o = new C(); o.m() ==> LinkageError;
305
*/
306
// disabling this test for REFLECTION and INVOKE for now until
307
// loader constraint issue associated with those modes has been resolved
308
@NotApplicableFor(modes = { REFLECTION, INVOKE_WITH_ARGS, INVOKE_EXACT, INVOKE_GENERIC, INDY })
309
public void testLoaderConstraint() {
310
TestBuilder b = factory.getBuilder();
311
ConcreteClass A = b.clazz("A").build();
312
313
Interface I = b.intf("I")
314
.defaultMethod("m", "()LA;").returnsNewInstance(A).build()
315
.build();
316
317
ConcreteClass C = b.clazz("C").implement(I).build();
318
319
b.test().callSite(I, C, "m", "()LA;").throws_(LinkageError.class).done();
320
b.test().callSite(C, C, "m", "()LA;").throws_(LinkageError.class).done();
321
322
TestExecutor executor = b.prepare(new TestBuilder.LoaderConstructor() {
323
@Override
324
public MemoryClassLoader construct(Map<String, byte[]> classFiles) {
325
final byte[] cfI = classFiles.get("I");
326
final byte[] cfC = classFiles.get("C");
327
final byte[] cfA = classFiles.get("A");
328
final byte[] cfT1 = classFiles.get("Test1_I_C_m");
329
final byte[] cfT2 = classFiles.get("Test2_C_C_m");
330
331
final ClassLoader l1 = new ClassLoader() {
332
@Override
333
protected Class<?> findClass(String name) throws ClassNotFoundException {
334
switch (name) {
335
case "I":
336
return defineClass(name, cfI, 0, cfI.length);
337
case "A":
338
return defineClass(name, cfA, 0, cfA.length);
339
default:
340
throw new ClassNotFoundException(name);
341
}
342
}
343
};
344
345
final MemoryClassLoader l2 = new MemoryClassLoader(classFiles) {
346
@Override
347
public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
348
if ("I".equals(name)) {
349
return l1.loadClass(name);
350
} else {
351
return super.loadClass(name, resolve);
352
}
353
}
354
};
355
356
try {
357
// Need to preload classes, otherwise A will be resolved in the same loader
358
l1.loadClass("A"); l1.loadClass("I");
359
l2.loadClass("A"); l2.loadClass("C");
360
} catch (ClassNotFoundException e) {
361
throw new TestFailure(e);
362
}
363
364
return l2;
365
}
366
});
367
368
executor.run();
369
}
370
}
371
372