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("Outer.java:3:5: compiler.warn.preview.feature.use.plural: (compiler.misc.feature.records)",
97 "Outer.java:3:5: compiler.warn.preview.feature.use.plural: (compiler.misc.feature.records)",
98 "2 warnings");
99 if (!log.equals(expected))
100 throw new Exception("expected output not found" + log);
101 checkPreviewClassfile(classes.resolve("test").resolve("Outer.class"), true); //TODO: correct?
102 checkPreviewClassfile(classes.resolve("test").resolve("Outer$R.class"),true);
103 checkPreviewClassfile(classes.resolve("test").resolve("Use.class"),false);
104 }
105
106 @Test
107 public void previewAPI(Path base) throws Exception {
108 Path apiSrc = base.resolve("api-src");
109 tb.writeJavaFiles(apiSrc,
110 """
111 package preview.api;
112 @jdk.internal.javac.PreviewFeature(feature=jdk.internal.javac.PreviewFeature.Feature.TEST)
113 public class Outer {
114 public void test() {}
115 }
116 """,
117 """
118 package preview.impl;
119 import preview.api.Outer;
120 public class Impl {
121 public void run() {
122 new Outer().test();
123 }
124 public static class C extends Outer {}
125 }
126 """);
127 Path apiClasses = base.resolve("api-classes");
128
129 new JavacTask(tb, Task.Mode.CMDLINE)
130 .outdir(apiClasses)
131 .options("-XDforcePreview",
132 "-XDrawDiagnostics",
133 "--patch-module", "java.base=" + apiSrc.toString(),
134 "-Werror")
135 .files(tb.findJavaFiles(apiSrc))
136 .run()
137 .writeAll()
138 .getOutputLines(Task.OutputKind.DIRECT);
139
140 checkPreviewClassfile(apiClasses.resolve("preview").resolve("api").resolve("Outer.class"),
141 false);
142 checkPreviewClassfile(apiClasses.resolve("preview").resolve("impl").resolve("Impl.class"),
143 false);
144 checkPreviewClassfile(apiClasses.resolve("preview").resolve("impl").resolve("Impl$C.class"),
145 false);
146
147 Path testSrc = base.resolve("test-src");
148 tb.writeJavaFiles(testSrc,
149 """
150 package test;
151 import preview.api.Outer;
152 public class Use {
153 public void run() {
154 new Outer().test();
155 }
156 public static class C extends Outer {}
157 }
158 """);
159 Path testClasses = base.resolve("test-classes");
160 List<String> log = new JavacTask(tb, Task.Mode.CMDLINE)
161 .outdir(testClasses)
162 .options("--patch-module", "java.base=" + apiClasses.toString(),
163 "--add-exports", "java.base/preview.api=ALL-UNNAMED",
164 "-XDrawDiagnostics")
165 .files(tb.findJavaFiles(testSrc))
166 .run(Task.Expect.FAIL)
167 .writeAll()
168 .getOutputLines(Task.OutputKind.DIRECT);
169
170 List<String> expected =
171 List.of("Use.java:2:19: compiler.err.is.preview: preview.api.Outer",
172 "Use.java:7:35: compiler.err.is.preview: preview.api.Outer",
173 "Use.java:5:13: compiler.err.is.preview: preview.api.Outer",
174 "3 errors");
175
176 if (!log.equals(expected))
177 throw new Exception("expected output not found" + log);
178
179 log = new JavacTask(tb, Task.Mode.CMDLINE)
180 .outdir(testClasses)
181 .options("--patch-module", "java.base=" + apiClasses.toString(),
182 "--add-exports", "java.base/preview.api=ALL-UNNAMED",
183 "--enable-preview",
184 "-Xlint:preview",
185 "-source", String.valueOf(Runtime.version().feature()),
186 "-XDrawDiagnostics")
187 .files(tb.findJavaFiles(testSrc))
188 .run()
189 .writeAll()
190 .getOutputLines(Task.OutputKind.DIRECT);
191
192 expected =
193 List.of("Use.java:7:35: compiler.warn.is.preview: preview.api.Outer",
194 "Use.java:5:13: compiler.warn.is.preview: preview.api.Outer",
195 "2 warnings");
196
197 if (!log.equals(expected))
198 throw new Exception("expected output not found" + log);
199
200 checkPreviewClassfile(testClasses.resolve("test").resolve("Use.class"),
201 true);
202 checkPreviewClassfile(testClasses.resolve("test").resolve("Use$C.class"),
203 true);
204 }
205
206 private void checkPreviewClassfile(Path p, boolean preview) throws Exception {
207 try (InputStream in = Files.newInputStream(p)) {
208 ClassModel cf = ClassFile.of().parse(in.readAllBytes());
209 if (preview && cf.minorVersion() != 65535) {
210 throw new IllegalStateException("Expected preview class, but got: " + cf.minorVersion());
211 } else if (!preview && cf.minorVersion() != 0) {
212 throw new IllegalStateException("Expected minor version == 0 but got: " + cf.minorVersion());
213 }
214 }
215 }
216 }