Skip to content

Prevent switch expression migrations that produce uncompilable code on Java 17#823

Closed
jevanlingen wants to merge 4 commits intomainfrom
switch-recipes-supportsMultiCaseLabelsWithDefaultCase
Closed

Prevent switch expression migrations that produce uncompilable code on Java 17#823
jevanlingen wants to merge 4 commits intomainfrom
switch-recipes-supportsMultiCaseLabelsWithDefaultCase

Conversation

@jevanlingen
Copy link
Copy Markdown
Contributor

@jevanlingen jevanlingen commented Aug 13, 2025

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:

String formatted = "initialValue";
switch (str) {
    case "foo": formatted = "Foo"; break;
    case "ignored", default: formatted = "unknown";
}

would produce:

String formatted = switch (str) {
    case "foo" -> "Foo";
    case "ignored", default -> "unknown";
};

While the original statement compiles under Java 17, the transformed switch expression triggers the following error:

patterns in switch statements are a preview feature and are disabled by default.

This happens because combining a constant label (e.g., "ignored") with default in 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

  • 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

@jevanlingen jevanlingen self-assigned this Aug 13, 2025
@jevanlingen jevanlingen added bug Something isn't working recipe Recipe requested labels Aug 13, 2025
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Aug 13, 2025
@jevanlingen jevanlingen moved this from In Progress to Ready to Review in OpenRewrite Aug 13, 2025
jevanlingen and others added 3 commits August 13, 2025 12:13
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>
@timtebeek
Copy link
Copy Markdown
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.

@jevanlingen
Copy link
Copy Markdown
Contributor Author

Seems like a valid point! I'll close this PR and move everything to Java 21.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working recipe Recipe requested

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants