1 /*
 2  * Copyright (c) 2024, 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  * @test
26  * @bug 8331462
27  * @summary [lworld] javac is setting the ACC_STRICT flag for value-based classes
28  * @library /tools/lib
29  * @modules
30  *      jdk.compiler/com.sun.tools.javac.code
31  *      jdk.compiler/com.sun.tools.javac.util
32  *      jdk.compiler/com.sun.tools.javac.api
33  *      jdk.compiler/com.sun.tools.javac.file
34  *      jdk.compiler/com.sun.tools.javac.main
35  * @build toolbox.ToolBox toolbox.JavacTask
36  * @run main ValueBasedFlagsTest
37  */
38 
39 import java.lang.classfile.ClassFile;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 
43 import com.sun.tools.javac.code.Flags;
44 import com.sun.tools.javac.util.Assert;
45 
46 import toolbox.TestRunner;
47 import toolbox.ToolBox;
48 import toolbox.JavacTask;
49 import toolbox.Task;
50 
51 public class ValueBasedFlagsTest extends TestRunner {
52     ToolBox tb = new ToolBox();
53 
54     public ValueBasedFlagsTest() {
55         super(System.err);
56     }
57 
58     protected void runTests() throws Exception {
59         runTests(m -> new Object[] { Paths.get(m.getName()) });
60     }
61 
62     Path[] findJavaFiles(Path... paths) throws Exception {
63         return tb.findJavaFiles(paths);
64     }
65 
66     public static void main(String... args) throws Exception {
67         new ValueBasedFlagsTest().runTests();
68     }
69 
70     @Test
71     public void testValueBased(Path base) throws Exception {
72         Path src = base.resolve("src");
73         tb.writeJavaFiles(src,
74                 """
75                 package java.lang;
76                 @jdk.internal.ValueBased
77                 final class ValueBasedTest {}
78                 """);
79         Path classes = base.resolve("classes");
80         tb.createDirectories(classes);
81 
82         new toolbox.JavacTask(tb)
83                 .options("--patch-module", "java.base=" + src.toString(),
84                         "--enable-preview", "-source", Integer.toString(Runtime.version().feature()))
85                 .outdir(classes)
86                 .files(findJavaFiles(src))
87                 .run()
88                 .writeAll();
89         Path classFilePath = classes.resolve("java", "lang", "ValueBasedTest.class");
90         var classFile = ClassFile.of().parse(classFilePath);
91         Assert.check((classFile.flags().flagsMask() & Flags.ACC_STRICT) == 0);
92     }
93 }