|
23 | 23 |
|
24 | 24 | /**
|
25 | 25 | * @test
|
26 |
| - * @bug 8291769 |
| 26 | + * @bug 8291769 8326129 |
27 | 27 | * @summary Check expected translation of various pattern related constructs
|
28 | 28 | * @library /tools/lib
|
29 | 29 | * @modules jdk.compiler/com.sun.tools.javac.api
|
30 | 30 | * jdk.compiler/com.sun.tools.javac.comp
|
31 | 31 | * jdk.compiler/com.sun.tools.javac.main
|
32 | 32 | * jdk.compiler/com.sun.tools.javac.tree
|
33 | 33 | * jdk.compiler/com.sun.tools.javac.util
|
34 |
| - * @build toolbox.ToolBox toolbox.JavacTask |
| 34 | + * @build toolbox.ToolBox toolbox.JavacTask toolbox.JavaTask |
35 | 35 | * @run main TranslationTest
|
36 | 36 | */
|
37 | 37 |
|
|
48 | 48 | import com.sun.tools.javac.tree.TreeScanner;
|
49 | 49 | import com.sun.tools.javac.util.Context;
|
50 | 50 | import com.sun.tools.javac.util.Context.Factory;
|
| 51 | +import java.io.File; |
51 | 52 | import java.io.IOException;
|
52 | 53 | import java.nio.file.Files;
|
53 | 54 | import java.nio.file.Path;
|
|
56 | 57 | import java.util.List;
|
57 | 58 |
|
58 | 59 | import toolbox.TestRunner;
|
| 60 | +import toolbox.JavaTask; |
59 | 61 | import toolbox.JavacTask;
|
60 | 62 | import toolbox.Task;
|
61 | 63 | import toolbox.ToolBox;
|
@@ -188,6 +190,38 @@ private int test(Object obj) {
|
188 | 190 | """);
|
189 | 191 | }
|
190 | 192 |
|
| 193 | + @Test //JDK-8326129 |
| 194 | + public void testRunWithNull(Path base) throws Exception { |
| 195 | + doRunTest(base, |
| 196 | + new String[]{""" |
| 197 | + package lib; |
| 198 | + public record Box(Object o) {} |
| 199 | + """}, |
| 200 | + """ |
| 201 | + import lib.*; |
| 202 | + public class Test { |
| 203 | + public static void main(String... args) { |
| 204 | + System.err.println(new Test().test(new Box(null))); |
| 205 | + } |
| 206 | + private int test(Box b) { |
| 207 | + return switch (b) { |
| 208 | + case Box(Integer i) -> 0; |
| 209 | + case Box(Object o) when check(o) -> 1; |
| 210 | + case Box(Object o) -> 2; |
| 211 | + }; |
| 212 | + } |
| 213 | + private static int c; |
| 214 | + private boolean check(Object o) { |
| 215 | + System.err.println("check: " + o); |
| 216 | + if (c++ > 10) throw new IllegalStateException(); |
| 217 | + return o != null; |
| 218 | + } |
| 219 | + } |
| 220 | + """, |
| 221 | + "check: null", |
| 222 | + "2"); |
| 223 | + } |
| 224 | + |
191 | 225 | private void doTest(Path base, String[] libraryCode, String testCode,
|
192 | 226 | Callback callback, String expectedOutput) throws IOException {
|
193 | 227 | Path current = base.resolve(".");
|
@@ -322,4 +356,51 @@ public JCTree translateTopLevelClass(Env<AttrContext> env, JCTree cdef, TreeMake
|
322 | 356 | }
|
323 | 357 |
|
324 | 358 | }
|
| 359 | + |
| 360 | + private void doRunTest(Path base, String[] libraryCode, String testCode, |
| 361 | + String... expectedOutput) throws IOException { |
| 362 | + Path current = base.resolve("."); |
| 363 | + Path libClasses = current.resolve("libClasses"); |
| 364 | + |
| 365 | + Files.createDirectories(libClasses); |
| 366 | + |
| 367 | + if (libraryCode.length != 0) { |
| 368 | + Path libSrc = current.resolve("lib-src"); |
| 369 | + |
| 370 | + for (String code : libraryCode) { |
| 371 | + tb.writeJavaFiles(libSrc, code); |
| 372 | + } |
| 373 | + |
| 374 | + new JavacTask(tb) |
| 375 | + .outdir(libClasses) |
| 376 | + .files(tb.findJavaFiles(libSrc)) |
| 377 | + .run(); |
| 378 | + } |
| 379 | + |
| 380 | + Path src = current.resolve("src"); |
| 381 | + tb.writeJavaFiles(src, testCode); |
| 382 | + |
| 383 | + Path classes = current.resolve("classes"); |
| 384 | + |
| 385 | + Files.createDirectories(classes); |
| 386 | + |
| 387 | + new JavacTask(tb) |
| 388 | + .options("-Xlint:-preview", |
| 389 | + "--class-path", libClasses.toString()) |
| 390 | + .outdir(classes) |
| 391 | + .files(tb.findJavaFiles(src)) |
| 392 | + .run() |
| 393 | + .writeAll(); |
| 394 | + |
| 395 | + List<String> log = new JavaTask(tb) |
| 396 | + .classpath(libClasses.toString() + File.pathSeparatorChar + classes.toString()) |
| 397 | + .classArgs("Test") |
| 398 | + .run() |
| 399 | + .getOutputLines(Task.OutputKind.STDERR); |
| 400 | + |
| 401 | + if (!List.of(expectedOutput).equals(log)) { |
| 402 | + throw new AssertionError("Expected: " + expectedOutput + |
| 403 | + "but got: " + log); |
| 404 | + } |
| 405 | + } |
325 | 406 | }
|
0 commit comments