Refactoring in eclipse: how to add a field, getter, setter to all interface implementations?

how can I add a field, getter and setter to all implementations of MyInterface (in the current project or folder?)

+4
source share
2 answers
  • Open the source file containing the interface. Select the interface name and press F4 (Open hierarchy type).
  • In the type hierarchy view, select all classes that implement your interface. The hierarchy is displayed as a tree, so the choice should be very simple.
  • Right-click your selection, select Source , then Override/Implement Methods...

Mission Complete.

[Changed]

When I wrote these steps, I thought you intended to stub the recently introduced interface methods in all classes implementing this interface.

Now, when I re-read your request, it is difficult for me to understand exactly what you want to do. You wrote:

how can i add a field, getter and setter to all implementations of MyInterface

So, you have an interface named MyInterface and 1000 classes that implement it. You would like to enter a new field, getter and setter for this field. Therefore, I assume that my (and possibly others) first problem is that you cannot add a field to an interface unless it is final - so your desire to "add a field to an interface" just doesn't sound right.

I suppose this will help if you give us a chart / explanation of your hierarchy at 30,000 feet and what you are trying to accomplish.

+2
source

I do not think there is one step for this. You can add a getter and setter to the interface, and then let the compiler tell you where your developers are, and this should be a simple copy and paste to put the field, getter and setter in each class after the first.

Alternatively, you can convert your interface to an abstract class or introduce an abstract class between your interface and specific classes, but this is only if your classes do not extend other classes yet.

0
source

All Articles