61 /*
62 * @test id=aot
63 * @requires vm.cds.supports.aot.class.linking
64 * @comment work around JDK-8345635
65 * @requires !vm.jvmci.enabled
66 * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes
67 * @build jdk.test.whitebox.WhiteBox InitiatingLoaderTester BadOldClassA BadOldClassB
68 * @build BulkLoaderTest SimpleCusty
69 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar BulkLoaderTestApp.jar BulkLoaderTestApp MyUtil InitiatingLoaderTester
70 * BadOldClassA BadOldClassB
71 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar WhiteBox.jar jdk.test.whitebox.WhiteBox
72 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar
73 * SimpleCusty
74 * @run driver BulkLoaderTest AOT
75 */
76
77 import java.io.File;
78 import java.lang.StackWalker.StackFrame;
79 import java.net.URL;
80 import java.net.URLClassLoader;
81 import java.util.List;
82 import java.util.regex.Matcher;
83 import java.util.regex.Pattern;
84 import java.util.stream.Collectors;
85 import java.util.Set;
86 import jdk.test.lib.cds.CDSAppTester;
87 import jdk.test.lib.helpers.ClassFileInstaller;
88 import jdk.test.lib.process.OutputAnalyzer;
89 import jdk.test.whitebox.WhiteBox;
90
91 public class BulkLoaderTest {
92 static final String appJar = ClassFileInstaller.getJarPath("BulkLoaderTestApp.jar");
93 static final String mainClass = "BulkLoaderTestApp";
94
95 public static void main(String[] args) throws Exception {
96 Tester t = new Tester();
97
98 // Run with archived FMG loaded
99 t.run(args);
100
301 WhiteBox wb = WhiteBox.getWhiteBox();
302 for (int i = 0; i < 2; i++) {
303 Object o = initFromCustomLoader();
304 System.out.println(o);
305 Class c = o.getClass();
306 if (wb.isSharedClass(BulkLoaderTestApp.class)) {
307 // We are running with BulkLoaderTestApp from the AOT cache (or CDS achive)
308 if (i == 0) {
309 if (!wb.isSharedClass(c)) {
310 throw new RuntimeException("The first loader should load SimpleCusty from AOT cache (or CDS achive)");
311 }
312 } else {
313 if (wb.isSharedClass(c)) {
314 throw new RuntimeException("The second loader should not load SimpleCusty from AOT cache (or CDS achive)");
315 }
316 }
317 }
318 }
319 }
320
321 static Object initFromCustomLoader() throws Exception {
322 String path = "cust.jar";
323 URL url = new File(path).toURI().toURL();
324 URL[] urls = new URL[] {url};
325 URLClassLoader urlClassLoader =
326 new URLClassLoader("MyLoader", urls, null);
327 Class c = Class.forName("SimpleCusty", true, urlClassLoader);
328 return c.newInstance();
329 }
330 }
331
332 class MyUtil {
333 // depth is 0-based -- i.e., depth==0 returns the class of the immediate caller of getCallerClass
334 static Class<?> getCallerClass(int depth) {
335 // Need to add the frame of the getCallerClass -- so the immediate caller (depth==0) of this method
336 // is at stack.get(1) == stack.get(depth+1);
337 StackWalker walker = StackWalker.getInstance(
338 Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE,
339 StackWalker.Option.SHOW_HIDDEN_FRAMES));
340 List<StackFrame> stack = walker.walk(s -> s.limit(depth+2).collect(Collectors.toList()));
341 return stack.get(depth+1).getDeclaringClass();
342 }
343 }
|
61 /*
62 * @test id=aot
63 * @requires vm.cds.supports.aot.class.linking
64 * @comment work around JDK-8345635
65 * @requires !vm.jvmci.enabled
66 * @library /test/jdk/lib/testlibrary /test/lib /test/hotspot/jtreg/runtime/cds/appcds/test-classes
67 * @build jdk.test.whitebox.WhiteBox InitiatingLoaderTester BadOldClassA BadOldClassB
68 * @build BulkLoaderTest SimpleCusty
69 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar BulkLoaderTestApp.jar BulkLoaderTestApp MyUtil InitiatingLoaderTester
70 * BadOldClassA BadOldClassB
71 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar WhiteBox.jar jdk.test.whitebox.WhiteBox
72 * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar
73 * SimpleCusty
74 * @run driver BulkLoaderTest AOT
75 */
76
77 import java.io.File;
78 import java.lang.StackWalker.StackFrame;
79 import java.net.URL;
80 import java.net.URLClassLoader;
81 import java.util.ArrayList;
82 import java.util.List;
83 import java.util.regex.Matcher;
84 import java.util.regex.Pattern;
85 import java.util.stream.Collectors;
86 import java.util.Set;
87 import jdk.test.lib.cds.CDSAppTester;
88 import jdk.test.lib.helpers.ClassFileInstaller;
89 import jdk.test.lib.process.OutputAnalyzer;
90 import jdk.test.whitebox.WhiteBox;
91
92 public class BulkLoaderTest {
93 static final String appJar = ClassFileInstaller.getJarPath("BulkLoaderTestApp.jar");
94 static final String mainClass = "BulkLoaderTestApp";
95
96 public static void main(String[] args) throws Exception {
97 Tester t = new Tester();
98
99 // Run with archived FMG loaded
100 t.run(args);
101
302 WhiteBox wb = WhiteBox.getWhiteBox();
303 for (int i = 0; i < 2; i++) {
304 Object o = initFromCustomLoader();
305 System.out.println(o);
306 Class c = o.getClass();
307 if (wb.isSharedClass(BulkLoaderTestApp.class)) {
308 // We are running with BulkLoaderTestApp from the AOT cache (or CDS achive)
309 if (i == 0) {
310 if (!wb.isSharedClass(c)) {
311 throw new RuntimeException("The first loader should load SimpleCusty from AOT cache (or CDS achive)");
312 }
313 } else {
314 if (wb.isSharedClass(c)) {
315 throw new RuntimeException("The second loader should not load SimpleCusty from AOT cache (or CDS achive)");
316 }
317 }
318 }
319 }
320 }
321
322 static ArrayList<ClassLoader> savedLoaders = new ArrayList<>();
323
324 static Object initFromCustomLoader() throws Exception {
325 String path = "cust.jar";
326 URL url = new File(path).toURI().toURL();
327 URL[] urls = new URL[] {url};
328 URLClassLoader urlClassLoader =
329 new URLClassLoader("MyLoader", urls, null);
330 savedLoaders.add(urlClassLoader);
331 Class c = Class.forName("SimpleCusty", true, urlClassLoader);
332 return c.newInstance();
333 }
334 }
335
336 class MyUtil {
337 // depth is 0-based -- i.e., depth==0 returns the class of the immediate caller of getCallerClass
338 static Class<?> getCallerClass(int depth) {
339 // Need to add the frame of the getCallerClass -- so the immediate caller (depth==0) of this method
340 // is at stack.get(1) == stack.get(depth+1);
341 StackWalker walker = StackWalker.getInstance(
342 Set.of(StackWalker.Option.RETAIN_CLASS_REFERENCE,
343 StackWalker.Option.SHOW_HIDDEN_FRAMES));
344 List<StackFrame> stack = walker.walk(s -> s.limit(depth+2).collect(Collectors.toList()));
345 return stack.get(depth+1).getDeclaringClass();
346 }
347 }
|