1 /* 2 * Copyright (c) 2018, 2024, 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 8202113 27 * @summary Test the caller class loader is not kept strongly reachable 28 * by reflection API 29 * @library /test/lib/ 30 * @requires vm.compMode != "Xcomp" 31 * @modules jdk.compiler 32 * @build ReflectionCallerCacheTest Members jdk.test.lib.compiler.CompilerUtils 33 * @run testng/othervm ReflectionCallerCacheTest 34 */ 35 36 import java.io.IOException; 37 import java.lang.ref.WeakReference; 38 import java.lang.reflect.*; 39 import java.net.MalformedURLException; 40 import java.net.URL; 41 import java.net.URLClassLoader; 42 import java.nio.file.Path; 43 import java.nio.file.Paths; 44 import java.util.concurrent.Callable; 45 46 import jdk.test.lib.compiler.CompilerUtils; 47 import jdk.test.lib.util.ForceGC; 48 import org.testng.annotations.BeforeTest; 49 import org.testng.annotations.DataProvider; 50 import org.testng.annotations.Test; 51 52 public class ReflectionCallerCacheTest { 53 private static final Path CLASSES = Paths.get("classes"); 54 private static final ReflectionCallerCacheTest TEST = new ReflectionCallerCacheTest(); 55 56 @BeforeTest 57 public void setup() throws IOException { 58 String src = System.getProperty("test.src", "."); 59 String classpath = System.getProperty("test.classes", "."); 60 boolean rc = CompilerUtils.compile(Paths.get(src, "AccessTest.java"), CLASSES, "-cp", classpath); 61 if (!rc) { 62 throw new RuntimeException("fail compilation"); 63 } 64 } 65 @DataProvider(name = "memberAccess") 66 public Object[][] memberAccess() { 67 return new Object[][] { 68 { "AccessTest$PublicConstructor" }, 69 { "AccessTest$PublicMethod" }, 70 { "AccessTest$PublicField" }, 71 { "AccessTest$ProtectedMethod" }, 72 { "AccessTest$ProtectedField" }, 73 { "AccessTest$PrivateMethod" }, 74 { "AccessTest$PrivateField"}, 75 { "AccessTest$PublicFinalField"}, 76 { "AccessTest$PrivateFinalField"}, 77 { "AccessTest$PublicStaticFinalField"}, 78 { "AccessTest$PrivateStaticFinalField"}, 79 { "AccessTest$NewInstance"} 80 }; 81 } 82 83 // Keep the root of the reflective objects strongly reachable 84 private final Constructor<?> publicConstructor; 85 private final Method publicMethod; 86 private final Method protectedMethod; 87 private final Method privateMethod; 88 private final Field publicField; 89 private final Field protectedField; 90 private final Field privateField; 91 92 ReflectionCallerCacheTest() { 93 try { 94 this.publicConstructor = Members.class.getConstructor(); 95 this.publicMethod = Members.class.getDeclaredMethod("publicMethod"); 96 this.publicField = Members.class.getDeclaredField("publicField"); 97 this.protectedMethod = Members.class.getDeclaredMethod("protectedMethod"); 98 this.protectedField = Members.class.getDeclaredField("protectedField"); 99 this.privateMethod = Members.class.getDeclaredMethod("privateMethod"); 100 this.privateField = Members.class.getDeclaredField("privateField"); 101 } catch (ReflectiveOperationException e) { 102 throw new RuntimeException(e); 103 } 104 } 105 106 @Test(dataProvider = "memberAccess") 107 private void load(String classname) throws Exception { 108 WeakReference<?> weakLoader = loadAndRunClass(classname); 109 110 // Force garbage collection to trigger unloading of class loader 111 if (!ForceGC.wait(() -> weakLoader.refersTo(null))) { 112 throw new RuntimeException("Class " + classname + " not unloaded!"); 113 } 114 } 115 116 private WeakReference<?> loadAndRunClass(String classname) throws Exception { 117 try (TestLoader loader = new TestLoader()) { 118 // Load member access class with custom class loader 119 Class<?> c = Class.forName(classname, true, loader); 120 // access the reflective member 121 Callable callable = (Callable) c.newInstance(); 122 callable.call(); 123 return new WeakReference<>(loader); 124 } 125 } 126 127 static class TestLoader extends URLClassLoader { 128 static URL[] toURLs() { 129 try { 130 return new URL[] { CLASSES.toUri().toURL() }; 131 } catch (MalformedURLException e) { 132 throw new Error(e); 133 } 134 } 135 136 TestLoader() { 137 super("testloader", toURLs(), ClassLoader.getSystemClassLoader()); 138 } 139 } 140 }