Skip to content

Support JEP-441: Pattern Matching for switch for Java 21+ - #4661

Merged
Laurens-W merged 41 commits into
mainfrom
4660-support-jep-440-and-441
Jan 17, 2025
Merged

Laurens-W merged 41 commits into
mainfrom
4660-support-jep-440-and-441

Conversation

@nielsdebruin

@nielsdebruin nielsdebruin commented Nov 11, 2024 •

Copy link
Copy Markdown
Contributor

What's changed?

Initial test cases for verifying parser support for JEP 440 and 441. The goal of these tests is to demonstrate that code containing JEP 440/441 structures remains unchanged. The tests very closely resemble the code snippets used in the JEPs themselves to describe their changes. They can be summarized as follows:

Pattern Switch Statement
Verifies that switch expressions using patterns for different object types (e.g., Integer, Long, Double, String) maintain structure after parsing.

Pattern Matching for Records
Tests parsing of instanceof with record deconstruction (e.g., if (obj instanceof Point(int x, int y))), ensuring that records can be deconstructed and match syntax remains unchanged.

Nested Pattern Matching for Records and Enums
Ensures that nested pattern matching with records containing other records and enums (e.g., Rectangle(ColoredPoint(Point p, Color c), ColoredPoint lr)) is correctly handled and maintains structure.

null Case Handling in Switch Statements
Checks that switch cases with null handling (e.g., case null ->)

Improved Enum Support with Sealed Types
Tests switch handling for sealed types and enums (e.g., case Foo.CLUBS, case Bar b) to ensure that sealed interfaces with enums and final classes maintain parsing integrity.

Relaxed Type Restrictions in Switch Statements
Verifies that switch cases with mixed types (e.g., String, Color, Point, int[]) correctly serialize and deserialize while preserving type information.

Special Integer Cases in Switch
Tests specific integer cases and when clauses (e.g., case -1, 1, case Integer j when (j - 1) > -1) in switch statements, ensuring all conditional logic remains intact after parsing.

What's your motivation?

Provide support for JEP 440/441

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@nielsdebruin nielsdebruin linked an issue Nov 11, 2024 that may be closed by this pull request
@nielsdebruin nielsdebruin changed the title 4660 support jep 440 and 441 4660 support JEP 440 and 441 Nov 11, 2024
@nielsdebruin nielsdebruin added enhancement New feature or request java java 21+ labels Nov 11, 2024
@timtebeek
timtebeek self-requested a review November 11, 2024 23:12
@knutwannheden

Copy link
Copy Markdown
Contributor

Aren't some of the tests (like record pattern matching) for preview only features? So far we haven't supported any preview features and people haven't really asked for it either.

@nielsdebruin

Copy link
Copy Markdown
Contributor Author

As far as I understand it, both 440/441 have been delivered/closed (see status in 440 and 441) and have been included in the release of Java 21 as permanent JEPs (source: https://www.jrebel.com/blog/java-21).

I double-checked by running some of the code snippets in these using Java Termurin-21.0.4, which worked fine.

@knutwannheden

Copy link
Copy Markdown
Contributor

I think you are right. I missed that!

@timtebeek timtebeek changed the title 4660 support JEP 440 and 441 Support JEP-440 and 441 Nov 18, 2024

@knutwannheden knutwannheden left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@Laurens-W
Laurens-W merged commit 9ddbb83 into main Jan 17, 2025
@Laurens-W
Laurens-W deleted the 4660-support-jep-440-and-441 branch January 17, 2025 13:08
knutwannheden added a commit to openrewrite/rewrite-static-analysis that referenced this pull request Jan 17, 2025
knutwannheden added a commit to openrewrite/rewrite-python that referenced this pull request Jan 17, 2025
hsoliwal added a commit to hsoliwal/rewrite that referenced this pull request Oct 24, 2026
Follow-up to the JEP 441 backport (openrewrite#4661): a case whose first label is a type pattern has no expression label, so
the 8.17.1 derivation caze.getExpressions().get(0) threw IndexOutOfBoundsException, for example from
ChangeType.postVisit. The type now stays unknown (the method is @nullable) when the first case has no expression
label; constant-label switches are unchanged. Upstream later stores the switch expression's type as a field, a model
change this backport does not take.

Verified with a probe that runs ChangeType over a file with `switch (o) { case Integer i -> ...; default -> ...; }`:
it fails on the previous patched jar and passes now; the openrewrite#4184 sibling probe stays green.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request java java 21+

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Support JEP-441: Pattern Matching for switch

4 participants