Fix IfElseIfConstructToSwitch type attribution for non-JDK types#994
Open
Fix IfElseIfConstructToSwitch type attribution for non-JDK types#994
IfElseIfConstructToSwitch type attribution for non-JDK types#994Conversation
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.
1e6a960 to
fa52f6b
Compare
IfElseIfConstructToSwitch for non-JDK types
IfElseIfConstructToSwitch for non-JDK typesIfElseIfConstructToSwitch type attribution for non-JDK 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
JavaTemplateapplies raw string substitution (#{}) for non-JDK types in generated switch case labelsfixTypeAttributionpost-processing method that restores original type information frominstanceofchecks onto generatedcaselabels and pattern variablesIfElseIfConstructToSwitchinjava-version-21.yml(was disabled with a FIXME aboutcasecaseartifact near non-JDK types)What was wrong
JavaTemplate.apply()uses raw string substitution which loses type information for non-JDK types. This caused corruptedcasecaseoutput in the generated switch statements when the recipe encountered user-defined types likeDog,Cat, etc.How it's fixed
After the template generates the switch statement,
fixTypeAttribution()iterates over the originalinstanceofchecks (frompatternMatchers) alongside the generated case statements. For each pattern-matching case label (J.VariableDeclarations), it replaces thetypeExpressionand variable type with the originals from the correspondinginstanceofcheck, which carry proper type information.Test plan
nullCheckWithNonJdkTypes— null check + two non-JDKinstanceoftypes (Dog,Cat)threeNonJdkTypes— three non-JDKinstanceoftypes without null check (Dog,Cat,Bird)