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

Bug 471955

Summary: Encapsulate Field Refactoring encapsulates a private field
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:34:37 EDT
According to Fowler [1] definition about the Encapsulate Field Refactoring, we can only encapsulate public fields. So, this transformation should not be allowed by the tool.

Before Refactoring:

class A {
    private int f = 10;    
}

After Refactoring:

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


[1] Martin Fowler. Refactoring: improving the design of existing code. Addison-Wesley Longman Publishing Company, Inc., Boston, MA, USA, 1999.
Comment 1 Andrey Loskutov CLA 2015-07-06 13:36:02 EDT
Please report Java refactoring related bugs to JDT product.
Comment 2 Timo Kinnunen CLA 2015-07-07 18:26:04 EDT
According to my experience that book is 16 years out-of-date and I can encapsulate private fields perfectly fine whenever I want as long as I remember to select "Field access in declaring type: use getter and setter". Having to do it without the aid of a tool would be inconvenient.
Comment 3 Noopur Gupta CLA 2015-07-08 04:17:25 EDT
(In reply to Timo Kinnunen from comment #2)
> According to my experience that book is 16 years out-of-date and I can
> encapsulate private fields perfectly fine whenever I want as long as I
> remember to select "Field access in declaring type: use getter and setter".

> Having to do it without the aid of a tool would be inconvenient.
+1.
Comment 4 Melina Mongiovi CLA 2015-09-01 08:07:46 EDT
(In reply to Timo Kinnunen from comment #2)
> According to my experience that book is 16 years out-of-date and I can
> encapsulate private fields perfectly fine whenever I want as long as I
> remember to select "Field access in declaring type: use getter and setter".
> Having to do it without the aid of a tool would be inconvenient.

Encapsulate Field (http://www.refactoring.com/catalog/encapsulateField.html) and Self Encapsulate Field (http://www.refactoring.com/catalog/selfEncapsulateField.html) are different kinds of refactorings. Encapsulate Field is applied when there is a public field. Self Encapsulate Field is applied in a private field. I think it may confuse the user. In this case, the refactoring applied was Self Encapsulate Field.
I would suggest to report a warning message to the user.