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

Bug 471961

Summary: Encapsulate Field refactoring does not implement a correct "get" method.
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 14:00:30 EDT
When encapsulating a field, it does not implement a correct "get" method.
The program has a "get" Method returning 0. After the transformation to encapsulate field A.f, the getF still returns 0 instead of "f".

Before Refactoring:

public class A {
    int f;
    public int getF(){
        return 0;
    }
}


Resulting Program:

public class A {
    private int f;
    public int getF(){
        return 0;
    }
	public void setF(int f) {
		this.f = f;
	}
}
Comment 1 Timo Kinnunen CLA 2015-07-07 18:42:42 EDT
Perhaps the meaning of "f" is "work-units remaining to be consumed" and in this implementation all work-units are immediately consumed upon receipt without actually doing any work on them, making this implementation a "null-implementation". It would be bad for Encapsulate Field to change such a null-implementation in ways that make it not-a-null-implementation.
Comment 2 Noopur Gupta CLA 2015-07-08 04:30:34 EDT
This is as expected. We use local getter/setter if it already exists.
Comment 3 Melina Mongiovi CLA 2015-07-24 15:11:00 EDT
(In reply to Noopur Gupta from comment #2)
> This is as expected. We use local getter/setter if it already exists.

I suggest you to report a warning message to the user before applying the refactoring. Then, the user can decide about leave the class abstract or not.