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