Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/constant/DynamicConstantDescTest.java
41149 views
1
/*
2
* Copyright (c) 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
import java.lang.constant.ClassDesc;
25
import java.lang.constant.ConstantDesc;
26
import java.lang.constant.DirectMethodHandleDesc;
27
import java.lang.constant.DynamicConstantDesc;
28
import java.lang.constant.MethodHandleDesc;
29
import java.lang.constant.MethodTypeDesc;
30
import java.lang.invoke.MethodHandles;
31
import java.util.ArrayList;
32
import java.util.List;
33
import java.util.concurrent.Callable;
34
import java.util.concurrent.CountDownLatch;
35
import java.util.concurrent.ExecutorService;
36
import java.util.concurrent.Executors;
37
import java.util.concurrent.Future;
38
39
/**
40
* @test
41
* @bug 8263108
42
* @summary Verify that concurrent classloading of java.lang.constant.DynamicConstantDesc and
43
* java.lang.constant.ConstantDescs doesn't lead to a deadlock
44
* @run main/othervm DynamicConstantDescTest
45
* @run main/othervm DynamicConstantDescTest
46
* @run main/othervm DynamicConstantDescTest
47
* @run main/othervm DynamicConstantDescTest
48
* @run main/othervm DynamicConstantDescTest
49
*/
50
// Implementation note: This test cannot use testng, since by the time this test gets a chance
51
// to trigger a concurrent classloading of the classes it's interested in, the testng infrastructure
52
// would already have loaded those classes in a single main thread.
53
public class DynamicConstantDescTest {
54
55
/**
56
* Loads {@code java.lang.constant.DynamicConstantDesc} and {@code java.lang.constant.ConstantDescs}
57
* and invokes {@code java.lang.constant.DynamicConstantDesc#ofCanonical()} concurrently in a thread
58
* of their own and expects the classloading of both those classes
59
* to succeed.
60
*/
61
public static void main(final String[] args) throws Exception {
62
final CountDownLatch taskTriggerLatch = new CountDownLatch(4);
63
final List<Callable<?>> tasks = new ArrayList<>();
64
// a bunch of tasks - some doing just Class.forName and some
65
// invoking DynamicConstantDesc.ofCanonical
66
tasks.add(new Task("java.lang.constant.DynamicConstantDesc", taskTriggerLatch));
67
tasks.add(new InvokeOfCanonical(taskTriggerLatch));
68
tasks.add(new Task("java.lang.constant.ConstantDescs", taskTriggerLatch));
69
tasks.add(new InvokeOfCanonical(taskTriggerLatch));
70
final ExecutorService executor = Executors.newFixedThreadPool(tasks.size());
71
try {
72
final Future<?>[] results = new Future[tasks.size()];
73
// submit
74
int i = 0;
75
for (final Callable<?> task : tasks) {
76
results[i++] = executor.submit(task);
77
}
78
// wait for completion
79
for (i = 0; i < tasks.size(); i++) {
80
results[i].get();
81
}
82
} finally {
83
executor.shutdownNow();
84
}
85
}
86
87
private static class Task implements Callable<Class<?>> {
88
private final String className;
89
private final CountDownLatch latch;
90
91
private Task(final String className, final CountDownLatch latch) {
92
this.className = className;
93
this.latch = latch;
94
}
95
96
@Override
97
public Class<?> call() {
98
System.out.println(Thread.currentThread().getName() + " loading " + this.className);
99
try {
100
// let the other tasks know we are ready to trigger our work
101
latch.countDown();
102
// wait for the other task to let us know they are ready to trigger their work too
103
latch.await();
104
return Class.forName(this.className);
105
} catch (Exception e) {
106
throw new RuntimeException(e);
107
}
108
}
109
}
110
111
enum MyEnum {A, B}
112
113
private static class InvokeOfCanonical implements Callable<Object> {
114
private final CountDownLatch latch;
115
116
private InvokeOfCanonical(final CountDownLatch latch) {
117
this.latch = latch;
118
}
119
120
@Override
121
public Object call() {
122
System.out.println(Thread.currentThread().getName()
123
+ " calling DynamicConstantDesc.ofCanonical()");
124
try {
125
// let the other tasks know we are ready to trigger our work
126
latch.countDown();
127
// wait for the other task to let us know they are ready to trigger their work too
128
latch.await();
129
ConstantDesc desc = DynamicConstantDesc.ofCanonical(boostrapMethodForEnumConstant(),
130
"A", ClassDesc.of("DynamicConstantDescTest").nested("MyEnum"),
131
new ConstantDesc[0]);
132
if (desc == null) {
133
throw new Exception("DynamicConstantDesc.ofCanonical unexpectedly returned null");
134
}
135
if (!MyEnum.A.equals(desc.resolveConstantDesc(MethodHandles.lookup()))) {
136
throw new Exception("DynamicConstantDesc.ofCanonical returned unexpected result " + desc);
137
}
138
return desc;
139
} catch (Exception e) {
140
throw new RuntimeException(e);
141
}
142
}
143
144
private static DirectMethodHandleDesc boostrapMethodForEnumConstant() {
145
ClassDesc[] args = {ClassDesc.of("java.lang.invoke.MethodHandles").nested("Lookup"),
146
ClassDesc.of("java.lang.String"),
147
ClassDesc.of("java.lang.Class")};
148
return MethodHandleDesc.ofMethod(java.lang.constant.DirectMethodHandleDesc.Kind.STATIC,
149
ClassDesc.of("java.lang.invoke.ConstantBootstraps"),
150
"enumConstant", MethodTypeDesc.of(ClassDesc.of("java.lang.Enum"), new ClassDesc[0])
151
.insertParameterTypes(0, args));
152
}
153
154
}
155
156
}
157