1 /*
   2  * Copyright (c) 2016, 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 import java.beans.BeanInfo;
  25 import java.beans.Introspector;
  26 import java.beans.MethodDescriptor;
  27 import java.io.File;
  28 import java.net.URL;
  29 import java.net.URLClassLoader;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.nio.file.StandardCopyOption;
  34 import java.util.Arrays;
  35 
  36 /**
  37  * @test
  38  * @bug 8159696
  39  * @library /javax/swing/regtesthelpers
  40  * @compile ./stub/Stub.java
  41  * @run main/othervm -mx32M UnloadClassBeanInfo
  42  */
  43 public class UnloadClassBeanInfo {
  44 
  45     private static URLClassLoader loader;
  46 
  47     public static void main(final String[] args) throws Exception {
  48         Class cl = getStub();
  49         System.out.println("cl.getClassLoader() = " + cl.getClassLoader());
  50         final BeanInfo beanInfo = Introspector.getBeanInfo(cl, Object.class);
  51         MethodDescriptor[] mds = beanInfo.getMethodDescriptors();
  52         System.out.println("mds = " + Arrays.toString(mds));
  53         loader.close();
  54         loader=null;
  55         cl=null;
  56         Util.generateOOME();
  57         mds = beanInfo.getMethodDescriptors();
  58         System.out.println("mds = " + Arrays.toString(mds));
  59     }
  60 
  61     /**
  62      * The Stub class is compiled by jtreg, but we want to move it so it is not
  63      * on the application classpath. We want to load it through a separate
  64      * classloader.
  65      */
  66     static Class<?> getStub() throws Exception {
  67         final String testclasses = System.getProperty("test.classes");
  68         final File subdir = new File(testclasses, "stub");
  69         subdir.mkdir();
  70 
  71         final Path src = Paths.get(testclasses, "Stub.class");
  72         final Path dest = subdir.toPath().resolve("Stub.class");
  73         Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING);
  74 
  75         loader = new URLClassLoader(new URL[]{subdir.toURL()});
  76         return Class.forName("Stub", true, loader);
  77     }
  78 }