Skip to content

Fix IfElseIfConstructToSwitch dropping pattern variables for unresolvable types#1005

Merged
timtebeek merged 3 commits intomainfrom
fix/if-else-switch-missing-pattern-vars
Mar 9, 2026
Merged

Fix IfElseIfConstructToSwitch dropping pattern variables for unresolvable types#1005
timtebeek merged 3 commits intomainfrom
fix/if-else-switch-missing-pattern-vars

Conversation

@timtebeek
Copy link
Member

@timtebeek timtebeek commented Mar 9, 2026

Summary

  • When IfElseIfConstructToSwitch converts if-else-if chains with instanceof checks on non-JDK types, JavaTemplate's raw string substitution (#{}) can fail to produce J.VariableDeclarations AST nodes, causing pattern variable declarations to be dropped (e.g., case Dog d -> becomes case Dog ->)
  • Uses positional case identification (null case offset + pattern case count) instead of type-checking labels, ensuring the instanceOfs iterator always advances correctly for pattern match cases
  • Adds a buildVariableDeclarations() fallback that reconstructs a proper J.VariableDeclarations from the original J.InstanceOf when JavaTemplate fails to produce one

Test plan

…lvable types

When JavaTemplate can't resolve a non-JDK type during switch case
generation, it may not produce a J.VariableDeclarations AST node,
causing pattern variable declarations to be dropped (e.g.,
`case Dog d ->` becomes `case Dog ->`).

Fix by using positional case identification instead of type-checking
the label, and reconstructing a proper J.VariableDeclarations from
the original J.InstanceOf when the template parser fails to produce
one.

Fixes #1004
@timtebeek timtebeek marked this pull request as ready for review March 9, 2026 15:03
@timtebeek timtebeek requested a review from Jenson3210 March 9, 2026 15:03
Copy link
Contributor

@Jenson3210 Jenson3210 left a comment

Choose a reason for hiding this comment

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

Left a question/comment. Other that I think it looks okay. Too bad we do not have the original types of the code at hand during JavaTemplate generation as that would make this not necessary.

@github-project-automation github-project-automation bot moved this from In Progress to Ready to Review in OpenRewrite Mar 9, 2026
@timtebeek timtebeek changed the title Fix IfElseIfConstructToSwitch dropping pattern variables for unresolvable types Fix IfElseIfConstructToSwitch dropping pattern variables for unresolvable types Mar 9, 2026
@timtebeek timtebeek merged commit 7661bb1 into main Mar 9, 2026
1 check passed
@timtebeek timtebeek deleted the fix/if-else-switch-missing-pattern-vars branch March 9, 2026 16:34
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

IfElseIfConstructToSwitch fails to add variable declarations for custom types used in instanceof

2 participants