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.Paths; 35 import java.util.ArrayList; 36 37 public class ClassUnloadCommon { 38 public static class TestFailure extends RuntimeException { 39 TestFailure(String msg) { 40 super(msg); 41 } 42 } 43 44 public static void failIf(boolean value, String msg) { 45 if (value) throw new TestFailure("Test failed: " + msg); 46 } 47 48 private static volatile Object dummy = null; 49 private static void allocateMemory(int kilobytes) { 50 ArrayList<byte[]> l = new ArrayList<>(); 51 dummy = l; 52 for (int i = kilobytes; i > 0; i -= 1) { 53 l.add(new byte[1024]); 54 } 55 l = null; 56 dummy = null; 57 } 58 59 public static void triggerUnloading() { 60 allocateMemory(16 * 1024); // yg size is 8m with cms, force young collection 61 System.gc(); 62 } 63 64 public static ClassLoader newClassLoader() { 65 try { 66 return new URLClassLoader(new URL[] { 67 Paths.get(System.getProperty("test.classes",".") + File.separatorChar + "classes").toUri().toURL(), 68 }, null); 69 } catch (MalformedURLException e){ 70 throw new RuntimeException("Unexpected URL conversion failure", e); 71 } 72 } 73 74 } | 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 } |