Method for adding an interface, how to add a default implementation for all classes

Now I have the DetailUI interface, and I have thousands of classes that implements the DetailUI interface, and I want to add the isXXX () method, and I want the whole current class to automatically implement the method, and the method returns true by default, is there an easy way to eclipse ?

+4
source share
3 answers

Maybe there is another way, but you can

  • add method declaration in interface
  • go to the "Problems" view, select one of the compilation errors caused by the new method, right-click and select "Quick Fix"
  • Select "Add Unrealized Methods" and select all other classes at the bottom of the dialog box.

This will force the generated methods to return false, though:

public boolean isXXX() { // TODO Auto-generated method stub return false; } 

So, you can search and replace with the following regular expression to search for:

 // TODO Auto-generated method stub\R\s*return false; 

And the following replacement line:

 return true; 
+4
source

Yes. Go to the "Problems" view and select all the problems that indicate that you are missing the method ("Type XYZ should implement the inherited ..."). With all of them selected, right-click and select Quick Fix.

+2
source

The above solutions may not be ideal in the case of a class hierarchy that implements the interface - if there is an abstract superclass, it will not flag an error due to another missing method. It was more convenient for me to look at the hierarchy type view (F4 on Windows) to find the top-level classes and add a method there.

Another alternative is to switch to Java 1.8, which allows you to implement by default. https://docs.oracle.com/javase/tutorial/java/IandI/defaultmethods.html

0
source

Source: https://habr.com/ru/post/1447880/


All Articles