Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/annotation/LoaderLeakTest.java
41149 views
1
/*
2
* Copyright (c) 2004, 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
* @bug 5040740
27
* @summary annotations cause memory leak
28
* @library /test/lib
29
* @build jdk.test.lib.process.*
30
* @run testng LoaderLeakTest
31
*/
32
33
import jdk.test.lib.Utils;
34
import jdk.test.lib.process.ProcessTools;
35
import org.testng.annotations.BeforeClass;
36
import org.testng.annotations.Test;
37
import java.io.FileInputStream;
38
import java.lang.annotation.Retention;
39
import java.lang.ref.Reference;
40
import java.lang.ref.WeakReference;
41
import java.nio.file.*;
42
import java.util.*;
43
import static java.lang.annotation.RetentionPolicy.RUNTIME;
44
45
public class LoaderLeakTest {
46
47
@Test
48
public void testWithoutReadingAnnotations() throws Throwable {
49
runJavaProcessExpectSuccessExitCode("Main");
50
}
51
52
@Test
53
public void testWithReadingAnnotations() throws Throwable {
54
runJavaProcessExpectSuccessExitCode("Main", "foo");
55
}
56
57
private void runJavaProcessExpectSuccessExitCode(String ... command) throws Throwable {
58
var processBuilder = ProcessTools.createJavaProcessBuilder(command)
59
.directory(Paths.get(Utils.TEST_CLASSES).toFile());
60
ProcessTools.executeCommand(processBuilder).shouldHaveExitValue(0);
61
}
62
63
}
64
65
class Main {
66
67
public static void main(String[] args) throws Exception {
68
for (int i = 0; i < 100; i++) {
69
doTest(args.length != 0);
70
}
71
}
72
73
static void doTest(boolean readAnn) throws Exception {
74
ClassLoader loader = new SimpleClassLoader();
75
var c = new WeakReference<Class<?>>(loader.loadClass("C"));
76
if (c.refersTo(null)) throw new AssertionError("class missing after loadClass");
77
// c.get() should never return null here since we hold a strong
78
// reference to the class loader that loaded the class referred by c.
79
if (c.get().getClassLoader() != loader) throw new AssertionError("wrong classloader");
80
if (readAnn) System.out.println(c.get().getAnnotations()[0]);
81
if (c.refersTo(null)) throw new AssertionError("class missing before GC");
82
System.gc();
83
System.gc();
84
if (c.refersTo(null)) throw new AssertionError("class missing after GC but before loader is unreachable");
85
System.gc();
86
System.gc();
87
Reference.reachabilityFence(loader);
88
loader = null;
89
90
// Might require multiple calls to System.gc() for weak-references
91
// processing to be complete. If the weak-reference is not cleared as
92
// expected we will hang here until timed out by the test harness.
93
while (true) {
94
System.gc();
95
Thread.sleep(20);
96
if (c.refersTo(null)) {
97
break;
98
}
99
}
100
}
101
}
102
103
@Retention(RUNTIME)
104
@interface A {
105
B b();
106
}
107
108
@interface B { }
109
110
@A(b=@B()) class C { }
111
112
class SimpleClassLoader extends ClassLoader {
113
public SimpleClassLoader() { }
114
115
private byte[] getClassImplFromDataBase(String className) {
116
try {
117
return Files.readAllBytes(Paths.get(className + ".class"));
118
} catch (Exception e) {
119
throw new Error("could not load class " + className, e);
120
}
121
}
122
123
@Override
124
public Class<?> loadClass(String className, boolean resolveIt)
125
throws ClassNotFoundException {
126
switch (className) {
127
case "A", "B", "C" -> {
128
var classData = getClassImplFromDataBase(className);
129
return defineClass(className, classData, 0, classData.length);
130
}
131
}
132
return super.loadClass(className, resolveIt);
133
}
134
135
}
136
137