We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Add recipe for JEP 441: Pattern Matching for switch to improve Java 21 migration (and possibly also for the related JEP 440: Record Patterns).
The two examples copied from the JEP 441: Pattern Matching for switch project (there are more):
instanceof
// Prior to Java 21 static String formatter(Object obj) { String formatted = "unknown"; if (obj instanceof Integer i) { formatted = String.format("int %d", i); } else if (obj instanceof Long l) { formatted = String.format("long %d", l); } else if (obj instanceof Double d) { formatted = String.format("double %f", d); } else if (obj instanceof String s) { formatted = String.format("String %s", s); } return formatted; }
// As of Java 21 static 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(); }; }
Switches and null
// Prior to Java 21 static void testFooBarOld(String s) { if (s == null) { System.out.println("Oops!"); return; } switch (s) { case "Foo", "Bar" -> System.out.println("Great"); default -> System.out.println("Ok"); } }
// As of Java 21 static void testFooBarNew(String s) { switch (s) { case null -> System.out.println("Oops"); case "Foo", "Bar" -> System.out.println("Great"); default -> System.out.println("Ok"); } }
no
Not at the moment
The text was updated successfully, but these errors were encountered:
No branches or pull requests
What problem are you trying to solve?
Add recipe for JEP 441: Pattern Matching for switch to improve Java 21 migration (and possibly also for the related JEP 440: Record Patterns).
Describe the solution you'd like
The two examples copied from the JEP 441: Pattern Matching for switch project (there are more):
instanceof
Switches and null
Have you considered any alternatives or workarounds?
no
Additional context
Are you interested in contributing this feature to OpenRewrite?
Not at the moment
The text was updated successfully, but these errors were encountered: