Fix RenameVariable to properly rename qualified field references #6196
+48
−0
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
RenameVariablewas not renaming qualified field references (e.g.,ClassName.fieldName).Problem
When renaming a field variable,
RenameVariablewould correctly rename:foo)Bar.fooorAcceptanceChecks.foo)Example
Root Cause
The
fieldAccessTargetsVariablemethod inRenameVariable.javaonly verified that the target class matched the field's owner, but didn't verify that the accessed field was actually the same field being renamed. This could potentially match other fields from the same class with different names.Solution
Enhanced
fieldAccessTargetsVariableto also verify the field being accessed matches the variable being renamed by:TypeUtils.isOfTypeChanges
RenameVariable.java(rewrite-java/src/main/java/org/openrewrite/java/RenameVariable.java:174-192)fieldAccessTargetsVariablemethod to verify both the target class and the field name/typeRenameVariableTest.java(rewrite-java-test/src/test/java/org/openrewrite/java/RenameVariableTest.java)renameVariableQualifiedFieldto verify the fixTesting
renameVariableQualifiedFieldpassesRenameVariableTesttests passrewrite-java-testsuite passesTest Plan
The new test verifies that when renaming all variables named
foo, qualified field references likeAcceptanceChecks.fooare correctly renamed toAcceptanceChecks.FOO.🤖 Generated with Claude Code