Prevent switch expression migrations that produce uncompilable code on Java 17#823
Closed
jevanlingen wants to merge 4 commits intomainfrom
Closed
Prevent switch expression migrations that produce uncompilable code on Java 17#823jevanlingen wants to merge 4 commits intomainfrom
jevanlingen wants to merge 4 commits intomainfrom
Conversation
…LabelsWithDefaultCase check
src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseAssignmentsToSwitchExpression.java
Outdated
Show resolved
Hide resolved
src/main/java/org/openrewrite/java/migrate/lang/SwitchCaseReturnsToSwitchExpression.java
Outdated
Show resolved
Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Member
|
Thanks for pointing out these issues with the Java 17 migration; from a maintenance point of view I think I'd prefer to just move these recipes over to the Java 21 migration, also to provide incentive for folks to upgrade. Having these conditionals, and spread across files isn't ideal when we need to maintain these longer term, especially as switches will get more options like primitives in the near future. |
Contributor
Author
|
Seems like a valid point! I'll close this PR and move everything to Java 21. |
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?
Switches are not changed to switch expressions when it leads to uncompilable code.
What's your motivation?
When running the recipes on a Java 17 codebase, certain transformations could result in uncompilable code.
For example, applying the SwitchCaseAssignmentsToSwitchExpression recipe to:
would produce:
While the original statement compiles under Java 17, the transformed switch expression triggers the following error:
This happens because combining a constant label (e.g., "ignored") with
defaultin a single case label is only permitted with the pattern matching for switch preview feature in Java 17, which was finalized in Java 21.Checklist