Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support JEP-440 and 441 #4661

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,182 @@
import java.nio.file.Files;
import java.nio.file.Paths;

import static org.openrewrite.java.Assertions.java;

class Java21ParserTest implements RewriteTest {

@Test
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);
}
}
}
"""
));
}

@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.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");
}
}
}
"""
));
}

@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");
}
}
}
"""
));
}
}
Loading