Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/management/Introspector/ClassLeakTest.java
38838 views
/*1* Copyright (c) 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 490953626* @summary Ensure that the Introspector does not retain refs to classes27* @author Eamonn McManus28* @run clean ClassLeakTest29* @run build ClassLeakTest30* @run main ClassLeakTest31*/3233import java.lang.ref.WeakReference;34import java.net.*;35import java.util.*;3637import javax.management.*;38import javax.management.loading.*;3940public class ClassLeakTest {41public static void main(String[] args) throws Exception {42System.out.println("Testing that registering and unregistering a " +43"Standard MBean does not retain a reference to " +44"the MBean's class");4546ClassLoader myClassLoader = ClassLeakTest.class.getClassLoader();47if (!(myClassLoader instanceof URLClassLoader)) {48System.out.println("TEST INVALID: test's class loader is not " +49"a URLClassLoader");50System.exit(1);51}5253URLClassLoader myURLClassLoader = (URLClassLoader) myClassLoader;54URL[] urls = myURLClassLoader.getURLs();55PrivateMLet mlet = new PrivateMLet(urls, null, false);56Class shadowClass = mlet.loadClass(TestMBean.class.getName());57if (shadowClass == TestMBean.class) {58System.out.println("TEST INVALID: MLet got original " +59"TestMBean not shadow");60System.exit(1);61}62shadowClass = null;6364MBeanServer mbs = MBeanServerFactory.createMBeanServer();65ObjectName mletName = new ObjectName("x:type=mlet");66mbs.registerMBean(mlet, mletName);6768ObjectName testName = new ObjectName("x:type=test");69mbs.createMBean(Test.class.getName(), testName, mletName);7071ClassLoader testLoader = mbs.getClassLoaderFor(testName);72if (testLoader != mlet) {73System.out.println("TEST INVALID: MBean's class loader is not " +74"MLet: " + testLoader);75System.exit(1);76}77testLoader = null;7879MBeanInfo info = mbs.getMBeanInfo(testName);80MBeanAttributeInfo[] attrs = info.getAttributes();81if (attrs.length != 1 || !attrs[0].getName().equals("A")82|| !attrs[0].isReadable() || !attrs[0].isWritable()83|| attrs[0].isIs() || !attrs[0].getType().equals("int")) {84System.out.println("TEST FAILED: unexpected MBeanInfo attrs");85System.exit(1);86}87MBeanOperationInfo[] ops = info.getOperations();88if (ops.length != 1 || !ops[0].getName().equals("bogus")89|| ops[0].getSignature().length > 090|| ops[0].getImpact() != MBeanOperationInfo.UNKNOWN91|| !ops[0].getReturnType().equals("void")) {92System.out.println("TEST FAILED: unexpected MBeanInfo ops");93System.exit(1);94}95if (info.getConstructors().length != 2) {96System.out.println("TEST FAILED: wrong number of constructors " +97"in introspected bean: " +98Arrays.asList(info.getConstructors()));99System.exit(1);100}101if (!info.getClassName().endsWith("Test")) {102System.out.println("TEST FAILED: wrong info class name: " +103info.getClassName());104System.exit(1);105}106107mbs.unregisterMBean(testName);108mbs.unregisterMBean(mletName);109110WeakReference mletRef = new WeakReference(mlet);111mlet = null;112113System.out.println("MBean registered and unregistered, waiting for " +114"garbage collector to collect class loader");115for (int i = 0; i < 10000 && mletRef.get() != null; i++) {116System.gc();117Thread.sleep(1);118}119120if (mletRef.get() == null)121System.out.println("Test passed: class loader was GC'd");122else {123System.out.println("TEST FAILED: class loader was not GC'd");124System.exit(1);125}126}127128public static interface TestMBean {129public void bogus();130public int getA();131public void setA(int a);132}133134public static class Test implements TestMBean {135public Test() {}136public Test(int x) {}137138public void bogus() {}139public int getA() {return 0;}140public void setA(int a) {}141}142}143144145