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

后端 未结 1 1445
盖世英雄少女心
盖世英雄少女心 2020-12-15 05:20

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 t

相关标签:
1条回答
  • 2020-12-15 06:15

    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.

    0 讨论(0)
提交回复
热议问题