Any tool can help refactor field into get/set pairs of method

3.9k Views Asked by At

Wondering if there is tool to assist this kind of refactoring.

5

There are 5 best solutions below

2
On BEST ANSWER

Eclipse is an awesome IDE! right click, go to Source, "Generate Getters and Setters..."

... or...

enter image description here


In Netbeans... right click, go to Refactor, "Encapsulate Fields"

6
On

It's a standard feature of the eclipse IDE (and of other IDEs too, I suppose).

Shortcut: ALT+s -> r shows the getter/setter dialog (if a class with class members was selected)

BTW - autogenerating getter/setter stubs for class members is not a refactoring.


Code refactoring is "a disciplined way to restructure code", undertaken in order to improve some of the nonfunctional attributes of the software. (wikipedia)

Autogenerating those stubs does not restructure the (existing) code. It automatically adds method implementations. And, if we look at eclipse, we don't find the according action in the list of refactorings (additional hint).

Further Reading: Self Encapsulate Field

1
On

Eclipse has the functionality of generate getters and setters.

Source > Generate setters and getters

Source: Generate Getters and Setters

0
On

eclipse provides this. You can use my plugin as well : https://stackoverflow.com/questions/2733311/copy-field-and-methods/2734647#2734647.

0
On

Depending on what IDE you use, this is a standard feature provided. For instance, in Netbeans - right click on your java class and choose Refactor -> Encapsulate Fields. You can additionally select the accessors visibility, order of appearance and insert point.