Skip to content

Conversation

@timtebeek
Copy link
Member

@timtebeek timtebeek commented Oct 26, 2025

Summary

Problem

When renaming a field variable, RenameVariable would correctly rename:

  • ✅ The field declaration
  • ✅ Simple references (e.g., foo)
  • ❌ Qualified references (e.g., Bar.foo or AcceptanceChecks.foo)

Example

public class Bar {
    private static final String foo = "hello";

    public static class AcceptanceChecks {
        public static final String foo = "world";
    }

    public void bar() {
        String a = foo;                    // Was renamed ✅
        String b = AcceptanceChecks.foo;   // Was NOT renamed ❌
    }
}

Root Cause

The fieldAccessTargetsVariable method in RenameVariable.java only 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 fieldAccessTargetsVariable to also verify the field being accessed matches the variable being renamed by:

  1. Attempting field type comparison using TypeUtils.isOfType
  2. Falling back to simple name comparison if field types don't match

Changes

  • RenameVariable.java (rewrite-java/src/main/java/org/openrewrite/java/RenameVariable.java:174-192)
    • Updated fieldAccessTargetsVariable method to verify both the target class and the field name/type
  • RenameVariableTest.java (rewrite-java-test/src/test/java/org/openrewrite/java/RenameVariableTest.java)
    • Added test case renameVariableQualifiedField to verify the fix

Testing

  • ✅ New test renameVariableQualifiedField passes
  • ✅ All 34 existing RenameVariableTest tests pass
  • ✅ Full rewrite-java-test suite passes

Test Plan

The new test verifies that when renaming all variables named foo, qualified field references like AcceptanceChecks.foo are correctly renamed to AcceptanceChecks.FOO.

🤖 Generated with Claude Code

The RenameVariable class was not properly renaming qualified field
references (e.g., ClassName.fieldName). The fieldAccessTargetsVariable
method only checked if the target class matched but didn't verify the
accessed field was the same field being renamed.

This fix adds field type comparison and name matching to ensure only
the correct qualified field references are renamed.

Fixes #6195

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Oct 26, 2025
@timtebeek timtebeek self-assigned this Oct 26, 2025
@timtebeek timtebeek added the bug Something isn't working label Oct 26, 2025
@timtebeek timtebeek closed this Oct 28, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Oct 28, 2025
@timtebeek timtebeek deleted the fix-6195-rename-variable-qualified-field branch October 28, 2025 16:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

RenameVariable doesn't rename qualified field reference

2 participants