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
Copy Markdown
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
Copy Markdown
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
mergify bot added a commit to robfrank/linklift that referenced this pull request Mar 19, 2026
…29.0 to 3.30.1 [skip ci]

Bumps [org.openrewrite.recipe:rewrite-migrate-java](https://github.com/openrewrite/rewrite-migrate-java) from 3.29.0 to 3.30.1.
Release notes

*Sourced from [org.openrewrite.recipe:rewrite-migrate-java's releases](https://github.com/openrewrite/rewrite-migrate-java/releases).*

> v3.30.1
> -------
>
> What's Changed
> --------------
>
> * Fix Jackson 2.21+ compatibility for JREThrowableFinalMethods by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1010](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1010)
> * Fix UpgradeToJava21 recipe ordering for switch expressions by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1012](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1012)
> * Fix IfElseIfConstructToSwitch null safety regression by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1014](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1014)
> * Fix Jetty EE9 dependencies by [`@​evie-lau`](https://github.com/evie-lau) in [openrewrite/rewrite-migrate-java#1016](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1016)
> * Cleanup Jetty EE10 recipes. Reorder… by [`@​evie-lau`](https://github.com/evie-lau) in [openrewrite/rewrite-migrate-java#1017](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1017)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.30.0...v3.30.1>
>
> 3.30.0
> ------
>
> What's Changed
> --------------
>
> * DataNucleus migrations by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-migrate-java#998](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/998)
> * Enable Lombok annotation processor during Java 17 upgrades by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#999](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/999)
> * Use Groovy 4 by [`@​greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-migrate-java#1001](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1001)
> * Avoid 9.1 Gradle version, should be 9.1.0 by [`@​greg-at-moderne`](https://github.com/greg-at-moderne) in [openrewrite/rewrite-migrate-java#1002](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1002)
> * Also upgrade maven-surefire-report-plugin to 3.5.x by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1006](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1006)
> * Fix `IfElseIfConstructToSwitch` dropping pattern variables for unresolvable types by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1005](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1005)
> * Fix Jackson 2.21+ conflicting creators in AWT recipes by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#1007](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/1007)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.29.0...v3.30.0>


Commits

* [`b9375e4`](openrewrite/rewrite-migrate-java@b9375e4) Fix Jetty EE10 duplicate oldArtifactId on ChangeDependencies. Reorder and bri...
* [`fd968d3`](openrewrite/rewrite-migrate-java@fd968d3) Fix Jetty EE9 dependencies ([#1016](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1016))
* [`062873e`](openrewrite/rewrite-migrate-java@062873e) Fix IfElseIfConstructToSwitch null safety regression ([#1014](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1014))
* [`7948b9a`](openrewrite/rewrite-migrate-java@7948b9a) Fix UpgradeToJava21 recipe ordering for switch expressions ([#1012](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1012))
* [`c421ab1`](openrewrite/rewrite-migrate-java@c421ab1) Fix Jackson 2.21+ compatibility for JREThrowableFinalMethods ([#1010](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1010))
* [`9826a90`](openrewrite/rewrite-migrate-java@9826a90) [Auto] SDKMAN! Java candidates as of 2026-03-16T1049
* [`5ff7443`](openrewrite/rewrite-migrate-java@5ff7443) Lombok Best Practices
* [`6044cbb`](openrewrite/rewrite-migrate-java@6044cbb) OpenRewrite recipe best practices
* [`114a5a2`](openrewrite/rewrite-migrate-java@114a5a2) Fix Jackson 2.21+ compatibility for AWT migration recipes ([#1007](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/1007))
* [`7661bb1`](openrewrite/rewrite-migrate-java@7661bb1) Fix `IfElseIfConstructToSwitch` dropping pattern variables for unresolvable t...
* Additional commits viewable in [compare view](openrewrite/rewrite-migrate-java@v3.29.0...v3.30.1)
  
[![Dependabot compatibility score](https://dependabot-badges.githubapp.com/badges/compatibility\_score?dependency-name=org.openrewrite.recipe:rewrite-migrate-java&package-manager=maven&previous-version=3.29.0&new-version=3.30.1)](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores)
Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.
[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)
---
Dependabot commands and options
  
You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot show  ignore conditions` will show all of the ignore conditions of the specified dependency
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
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