This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 253960 - Encapsulate field refactoring does not change a field assignment by the "set" method
Summary: Encapsulate field refactoring does not change a field assignment by the "set"...
Status: RESOLVED INVALID
Alias: None
Product: java
Classification: Unclassified
Component: Refactoring (show other bugs)
Version: 8.0.2
Hardware: PC Mac OS X
: P3 normal (vote)
Assignee: Ralph Ruijs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-07-29 18:01 UTC by melmongiovi
Modified: 2015-07-30 08:01 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description melmongiovi 2015-07-29 18:01:15 UTC
Applying the encapsulate field refactoring in A.f, it does not change the field assignment "f = 10" by setF(10) in the A constructor.

Before Refactoring:

public class A {
    private int f;    
    public A() {
        f = 10;
    }
    int m(int x) {
        return f;
    }
}

Resulting Program:

public class A {
    private int f;    
    public A() {
        f = 10;
    }
    int m(int x) {
        return getF();
    }

    /**
     * @return the f
     */
    public int getF() {
        return f;
    }

    /**
     * @param f the f to set
     */
    public void setF(int f) {
        this.f = f;
    }
}
Comment 1 Ralph Ruijs 2015-07-30 07:58:36 UTC
From sourcemaking.com:
Comment 2 Ralph Ruijs 2015-07-30 08:01:05 UTC
When you are using self-encapsulation you have to be careful about using the setting method in the constructor. Often it is assumed that you use the setting method for changes after the object is created, so you may have different behavior in the setter than you have when initializing.