apache / netbeans

Apache NetBeans
https://netbeans.apache.org/
Apache License 2.0
2.64k stars 849 forks source link

Renaming a field can cause the code behavior to change #7577

Open DongChunHao opened 3 months ago

DongChunHao commented 3 months ago

Apache NetBeans version

Apache NetBeans 22

What happened

When the "fieldToRename" field is selected for the rename field refactoring, it causes a refactoring bug because the new field name being renamed conflicts with other variables in the inner class, resulting in a change in code behavior.

Language / Project Type / NetBeans Component

No response

How to reproduce

  1. select 'fieldToREname'
  2. rename 'fieldToREname' to 'newVariable' image

In this example, a variable is assigned to a field before refactoring, and when the field is renamed to a variable name, the assignment statement becomes an assignment to the field itself, resulting in a change in code behavior

Did this work correctly in an earlier version?

No / Don't know

Operating System

Windows11

JDK

20

Apache NetBeans packaging

Apache NetBeans platform

Anything else

No response

Are you willing to submit a pull request?

No

mbien commented 2 months ago

I mean it could change behavior, but how does an IDE know what the user intends to do? Trying to rename this field will open the refactoring window which already has a preview button to review changes before applying them.

I often intentionally rename identifiers to the same name of something else as intermediate step, before removing the other declaration during cleanups.

I suppose this could be updated to detect this situation and add a "identifier will hide a field warning", but NB shouldn't prevent the refactoring there.

Rafay121 commented 2 weeks ago

@DongChunHao @mbien I've been reviewing issue #7577 related to the "fieldToRename" refactoring bug, but I’m having difficulty identifying the exact files and code sections where this problem occurs. I’ve also reviewed the provided image, but still need additional context to proceed.

Could you kindly provide the following:

  1. The specific file(s) in which the renaming bug is occurring.
  2. Any related code sections or classes that trigger the bug.
  3. Additional context or steps that could help narrow down the exact location in the codebase.

Any guidance would be greatly appreciated so I can continue working on resolving this issue. Thank you!

mbien commented 2 weeks ago

The specific file(s) in which the renaming bug is occurring.

well, its not a bug since the IDE does what the user instructed it to do. It might be possible to display a name-conflict warning for some cases, but doing it for all cases is probably not possible until the refactoring is actually run or simulated. Given that there already is a refactoring preview and all refactorings have to be reviewed, I personally see this as low priority.

package feature.renamehidesfield;

class RenameHidesField {

    // https://github.com/apache/netbeans/issues/7577
    public void method() {
        int outer = 0;
        class InnerClass {
            int inner; // rename to "outer", should the dialog post a "identifier hides a variable" warning?
            public void testmethod2() {
                this.inner = outer;
            }
        }
    }
}
Rafay121 commented 2 weeks ago

Thanks for the clarification. Could you please share the file path or class names where the renaming refactoring logic is handled?