GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix `IfElseIfConstructToSwitch` type attribution for non-JDK types by timtebeek · Pull Request #994 · openrewrite/rewrite-migrate-java

Conversation

@timtebeek

@timtebeek timtebeek commented Feb 27, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

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.

All reactions
@moderne-meeseeks moderne-meeseeks Bot added this to OpenRewrite Feb 27, 2026
@moderne-meeseeks moderne-meeseeks Bot assigned timtebeek Feb 27, 2026
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Feb 27, 2026
@timtebeek
Fix IfElseIfConstructToSwitch type attribution for non-JDK types
fa52f6b
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
timtebeek force-pushed the timtebeek/fix-java21-recipe branch from 1e6a960 to fa52f6b Compare February 27, 2026 10:06
@timtebeek timtebeek changed the title Fix IfElseIfConstructToSwitch for non-JDK types Fix IfElseIfConstructToSwitch for non-JDK types Feb 27, 2026
@timtebeek timtebeek changed the title Fix IfElseIfConstructToSwitch for non-JDK types Fix IfElseIfConstructToSwitch type attribution for non-JDK types Feb 27, 2026
@timtebeek
timtebeek requested a review from Jenson3210 February 27, 2026 11:32
@timtebeek timtebeek added bug Something isn't working recipe Recipe requested java 21+ labels Feb 27, 2026
@timtebeek timtebeek moved this from In Progress to Ready to Review in OpenRewrite Feb 27, 2026
@timtebeek
timtebeek marked this pull request as ready for review February 27, 2026 11:33
@timtebeek timtebeek changed the title Fix IfElseIfConstructToSwitch type attribution for non-JDK types Fix IfElseIfConstructToSwitch type attribution for non-JDK types Feb 27, 2026
@timtebeek
Use ListUtils.map in fixTypeAttribution
f488374
Jenson3210
Jenson3210 approved these changes Mar 2, 2026
View reviewed changes

@Jenson3210 Jenson3210 left a comment

Copy link
Copy Markdown
Contributor

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.

All reactions
@timtebeek
timtebeek merged commit 53b3725 into main Mar 2, 2026
1 check passed

Uh oh!

There was an error while loading. Please reload this page.

@timtebeek
timtebeek deleted the timtebeek/fix-java21-recipe branch March 2, 2026 08:40
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Mar 2, 2026
@timtebeek timtebeek mentioned this pull request Mar 9, 2026
IfElseIfConstructToSwitch fails to add variable declarations for custom types used in instanceof #1004
Closed
mergify Bot added a commit to robfrank/linklift that referenced this pull request Mar 10, 2026
@mergify
chore(deps): bump org.openrewrite.recipe:rewrite-migrate-java from 3.…
85364fe
…28.0 to 3.29.0 [skip ci]

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

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

> 3.29.0
> ------
>
> What's Changed
> --------------
>
> * Add entries to `.gitignore` by [`@​zieka`](https://github.com/zieka) in [openrewrite/rewrite-migrate-java#995](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/995)
> * Add [`@​RequiredArgsConstructor`](https://github.com/RequiredArgsConstructor) and [`@​AllArgsConstructor`](https://github.com/AllArgsConstructor) recipes by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-migrate-java#996](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/996)
> * Use classpathFromResources for lombok in Java templates by [`@​jkschneider`](https://github.com/jkschneider) in [openrewrite/rewrite-migrate-java#997](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/997)
> * Fix `IfElseIfConstructToSwitch` type attribution for non-JDK types by [`@​timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-migrate-java#994](https://redirect.github.com/openrewrite/rewrite-migrate-java/pull/994)
>
> **Full Changelog**: <openrewrite/rewrite-migrate-java@v3.28.0...v3.29.0>


Commits

* [`42fd45a`](openrewrite/rewrite-migrate-java@42fd45a) [Auto] SDKMAN! Java candidates as of 2026-03-02T1038
* [`53b3725`](openrewrite/rewrite-migrate-java@53b3725) Fix `IfElseIfConstructToSwitch` type attribution for non-JDK types ([#994](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/994))
* [`eec22b2`](openrewrite/rewrite-migrate-java@eec22b2) Use classpathFromResources for lombok in Java templates ([#997](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/997))
* [`c0f7b7d`](openrewrite/rewrite-migrate-java@c0f7b7d) Add [`@​RequiredArgsConstructor`](https://github.com/RequiredArgsConstructor) and [`@​AllArgsConstructor`](https://github.com/AllArgsConstructor) recipes ([#996](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/996))
* [`aa49b8f`](openrewrite/rewrite-migrate-java@aa49b8f) Add entries to `.gitignore` ([#995](https://redirect.github.com/openrewrite/rewrite-migrate-java/issues/995))
* See full diff in [compare view](openrewrite/rewrite-migrate-java@v3.28.0...v3.29.0)
  
[![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.28.0&new-version=3.29.0)](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

Reviewers

1 more reviewer
@Jenson3210 Jenson3210 Jenson3210 approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@timtebeek timtebeek

Labels

bug Something isn't working java 21+ recipe Recipe requested

Projects

OpenRewrite
Archived in project

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.

2 participants

@timtebeek @Jenson3210
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.