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