Skip to content

Skip IfElseIfConstructToSwitch when instanceof types are missing#1095

Merged
timtebeek merged 4 commits into
mainfrom
tim/fix-ifelseif-to-switch
May 18, 2026
Merged

Skip IfElseIfConstructToSwitch when instanceof types are missing#1095
timtebeek merged 4 commits into
mainfrom
tim/fix-ifelseif-to-switch

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented May 12, 2026

Summary

  • A user reported IfElseIfConstructToSwitch emitting broken output with leaked JavaTemplate parameter stubs (__P__./*__pN__*/voidp()) and mis-parsed pattern-switch syntax. The reporter suspected missing type information.
  • I could not reproduce the failure in a test, so rather than speculate at the precise template parser interaction, this PR stays conservative: add a precondition that all instanceof types are well-formed (TypeUtils.isWellFormedType). When types are unresolvable, the if-chain is now left unchanged.
  • This reverses the prior behavior of unresolvableTypesKeepCaseSpacing (Missing whitespace on case in zuul-core/.../Http2MetricsChannelHandlers.java with If-else-if-else to switch rewrite#7458), which previously produced a switch even with unresolvable types. The test is renamed and inverted to assert no-change.

Test plan

  • ./gradlew test --tests "*IfElseIfConstructToSwitchTest*" (all 25 tests pass)

The template references symbols visible from its insertion scope
(variables, types, methods), so parsing outside that context can fail
on Java 21 pattern-switch syntax and leak `__P__` parameter stubs into
the result.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 12, 2026
@timtebeek timtebeek marked this pull request as draft May 12, 2026 16:46
Replace the speculative `contextSensitive()` template change with a
precondition: skip the conversion when any `instanceof`'s type is
missing or unknown. A reporter observed JavaTemplate parameter stubs
(`__P__./*__pN__*/voidp()`) leaking into the generated switch when types
were unresolved; staying conservative avoids the broken output without
guessing at the precise template parser interaction.
@timtebeek timtebeek changed the title Use contextSensitive() template in IfElseIfConstructToSwitch Skip IfElseIfConstructToSwitch when instanceof types are missing May 18, 2026
@timtebeek timtebeek marked this pull request as ready for review May 18, 2026 08:41
@timtebeek timtebeek requested a review from MBoegers May 18, 2026 08:41
`RemoveAnnotationVisitor.maybeRemoveBlankLines` walks up to the
enclosing `JavaSourceFile`, but the visitor was invoked without a
parent cursor, causing `IllegalStateException: Expected to find
enclosing JavaSourceFile` after a recent framework change.
@timtebeek timtebeek merged commit d6ec0b8 into main May 18, 2026
1 check passed
@timtebeek timtebeek deleted the tim/fix-ifelseif-to-switch branch May 18, 2026 09:20
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant