1 /*
  2  * Copyright (c) 2025, 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  * @test
 27  * @bug 8353298
 28  * @requires vm.cds.supports.aot.class.linking
 29  * @comment work around JDK-8345635
 30  * @requires !vm.jvmci.enabled
 31  * @library /test/lib
 32  * @build AOTCacheSupportForCustomLoaders
 33  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar AppWithCustomLoaders AppWithCustomLoaders$MyLoader
 34  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar cust.jar AppWithCustomLoaders$MyLoadeeA AppWithCustomLoaders$MyLoadeeB
 35  * @run driver AOTCacheSupportForCustomLoaders AOT
 36  */
 37 
 38 import java.net.URL;
 39 import java.net.URLClassLoader;
 40 import java.io.File;
 41 import jdk.test.lib.cds.SimpleCDSAppTester;
 42 import jdk.test.lib.process.OutputAnalyzer;
 43 
 44 public class AOTCacheSupportForCustomLoaders {
 45     public static void main(String... args) throws Exception {
 46         SimpleCDSAppTester.of("AOTCacheSupportForCustomLoaders")
 47             .classpath("app.jar")
 48             .addVmArgs("-Xlog:cds+class=debug", "-Xlog:cds")
 49             .appCommandLine("AppWithCustomLoaders")
 50             .setAssemblyChecker((OutputAnalyzer out) -> {
 51                     out.shouldMatch("cds,class.*unreg AppWithCustomLoaders[$]MyLoadeeA")
 52                        .shouldMatch("cds,class.*array \\[LAppWithCustomLoaders[$]MyLoadeeA;");
 53                 })
 54             .setProductionChecker((OutputAnalyzer out) -> {
 55                     out.shouldContain("Using AOT-linked classes: true");
 56                 })
 57             .runAOTWorkflow();
 58     }
 59 }
 60 
 61 class AppWithCustomLoaders {
 62     public static void main(String args[]) throws Exception {
 63         File custJar = new File("cust.jar");
 64         URL[] urls = new URL[] {custJar.toURI().toURL()};
 65         MyLoader loader = new MyLoader(urls, AppWithCustomLoaders.class.getClassLoader());
 66 
 67         // Test 1: array class of MyLoadeeA (JDK-8353298)
 68         Class klass = loader.loadClass("AppWithCustomLoaders$MyLoadeeA");
 69         klass.newInstance();
 70     }
 71 
 72     public static class MyLoader extends URLClassLoader {
 73         public MyLoader(URL[] urls, ClassLoader parent) {
 74             super(urls, parent);
 75         }
 76     }
 77 
 78     public static class MyLoadeeA {
 79         static Object[] array1;
 80 
 81         public MyLoadeeA() {
 82             if (array1 == null) {
 83                 test();
 84                 Object o = array1[0];
 85                 System.out.println("array1[0] is of class: " + o.getClass());
 86                 if (!(o instanceof MyLoadeeA)) {
 87                     throw new RuntimeException("array1[0] should be an instanceof MyLoadeeA");
 88                 }
 89             }
 90         }
 91 
 92         static void test() {
 93             array1 = new MyLoadeeA[10];
 94             for (int i = 0; i < 10; i++) {
 95                 if ((i % 2) == 0) {
 96                     array1[i] = new MyLoadeeB();
 97                 } else {
 98                     array1[i] = new MyLoadeeA();
 99                 }
100             }
101         }
102     }
103 
104     public static class MyLoadeeB extends MyLoadeeA {}
105 }