Eclipse JDT: Is there a refactoring to replace direct field accesses with setter/getter methods?
Asked Answered
G

1

22

I know I can generate setters and getters for fields in the Eclipse source menu, but I'm very surprised it doesn't offer to replace the direct field accessors with calls to the newly created methods.

Does anyone know how to do this short of manual search and replace?

Thanks!

Gook answered 19/10, 2010 at 22:11 Comment(0)
I
30

There is a way. Focus on the attribute, right click. Select "Refactor" -> "Encapsulate Field".

(Edited to explain why it works, even though the menu's title doesn't imply it)

The "Encapsulate Field" dialog-box will create getters/setters for you if they don't exist, and will change field-access to go through the getters/setters. The dialog automatically detects existing getters/setters so, for the requirements that you had mentioned, that would do the trick.

Irena answered 19/10, 2010 at 22:35 Comment(2)
Thanks so much; it works beautifully! I really need to read through the JDT refactoring documentation.Gook
Here's another good answer to the question: https://mcmap.net/q/588707/-eclipse-generate-getters-and-setters-and-automatically-apply-themDreary

© 2022 - 2024 — McMap. All rights reserved.