Repository navigation
Support JEP-441: Pattern Matching for switch for Java 21+ - #4661
Merged
Merged
Conversation
timtebeek
self-requested a review
November 11, 2024 23:12
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. |
Small bug fix
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 |
Contributor
|
I think you are right. I missed that! |
Laurens-W
reviewed
Jan 8, 2025
Laurens-W
reviewed
Jan 16, 2025
Laurens-W
reviewed
Jan 16, 2025
knutwannheden
approved these changes
Jan 17, 2025
This was referenced Jan 17, 2025
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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