Conversation
…um constants For enum constants (e.g. OldValue.A), the J.FieldAccess expression type and the name identifier type both carry the owning enum type, unlike regular static constants where the expression type is the field's value type (e.g. String). This caused TypesInUse to still find a reference to the old owning type after replacement, preventing RemoveImport from cleaning up the stale import. Fix by updating name.type and fieldAccess.type when they reference the old owning type during field access replacement.
2 tasks
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
ReplaceConstantWithAnotherConstantcorrectly rewrote the constant reference (e.g.OldValue.A→NewValue.B) but failed to remove the oldimport com.constant.OldValuewhen the replaced constant was an enum constant.Root Cause
After the visitor modifies the tree,
RemoveImportre-scans it viaTypesInUseto determine whether the old type is still referenced. For regular static constants (e.g.File.pathSeparator), theJ.FieldAccessexpression type and the name identifier type are the field's value type (e.g.String) — unrelated to the owning class — so no stale reference remains after the target identifier is rewritten.For enum constants (e.g.
OldValue.A), the expression type is the enum type itself. Two LST nodes were left carryingcom.constant.OldValueafter replacement:J.Identifier(the constant nameA/B) — its.typeis the enum typeJ.FieldAccess— its expression.typeis also the enum typeTypesInUsepicked these up andRemoveImportconcluded the old type was still in use, keeping the stale import.Fix
After computing the new
target, updatename.typeandfieldAccess.typewhen they reference the old owning type. Both updates are guarded byTypeUtils.isOfClassTypeso they only fire for the enum-constant case; for regular constants (where those types are unrelated to the owner), the checks are no-ops.The duplicate
nameassignment that existed in both if/else branches was also hoisted out, and the redundantinstanceof JavaType.FullyQualifiedguard beforeisOfClassType(which performs that check internally) was removed.Test plan
replaceEnumConstanttest covers the broken scenario end-to-endReplaceConstantWithAnotherConstantTesttests continue to pass