GeezerVison is showing github.com — Fix `IfElseIfConstructToSwitch` type attribution for non-JDK types by timtebeek · Pull Request #994 · openrewrite/rewrite-migrate-java
Conversation
timtebeek
commented
Feb 27, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
Summary
- Fix type attribution loss when
JavaTemplate applies raw string substitution (#{}) for non-JDK types in generated switch case labels
- Add
fixTypeAttribution post-processing method that restores original type information from instanceof checks onto generated case labels and pattern variables
- Re-enable
IfElseIfConstructToSwitch in java-version-21.yml (was disabled with a FIXME about casecase artifact near non-JDK types)
What was wrong
JavaTemplate.apply() uses raw string substitution which loses type information for non-JDK types. This caused corrupted casecase output in the generated switch statements when the recipe encountered user-defined types like Dog, Cat, etc.
How it's fixed
After the template generates the switch statement, fixTypeAttribution() iterates over the original instanceof checks (from patternMatchers) alongside the generated case statements. For each pattern-matching case label (J.VariableDeclarations), it replaces the typeExpression and variable type with the originals from the corresponding instanceof check, which carry proper type information.
Test plan
-
nullCheckWithNonJdkTypes — null check + two non-JDK instanceof types (Dog, Cat)
-
threeNonJdkTypes — three non-JDK instanceof types without null check (Dog, Cat, Bird)
- Run full test suite to verify no regressions
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
JavaTemplate uses raw string substitution (#{}) for class names in
generated switch cases. The template parser can resolve JDK types
like Integer (in java.lang) but cannot resolve non-JDK types from
just a name string, producing missing type information on case labels.
Add fixTypeAttribution() post-processing that restores original type
information from the instanceof checks onto the generated switch case
variable declarations.
Re-enable the recipe in UpgradeToJava21.
timtebeek
changed the title
Fix IfElseIfConstructToSwitch for non-JDK types
Fix IfElseIfConstructToSwitch for non-JDK types
Feb 27, 2026
timtebeek
changed the title
Fix IfElseIfConstructToSwitch for non-JDK types
Fix IfElseIfConstructToSwitch type attribution for non-JDK types
Feb 27, 2026
timtebeek
changed the title
Fix IfElseIfConstructToSwitch type attribution for non-JDK types
Fix IfElseIfConstructToSwitch type attribution for non-JDK types
Feb 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice to see this enabled again!
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|