From 10e8435db6aeec8579ba4dc70f87920852161e78 Mon Sep 17 00:00:00 2001 From: Niels de Bruin Date: Mon, 11 Nov 2024 14:46:39 +0100 Subject: [PATCH 1/3] Add test for parsing JEP440 --- .../openrewrite/java/Java21ParserTest.java | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java b/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java index d5255503810..9b8719c8273 100644 --- a/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java +++ b/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java @@ -23,6 +23,8 @@ import java.nio.file.Files; import java.nio.file.Paths; +import static org.openrewrite.java.Assertions.java; + class Java21ParserTest implements RewriteTest { @Test @@ -30,4 +32,65 @@ void shouldLoadResourceFromClasspath() throws IOException { Files.deleteIfExists(Paths.get(System.getProperty("user.home"), ".rewrite", "classpath", "jackson-annotations-2.17.1.jar")); rewriteRun(spec -> spec.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), "jackson-annotations"))); } + + @Test + void shouldParseJava21PatternSwitch() { + rewriteRun( + java( + //language=java + """ + class Test { + String formatterPatternSwitch(Object obj) { + return switch (obj) { + case Integer i -> String.format("int %d", i); + case Long l -> String.format("long %d", l); + case Double d -> String.format("double %f", d); + case String s -> String.format("String %s", s); + default -> obj.toString(); + }; + } + } + """ + )); + } + + @Test + void shouldParseJava21PatternMatchForRecords() { + rewriteRun( + java( + //language=java + """ + record Point(int x, int y) {} + class Test { + void printSum(Object obj) { + if (obj instanceof Point(int x, int y)) { + System.out.println(x+y); + } + } + } + """ + )); + } + + @Test + void shouldParseJava21NestedPatternMatchForRecords() { + rewriteRun( + java( + //language=java + """ + record Point(int x, int y) {} + enum Color { RED, GREEN, BLUE } + record ColoredPoint(Point p, Color c) {} + record Rectangle(ColoredPoint upperLeft, ColoredPoint lowerRight) {} + class Test { + void printColorOfUpperLeftPoint(Rectangle r) { + if (r instanceof Rectangle(ColoredPoint(Point p, Color c), + ColoredPoint lr)) { + System.out.println(c); + } + } + } + """ + )); + } } From 87bfce99fe074be3bd4652c3a92f3a9dc3779bc0 Mon Sep 17 00:00:00 2001 From: Niels de Bruin Date: Mon, 11 Nov 2024 16:14:46 +0100 Subject: [PATCH 2/3] Add test for parsing JEP441 --- .../openrewrite/java/Java21ParserTest.java | 108 ++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java b/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java index 9b8719c8273..aa2eab8ffee 100644 --- a/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java +++ b/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java @@ -93,4 +93,112 @@ void printColorOfUpperLeftPoint(Rectangle r) { """ )); } + + @Test + void shouldSupportParsingNullSwitch() { + rewriteRun( + java( + //language=java + """ + class Test { + void fooBarWithNull(String s) { + switch (s) { + case null -> System.out.println("Oops"); + case "Foo", "Bar" -> System.out.println("Great"); + default -> System.out.println("Ok"); + } + } + } + """ + )); + } + + @Test + void shouldParseJava21EnumSupportInSwitch() { + rewriteRun( + java( + //language=java + """ + sealed interface Currency permits Coin {} + enum Coin implements Currency { HEADS, TAILS } + + class Test { + void switchEnum(Coin c) { + switch (c) { + case HEADS -> System.out.println("Heads"); + case Coin.TAILS -> System.out.println("Tails"); + } + } + } + """ + ) + ); + } + + @Test + void shouldParseJava21ImprovedEnumSupportInSwitch() { + rewriteRun( + java( + //language=java + """ + sealed interface I permits Foo, Bar {} + public enum Foo implements I { A, B } + final class Bar implements I {} + + class Test { + void switchEnumExtendedType(I c) { + switch (c) { + case Foo.CLUBS -> System.out.println("It's Foo A"); + case Foo.DIAMONDS -> System.out.println("It's Foo B"); + case Bar b -> System.out.println("It's Bar"); + } + } + } + """ + )); + } + + @Test + void shouldParseJava21SwitchWithRelaxedTypeRestrictions() { + rewriteRun( + java( + //language=java + """ + record Point(int i, int j) {} + enum Color { RED, GREEN, BLUE; } + + class Test { + void typeTester(Object obj) { + switch (obj) { + case null -> System.out.println("null"); + case String s -> System.out.println("String"); + case Color c -> System.out.println("Color: " + c.toString()); + case Point p -> System.out.println("Record class: " + p.toString()); + case int[] ia -> System.out.println("Array of ints of length" + ia.length); + default -> System.out.println("Something else"); + } + } + } + """ + )); + } + + @Test + void shouldParseJava21SwitchWithSpecialCases() { + rewriteRun( + java( + //language=java + """ + class Test { + void integerTester(Integer i) { + switch (i) { + case -1, 1 -> System.out.println("special"); + case Integer j when (j - 1) > -1 -> System.out.println("pos"); + case Integer j -> System.out.println("others"); + } + } + } + """ + )); + } } From 212fd516042cae52fdb3548e70931850d86f14dc Mon Sep 17 00:00:00 2001 From: Niels de Bruin Date: Wed, 13 Nov 2024 12:10:57 +0100 Subject: [PATCH 3/3] Update Java21ParserTest.java Small bug fix --- .../src/test/java/org/openrewrite/java/Java21ParserTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java b/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java index aa2eab8ffee..db63424deca 100644 --- a/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java +++ b/rewrite-java-21/src/test/java/org/openrewrite/java/Java21ParserTest.java @@ -148,8 +148,8 @@ final class Bar implements I {} class Test { void switchEnumExtendedType(I c) { switch (c) { - case Foo.CLUBS -> System.out.println("It's Foo A"); - case Foo.DIAMONDS -> System.out.println("It's Foo B"); + case Foo.A -> System.out.println("It's Foo A"); + case Foo.B -> System.out.println("It's Foo B"); case Bar b -> System.out.println("It's Bar"); } }