Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/javax/management/mxbean/MXBeanLoadingTest1.java
41149 views
1
/*
2
* Copyright (c) 2005, 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 8058865
27
* @summary Checks correct collection of MXBean's class after unregistration
28
* @requires vm.opt.final.ClassUnloading
29
* @author Olivier Lagneau
30
*
31
* @library /lib/testlibrary
32
*
33
* @run main/othervm/timeout=300 MXBeanLoadingTest1
34
*/
35
36
import java.lang.ref.WeakReference;
37
import java.net.URL;
38
import java.util.Arrays;
39
import java.util.Map;
40
import javax.management.Attribute;
41
import javax.management.JMX;
42
import javax.management.MBeanAttributeInfo;
43
import javax.management.MBeanInfo;
44
import javax.management.MBeanOperationInfo;
45
import javax.management.MBeanServer;
46
import javax.management.MBeanServerFactory;
47
import javax.management.MXBean;
48
import javax.management.ObjectName;
49
import javax.management.loading.PrivateMLet;
50
import javax.management.openmbean.CompositeData;
51
import javax.management.openmbean.CompositeDataSupport;
52
import javax.management.openmbean.CompositeType;
53
import javax.management.openmbean.OpenType;
54
import javax.management.openmbean.SimpleType;
55
56
public class MXBeanLoadingTest1 {
57
58
public static void main(String[] args) throws Exception {
59
MXBeanLoadingTest1 test = new MXBeanLoadingTest1();
60
test.run((Map<String, Object>)null);
61
}
62
63
64
public void run(Map<String, Object> args) {
65
66
System.out.println("MXBeanLoadingTest1::run: Start") ;
67
68
try {
69
System.out.println("We ensure no reference is retained on MXBean class"
70
+ " after it is unregistered. We take time to perform"
71
+ " some little extra check of Descriptors, MBean*Info.");
72
73
ClassLoader myClassLoader = MXBeanLoadingTest1.class.getClassLoader();
74
if(myClassLoader == null)
75
throw new RuntimeException("Test Failed : Null Classloader for test");
76
URL url = myClassLoader.getResource(
77
MXBeanLoadingTest1.class.getCanonicalName()
78
.replace(".", "/") + ".class");
79
String clsLoadPath = url.toURI().toString().
80
replaceAll(MXBeanLoadingTest1.class.getSimpleName()
81
+ ".class", "");
82
83
URL[] urls = new URL[]{new URL(clsLoadPath)};
84
PrivateMLet mlet = new PrivateMLet(urls, null, false);
85
Class<?> shadowClass = mlet.loadClass(TestMXBean.class.getName());
86
87
if (shadowClass == TestMXBean.class) {
88
String message = "(ERROR) MLet got original TestMXBean, not shadow";
89
System.out.println(message);
90
throw new RuntimeException(message);
91
}
92
shadowClass = null;
93
94
MBeanServer mbs = MBeanServerFactory.createMBeanServer();
95
ObjectName mletName = new ObjectName("x:type=mlet");
96
mbs.registerMBean(mlet, mletName);
97
98
ObjectName testName = new ObjectName("x:type=test");
99
mbs.createMBean(Test.class.getName(), testName, mletName);
100
101
// That test fails because the MXBean instance is accessed via
102
// a delegate OpenMBean which has
103
ClassLoader testLoader = mbs.getClassLoaderFor(testName);
104
105
if (testLoader != mlet) {
106
System.out.println("MLet " + mlet);
107
String message = "(ERROR) MXBean's class loader is not MLet: "
108
+ testLoader;
109
System.out.println(message);
110
throw new RuntimeException(message);
111
}
112
testLoader = null;
113
114
115
// Cycle get/set/get of the attribute of type Luis.
116
// We check the set is effective.
117
CompositeData cd_B = (CompositeData)mbs.getAttribute(testName, "B");
118
CompositeType compType_B = cd_B.getCompositeType();
119
120
CompositeDataSupport cds_B =
121
new CompositeDataSupport(compType_B,
122
new String[]{"something"},
123
new Object[]{Integer.valueOf(13)});
124
Attribute myAtt = new Attribute("B", cds_B);
125
mbs.setAttribute(testName, myAtt);
126
127
CompositeData cd_B2 = (CompositeData)mbs.getAttribute(testName, "B");
128
129
if ( ((Integer)cd_B2.get("something")).intValue() != 13 ) {
130
String message = "(ERROR) The setAttribute of att B did not work;"
131
+ " expect Luis.something = 13 but got "
132
+ cd_B2.get("something");
133
System.out.println(message);
134
throw new RuntimeException(message);
135
}
136
137
MBeanInfo info = mbs.getMBeanInfo(testName);
138
String mxbeanField =
139
(String)info.getDescriptor().getFieldValue(JMX.MXBEAN_FIELD);
140
141
if ( mxbeanField == null || ! mxbeanField.equals("true")) {
142
String message = "(ERROR) Improper mxbean field value "
143
+ mxbeanField;
144
System.out.println(message);
145
throw new RuntimeException(message);
146
}
147
148
// Check the 2 attributes.
149
MBeanAttributeInfo[] attrs = info.getAttributes();
150
151
if ( attrs.length == 2 ) {
152
for (MBeanAttributeInfo mbai : attrs) {
153
String originalTypeFieldValue =
154
(String)mbai.getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);
155
OpenType<?> openTypeFieldValue =
156
(OpenType<?>)mbai.getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);
157
158
if ( mbai.getName().equals("A") ) {
159
if ( !mbai.isReadable() || !mbai.isWritable()
160
|| mbai.isIs()
161
|| !mbai.getType().equals("int") ) {
162
String message = "(ERROR) Unexpected MBeanAttributeInfo for A "
163
+ mbai;
164
System.out.println(message);
165
throw new RuntimeException(message);
166
}
167
168
if ( ! originalTypeFieldValue.equals("int") ) {
169
String message = "(ERROR) Unexpected originalType in Descriptor for A "
170
+ originalTypeFieldValue;
171
System.out.println(message);
172
throw new RuntimeException(message);
173
}
174
175
if ( ! openTypeFieldValue.equals(SimpleType.INTEGER) ) {
176
String message = "(ERROR) Unexpected openType in Descriptor for A "
177
+ originalTypeFieldValue;
178
System.out.println(message);
179
throw new RuntimeException(message);
180
}
181
} else if ( mbai.getName().equals("B") ) {
182
if ( !mbai.isReadable() || !mbai.isWritable()
183
|| mbai.isIs()
184
|| !mbai.getType().equals("javax.management.openmbean.CompositeData") ) {
185
String message = "(ERROR) Unexpected MBeanAttributeInfo for B "
186
+ mbai;
187
System.out.println(message);
188
throw new RuntimeException(message);
189
}
190
191
if ( ! originalTypeFieldValue.equals(Luis.class.getName()) ) {
192
String message = "(ERROR) Unexpected originalType in Descriptor for B "
193
+ originalTypeFieldValue;
194
System.out.println(message);
195
throw new RuntimeException(message);
196
}
197
198
if ( ! openTypeFieldValue.equals(compType_B) ) {
199
String message = "(ERROR) Unexpected openType in Descriptor for B "
200
+ compType_B;
201
System.out.println(message);
202
throw new RuntimeException(message);
203
}
204
} else {
205
String message = "(ERROR) Unknown attribute name";
206
System.out.println(message);
207
throw new RuntimeException(message);
208
}
209
}
210
} else {
211
String message = "(ERROR) Unexpected MBeanAttributeInfo array"
212
+ Arrays.deepToString(attrs);
213
System.out.println(message);
214
throw new RuntimeException(message);
215
}
216
217
// Check the MXBean operation.
218
MBeanOperationInfo[] ops = info.getOperations();
219
// The impact is ACTION_INFO as for a standard MBean it is UNKNOWN,
220
// logged 6320104.
221
if (ops.length != 1 || !ops[0].getName().equals("bogus")
222
|| ops[0].getSignature().length > 0
223
|| !ops[0].getReturnType().equals("void")) {
224
String message = "(ERROR) Unexpected MBeanOperationInfo array "
225
+ Arrays.deepToString(ops);
226
System.out.println(message);
227
throw new RuntimeException(message);
228
}
229
230
String originalTypeFieldValue =
231
(String)ops[0].getDescriptor().getFieldValue(JMX.ORIGINAL_TYPE_FIELD);
232
OpenType<?> openTypeFieldValue =
233
(OpenType<?>)ops[0].getDescriptor().getFieldValue(JMX.OPEN_TYPE_FIELD);
234
235
if ( ! originalTypeFieldValue.equals("void") ) {
236
String message = "(ERROR) Unexpected originalType in Descriptor for bogus "
237
+ originalTypeFieldValue;
238
System.out.println(message);
239
throw new RuntimeException(message);
240
}
241
242
if ( ! openTypeFieldValue.equals(SimpleType.VOID) ) {
243
String message = "(ERROR) Unexpected openType in Descriptor for bogus "
244
+ originalTypeFieldValue;
245
System.out.println(message);
246
throw new RuntimeException(message);
247
}
248
249
// Check there is 2 constructors.
250
if (info.getConstructors().length != 2) {
251
String message = "(ERROR) Wrong number of constructors " +
252
"in introspected bean: " +
253
Arrays.asList(info.getConstructors());
254
System.out.println(message);
255
throw new RuntimeException(message);
256
}
257
258
// Check MXBean class name.
259
if (!info.getClassName().endsWith("Test")) {
260
String message = "(ERROR) Wrong info class name: " +
261
info.getClassName();
262
System.out.println(message);
263
throw new RuntimeException(message);
264
}
265
266
mbs.unregisterMBean(testName);
267
mbs.unregisterMBean(mletName);
268
269
WeakReference<PrivateMLet> mletRef =
270
new WeakReference<PrivateMLet>(mlet);
271
mlet = null;
272
273
System.out.println("MXBean registered and unregistered, waiting for " +
274
"garbage collector to collect class loader");
275
276
for (int i = 0; i < 10000 && mletRef.get() != null; i++) {
277
System.gc();
278
Thread.sleep(1);
279
}
280
281
if (mletRef.get() == null)
282
System.out.println("(OK) class loader was GC'd");
283
else {
284
String message = "(ERROR) Class loader was not GC'd";
285
System.out.println(message);
286
throw new RuntimeException(message);
287
}
288
} catch(Exception e) {
289
Utils.printThrowable(e, true) ;
290
throw new RuntimeException(e);
291
}
292
293
System.out.println("MXBeanLoadingTest1::run: Done without any error") ;
294
}
295
296
297
// I agree the use of the MXBean annotation and the MXBean suffix for the
298
// interface name are redundant but however harmless.
299
//
300
@MXBean(true)
301
public static interface TestMXBean {
302
public void bogus();
303
public int getA();
304
public void setA(int a);
305
public Luis getB();
306
public void setB(Luis mi);
307
}
308
309
310
public static class Test implements TestMXBean {
311
private Luis luis = new Luis() ;
312
public Test() {}
313
public Test(int x) {}
314
315
public void bogus() {}
316
public int getA() {return 0;}
317
public void setA(int a) {}
318
public Luis getB() {return this.luis;}
319
public void setB(Luis luis) {this.luis = luis;}
320
}
321
322
323
public static class Luis {
324
private int something = 0;
325
public Luis() {}
326
public int getSomething() {return something;}
327
public void setSomething(int v) {something = v;}
328
public void doNothing() {}
329
}
330
}
331
332