Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/management/Introspector/ClassLeakTest.java
41152 views
1
/*
2
* Copyright (c) 2003, 2018, 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 4909536
27
* @summary Ensure that the Introspector does not retain refs to classes
28
* @requires vm.opt.final.ClassUnloading
29
* @author Eamonn McManus
30
*
31
* @run clean ClassLeakTest
32
* @run build ClassLeakTest
33
* @run main ClassLeakTest
34
*/
35
36
import java.lang.ref.WeakReference;
37
import java.io.File;
38
import java.nio.file.Paths;
39
import java.net.*;
40
import java.util.*;
41
42
import javax.management.*;
43
import javax.management.loading.*;
44
45
public class ClassLeakTest {
46
public static void main(String[] args) throws Exception {
47
System.out.println("Testing that registering and unregistering a " +
48
"Standard MBean does not retain a reference to " +
49
"the MBean's class");
50
51
52
String[] cpaths = System.getProperty("test.classes", ".")
53
.split(File.pathSeparator);
54
URL[] urls = new URL[cpaths.length];
55
for (int i=0; i < cpaths.length; i++) {
56
urls[i] = Paths.get(cpaths[i]).toUri().toURL();
57
}
58
59
PrivateMLet mlet = new PrivateMLet(urls, null, false);
60
Class<?> shadowClass = mlet.loadClass(TestMBean.class.getName());
61
if (shadowClass == TestMBean.class) {
62
System.out.println("TEST INVALID: MLet got original " +
63
"TestMBean not shadow");
64
System.exit(1);
65
}
66
shadowClass = null;
67
68
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
69
ObjectName mletName = new ObjectName("x:type=mlet");
70
mbs.registerMBean(mlet, mletName);
71
72
ObjectName testName = new ObjectName("x:type=test");
73
mbs.createMBean(Test.class.getName(), testName, mletName);
74
75
ClassLoader testLoader = mbs.getClassLoaderFor(testName);
76
if (testLoader != mlet) {
77
System.out.println("TEST INVALID: MBean's class loader is not " +
78
"MLet: " + testLoader);
79
System.exit(1);
80
}
81
testLoader = null;
82
83
MBeanInfo info = mbs.getMBeanInfo(testName);
84
MBeanAttributeInfo[] attrs = info.getAttributes();
85
if (attrs.length != 1 || !attrs[0].getName().equals("A")
86
|| !attrs[0].isReadable() || !attrs[0].isWritable()
87
|| attrs[0].isIs() || !attrs[0].getType().equals("int")) {
88
System.out.println("TEST FAILED: unexpected MBeanInfo attrs");
89
System.exit(1);
90
}
91
MBeanOperationInfo[] ops = info.getOperations();
92
if (ops.length != 1 || !ops[0].getName().equals("bogus")
93
|| ops[0].getSignature().length > 0
94
|| ops[0].getImpact() != MBeanOperationInfo.UNKNOWN
95
|| !ops[0].getReturnType().equals("void")) {
96
System.out.println("TEST FAILED: unexpected MBeanInfo ops");
97
System.exit(1);
98
}
99
if (info.getConstructors().length != 2) {
100
System.out.println("TEST FAILED: wrong number of constructors " +
101
"in introspected bean: " +
102
Arrays.asList(info.getConstructors()));
103
System.exit(1);
104
}
105
if (!info.getClassName().endsWith("Test")) {
106
System.out.println("TEST FAILED: wrong info class name: " +
107
info.getClassName());
108
System.exit(1);
109
}
110
111
mbs.unregisterMBean(testName);
112
mbs.unregisterMBean(mletName);
113
114
WeakReference mletRef = new WeakReference(mlet);
115
mlet = null;
116
117
System.out.println("MBean registered and unregistered, waiting for " +
118
"garbage collector to collect class loader");
119
for (int i = 0; i < 10000 && mletRef.get() != null; i++) {
120
System.gc();
121
Thread.sleep(1);
122
}
123
124
if (mletRef.get() == null)
125
System.out.println("Test passed: class loader was GC'd");
126
else {
127
System.out.println("TEST FAILED: class loader was not GC'd");
128
System.exit(1);
129
}
130
}
131
132
public static interface TestMBean {
133
public void bogus();
134
public int getA();
135
public void setA(int a);
136
}
137
138
public static class Test implements TestMBean {
139
public Test() {}
140
public Test(int x) {}
141
142
public void bogus() {}
143
public int getA() {return 0;}
144
public void setA(int a) {}
145
}
146
}
147
148