Conversation
…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
Jenson3210
reviewed
Mar 9, 2026
src/main/java/org/openrewrite/java/migrate/lang/IfElseIfConstructToSwitch.java
Show resolved
Hide resolved
Jenson3210
approved these changes
Mar 9, 2026
Contributor
Jenson3210
left a comment
There was a problem hiding this comment.
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.
IfElseIfConstructToSwitch dropping pattern variables for unresolvable types
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.
Summary
IfElseIfConstructToSwitchconverts if-else-if chains withinstanceofchecks on non-JDK types, JavaTemplate's raw string substitution (#{}) can fail to produceJ.VariableDeclarationsAST nodes, causing pattern variable declarations to be dropped (e.g.,case Dog d ->becomescase Dog ->)instanceOfsiterator always advances correctly for pattern match casesbuildVariableDeclarations()fallback that reconstructs a properJ.VariableDeclarationsfrom the originalJ.InstanceOfwhen JavaTemplate fails to produce oneTest plan
All 14 existing
IfElseIfConstructToSwitchTesttests pass (includingnullCheckWithNonJdkTypesandthreeNonJdkTypes)No regressions in other tests
Fixes
IfElseIfConstructToSwitchfails to add variable declarations for custom types used ininstanceof#1004