1 /*
2 * Copyright (c) 2021, 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 * @test
26 * @bug 8250768
27 * @library /tools/lib
28 * @modules
29 * jdk.compiler/com.sun.tools.javac.api
30 * jdk.compiler/com.sun.tools.javac.main
31 * @build toolbox.ToolBox toolbox.JavacTask
32 * @run main PreviewAutoSuppress
33 */
34 import java.lang.classfile.*;
35 import java.io.InputStream;
36 import java.nio.file.Files;
37 import toolbox.JavacTask;
38 import toolbox.Task;
39 import toolbox.TestRunner;
40 import toolbox.ToolBox;
41
42 import java.nio.file.Path;
43 import java.nio.file.Paths;
44 import java.util.List;
45
46 public class PreviewAutoSuppress extends TestRunner {
47 // Major version number (e.g. '27').
48 private static final String FEATURE_VERSION = String.valueOf(Runtime.version().feature());
49
50 protected ToolBox tb;
51
52 PreviewAutoSuppress() {
53 super(System.err);
54 tb = new ToolBox();
55 }
56
57 public static void main(String... args) throws Exception {
58 PreviewAutoSuppress t = new PreviewAutoSuppress();
59 t.runTests();
60 }
61
62 protected void runTests() throws Exception {
63 runTests(m -> new Object[] { Paths.get(m.getName()) });
64 }
65
66 @Test
67 public void declarationWarning(Path base) throws Exception {
68 Path src = base.resolve("src");
69 tb.writeJavaFiles(src,
70 """
71 package test;
72 public class Outer {
73 record R(int i) {}
74 R r;
75 }
76 """,
77 """
78 package test;
79 public class Use {
80 Outer.R r;
81 }
82 """);
83 Path classes = base.resolve("classes");
84
85 List<String> log = new JavacTask(tb, Task.Mode.CMDLINE)
86 .outdir(classes)
87 .options("--enable-preview",
88 "-source", FEATURE_VERSION,
89 "-Xlint:preview",
90 "-XDforcePreview",
91 "-XDrawDiagnostics")
92 .files(tb.findJavaFiles(src))
93 .run()
94 .writeAll()
95 .getOutputLines(Task.OutputKind.DIRECT);
96
97 // As of Valhalla, j.l.Record is a preview class
98 List<String> expected =
99 List.of("- compiler.warn.preview.feature.use.classfile: Record.class, " + FEATURE_VERSION,
100 "Outer.java:3:5: compiler.warn.preview.feature.use.plural: (compiler.misc.feature.records)",
101 "Outer.java:3:5: compiler.warn.preview.feature.use.plural: (compiler.misc.feature.records)",
102 "3 warnings");
103 if (!log.equals(expected))
104 throw new Exception("expected output not found" + log);
105 checkPreviewClassfile(classes.resolve("test").resolve("Outer.class"), true); //TODO: correct?
106 checkPreviewClassfile(classes.resolve("test").resolve("Outer$R.class"),true);
107 checkPreviewClassfile(classes.resolve("test").resolve("Use.class"),false);
108 }
109
110 @Test
111 public void previewAPI(Path base) throws Exception {
112 Path apiSrc = base.resolve("api-src");
113 tb.writeJavaFiles(apiSrc,
114 """
115 package preview.api;
116 @jdk.internal.javac.PreviewFeature(feature=jdk.internal.javac.PreviewFeature.Feature.TEST)
117 public class Outer {
118 public void test() {}
119 }
120 """,
121 """
122 package preview.impl;
123 import preview.api.Outer;
124 public class Impl {
125 public void run() {
126 new Outer().test();
127 }
128 public static class C extends Outer {}
129 }
130 """);
131 Path apiClasses = base.resolve("api-classes");
132
133 new JavacTask(tb, Task.Mode.CMDLINE)
134 .outdir(apiClasses)
135 .options("-XDforcePreview",
136 "-XDrawDiagnostics",
137 "--patch-module", "java.base=" + apiSrc.toString(),
138 "-Werror")
139 .files(tb.findJavaFiles(apiSrc))
140 .run()
141 .writeAll()
142 .getOutputLines(Task.OutputKind.DIRECT);
143
144 checkPreviewClassfile(apiClasses.resolve("preview").resolve("api").resolve("Outer.class"),
145 false);
146 checkPreviewClassfile(apiClasses.resolve("preview").resolve("impl").resolve("Impl.class"),
147 false);
148 checkPreviewClassfile(apiClasses.resolve("preview").resolve("impl").resolve("Impl$C.class"),
149 false);
150
151 Path testSrc = base.resolve("test-src");
152 tb.writeJavaFiles(testSrc,
153 """
154 package test;
155 import preview.api.Outer;
156 public class Use {
157 public void run() {
158 new Outer().test();
159 }
160 public static class C extends Outer {}
161 }
162 """);
163 Path testClasses = base.resolve("test-classes");
164 List<String> log = new JavacTask(tb, Task.Mode.CMDLINE)
165 .outdir(testClasses)
166 .options("--patch-module", "java.base=" + apiClasses.toString(),
167 "--add-exports", "java.base/preview.api=ALL-UNNAMED",
168 "-XDrawDiagnostics")
169 .files(tb.findJavaFiles(testSrc))
170 .run(Task.Expect.FAIL)
171 .writeAll()
172 .getOutputLines(Task.OutputKind.DIRECT);
173
174 List<String> expected =
175 List.of("Use.java:2:19: compiler.err.is.preview: preview.api.Outer",
176 "Use.java:7:35: compiler.err.is.preview: preview.api.Outer",
177 "Use.java:5:13: compiler.err.is.preview: preview.api.Outer",
178 "3 errors");
179
180 if (!log.equals(expected))
181 throw new Exception("expected output not found" + log);
182
183 log = new JavacTask(tb, Task.Mode.CMDLINE)
184 .outdir(testClasses)
185 .options("--patch-module", "java.base=" + apiClasses.toString(),
186 "--add-exports", "java.base/preview.api=ALL-UNNAMED",
187 "--enable-preview",
188 "-Xlint:preview",
189 "-source", FEATURE_VERSION,
190 "-XDrawDiagnostics")
191 .files(tb.findJavaFiles(testSrc))
192 .run()
193 .writeAll()
194 .getOutputLines(Task.OutputKind.DIRECT);
195
196 expected =
197 List.of("Use.java:7:35: compiler.warn.is.preview: preview.api.Outer",
198 "Use.java:5:13: compiler.warn.is.preview: preview.api.Outer",
199 "2 warnings");
200
201 if (!log.equals(expected))
202 throw new Exception("expected output not found" + log);
203
204 checkPreviewClassfile(testClasses.resolve("test").resolve("Use.class"),
205 true);
206 checkPreviewClassfile(testClasses.resolve("test").resolve("Use$C.class"),
207 true);
208 }
209
210 private void checkPreviewClassfile(Path p, boolean preview) throws Exception {
211 try (InputStream in = Files.newInputStream(p)) {
212 ClassModel cf = ClassFile.of().parse(in.readAllBytes());
213 if (preview && cf.minorVersion() != 65535) {
214 throw new IllegalStateException("Expected preview class, but got: " + cf.minorVersion());
215 } else if (!preview && cf.minorVersion() != 0) {
216 throw new IllegalStateException("Expected minor version == 0 but got: " + cf.minorVersion());
217 }
218 }
219 }
220 }