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