| Summary: | Eclipse encapsulates a field in the wrong way. | ||
|---|---|---|---|
| Product: | [Eclipse Project] JDT | Reporter: | Melina Mongiovi <melmongiovi> |
| Component: | UI | Assignee: | 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: | |||
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".
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? 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.
This works as expected. See comment #3 from Timo. |
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; } }