1 /*
   2  * Copyright (c) 2013, 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 
  25 /*
  26  * To use ClassUnloadCommon from a sub-process, see hotspot/test/runtime/logging/ClassLoadUnloadTest.java
  27  * for an example.
  28  */
  29 
  30 import java.io.File;
  31 import java.net.MalformedURLException;
  32 import java.net.URL;
  33 import java.net.URLClassLoader;
  34 import java.nio.file.Path;
  35 import java.nio.file.Paths;
  36 import java.util.ArrayList;
  37 import java.util.stream.Stream;
  38 
  39 public class ClassUnloadCommon {
  40     public static class TestFailure extends RuntimeException {
  41         TestFailure(String msg) {
  42             super(msg);
  43         }
  44     }
  45 
  46     public static void failIf(boolean value, String msg) {
  47         if (value) throw new TestFailure("Test failed: " + msg);
  48     }
  49 
  50     private static volatile Object dummy = null;
  51     private static void allocateMemory(int kilobytes) {
  52         ArrayList<byte[]> l = new ArrayList<>();
  53         dummy = l;
  54         for (int i = kilobytes; i > 0; i -= 1) {
  55             l.add(new byte[1024]);
  56         }
  57         l = null;
  58         dummy = null;
  59     }
  60 
  61     public static void triggerUnloading() {
  62         allocateMemory(16 * 1024); // yg size is 8m with cms, force young collection
  63         System.gc();
  64     }
  65 
  66     /**
  67      * Creates a class loader that loads classes from {@code ${test.class.path}}
  68      * before delegating to the system class loader.
  69      */
  70     public static ClassLoader newClassLoader() {
  71         String cp = System.getProperty("test.class.path", ".");
  72         URL[] urls = Stream.of(cp.split(File.pathSeparator))
  73                 .map(Paths::get)
  74                 .map(ClassUnloadCommon::toURL)
  75                 .toArray(URL[]::new);
  76         return new URLClassLoader(urls) {
  77             @Override
  78             public Class<?> loadClass(String cn, boolean resolve)
  79                 throws ClassNotFoundException
  80             {
  81                 synchronized (getClassLoadingLock(cn)) {
  82                     Class<?> c = findLoadedClass(cn);
  83                     if (c == null) {
  84                         try {
  85                             c = findClass(cn);
  86                         } catch (ClassNotFoundException e) {
  87                             c = getParent().loadClass(cn);
  88                         }
  89 
  90                     }
  91                     if (resolve) {
  92                         resolveClass(c);
  93                     }
  94                     return c;
  95                 }
  96             }
  97         };
  98     }
  99 
 100     static URL toURL(Path path) {
 101         try {
 102             return path.toUri().toURL();
 103         } catch (MalformedURLException e) {
 104             throw new RuntimeException(e);
 105         }
 106     }
 107 }