Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 471959

Summary: Eclipse encapsulates a field in the wrong way.
Product: [Eclipse Project] JDT Reporter: Melina Mongiovi <melmongiovi>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED INVALID QA Contact:
Severity: normal    
Priority: P3 CC: noopur_gupta, stephan.herrmann
Version: 4.3   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X   
Whiteboard:

Description Melina Mongiovi CLA 2015-07-06 13:48:31 EDT
Eclipse encapsulates the field A.f in a wrong way. 
When encapsulating field A.f, it updates the method A.m to call the "set" method. But the correctly way is "new A().setF(0)" instead of "setF(0)".

Before Refactoring:

public class A {
    int f;
    void m() {
        (new A().f) = 0;
    }
}


Resulting Program:

public class A {
    private int f;
    void m() {
        setF(0);
    }
    public int getF() {
        return f;
    }
    public void setF(int f) {
        this.f = f;
    }
}
Comment 1 Melina Mongiovi CLA 2015-07-06 13:56:30 EDT
Sorry, the resulting program is following:


public class A {
private int f;
void m() {
(new A().f) = 0;
}
public void setF(int f) {
	this.f = f;
}
public int getF() {
	return f;
}
}


It does not update the field using the "set" Method.
The correctly way is "new A().setF(0)" instead of "(new A().f) = 0".
Comment 2 Andrey Loskutov CLA 2015-07-06 14:03:57 EDT
Melina, thanks for reporting, but can you *please* in the future report Java tooling related issues to the JDT product, *not* to the platform product? Would it be possible please?
Comment 3 Timo Kinnunen CLA 2015-07-07 18:56:48 EDT
This works for me and produces:

public class A {
	private int f;

	void m() {
		new A().setF(0);
	}

	int getF() {
		return f;
	}

	void setF(int f) {
		this.f = f;
	}
}

I am using "Field access in declaring type: use getter and setter" setting in the Encapsulate Field dialog. If you have "keep field reference" selected instead then I would expect your version as a result.
Comment 4 Noopur Gupta CLA 2015-07-08 04:21:32 EDT
This works as expected. See comment #3 from Timo.