< prev index next >

test/langtools/tools/javac/preview/PreviewAutoSuppress.java

Print this page

  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  */

 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;

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 

  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  */

 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;

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 
< prev index next >