1 /*
 2  * Copyright (c) 2026, 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 id=inline
27  * @bug 8384756
28  * @summary A class should be excluded if the type of one of its inlined fields is excluded.
29  * @requires vm.cds
30  * @library /test/lib
31  * @modules java.base/jdk.internal.vm.annotation
32  * @enablePreview
33  * @compile test-classes/InlineFieldExclusionApp.java test-classes/excluded/ExcludedValueClass.java
34  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar InlineFieldExclusionApp
35  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar excluded.jar excluded/ExcludedValueClass
36  * @run main/othervm InlineFieldExclusionTest InlineFieldExclusionApp
37  */
38 
39 /* @test id=static
40  * @bug 8384756
41  * @summary A class should be excluded if the type of one of its inlined fields is excluded.
42  * @requires vm.cds
43  * @library /test/lib
44  * @modules java.base/jdk.internal.vm.annotation
45  * @enablePreview
46  * @compile test-classes/StaticNullRestrictedExclusionApp.java test-classes/excluded/ExcludedValueClass.java
47  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar app.jar StaticNullRestrictedExclusionApp
48  * @run driver jdk.test.lib.helpers.ClassFileInstaller -jar excluded.jar excluded/ExcludedValueClass
49  * @run main/othervm InlineFieldExclusionTest StaticNullRestrictedExclusionApp
50  */
51 
52 import jdk.test.lib.helpers.ClassFileInstaller;
53 import jdk.test.lib.process.OutputAnalyzer;
54 import java.io.File;
55 
56 public class InlineFieldExclusionTest {
57     public static void main(String[] args) throws Exception {
58         if (args.length < 1) {
59             throw new RuntimeException("Missing main class name");
60         }
61 
62         String mainClass = args[0];
63         String appJar = ClassFileInstaller.getJarPath("app.jar");
64         String unsignedJar = ClassFileInstaller.getJarPath("excluded.jar");
65 
66         JarBuilder.signJar("excluded");
67         String signedJar = TestCommon.getTestJar("signed_excluded.jar");
68 
69         String classpath = appJar + System.getProperty("path.separator") + signedJar;
70         OutputAnalyzer output;
71 
72         String[] suffix = TestCommon.list("--enable-preview",
73                                           "-Xlog:cds,aot,class+load");
74 
75         String skipMsg1 = "Skipping excluded/ExcludedValueClass: Signed JAR";
76         String skipMsg2 = "Skipping " + mainClass;
77         String loadFromJar = ".class,load\s*. " + mainClass + " source: file:.*app.jar";
78 
79         // ExcludedValueClass should be excluded from the archive because it is found inside
80         // a signed JAR. InlineFieldExclusionApp has a flat field of type ExcludedValueClass and
81         // relies on this class to calculate the layout of the field, so it too must be excluded
82         // from the archive.
83         output = TestCommon.dump(classpath, TestCommon.list(mainClass, "excluded/ExcludedValueClass"), suffix);
84         TestCommon.checkDump(output, skipMsg1, skipMsg2);
85 
86         // At runtime, both InlineFieldExclusionApp and ExcludedValueClass should be loaded from
87         // the jar file instead of from the shared archive.
88         output = TestCommon.exec(classpath, TestCommon.concat(suffix, mainClass));
89         try {
90             output.shouldMatch(loadFromJar);
91         } catch (Exception e) {
92             TestCommon.checkCommonExecExceptions(output, e);
93         }
94     }
95 }