Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Eclipse add @Override annotations in "Generate Delegate Methods" refactoring?

In Eclipse, if I try to apply a "Generate Delegate Methods" refactoring to a Java class like the following:

class Foo implements Bar {
   Bar bar;
}

Eclipse doesn't generate @Override annotations for the generated methods. Is this a bug or is there a good reason for this?

like image 629
Jeff Axelrod Avatar asked May 23 '11 02:05

Jeff Axelrod


3 Answers

This doesn't answer your question of why it doesn't add the @Override annotation when you generate delegate methods, but you can add a save action to do it for you upon saving the file. Go to

Window -> Preferences -> Java -> Editor -> Save Actions

Eclipse Save Actions

Notice the entries for @Override in the Additional actions section. If those are missing for you, click the Configure... button and select the Missing Code tab and select those options:

enter image description here

If you want to add the annotations to an entire project or package in bulk, select the project or package (or multiples), right-click, and choose Source > Clean up...

In the Clean Up dialog, you can configure a custom profile that includes adding missing annotations.

like image 75
laz Avatar answered Sep 17 '22 08:09

laz


No good reason, but not a "bug", per se; just a shortcoming. Probably the refactoring is just older than @Override -- older than annotations in general, of course -- and has never been updated.

like image 30
Ernest Friedman-Hill Avatar answered Sep 21 '22 08:09

Ernest Friedman-Hill


Sometimes settings in Window -> Preferences -> Java -> Editor -> Save Actions cause undesired effects especially on XML files while developing Android applications (been there, experienced that). IMHO better solution is to use +1 or right click on package (or class etc...) and then choose Source -> Clean up to add all of your missing @Override annotations.

like image 40
Nenad Bulatović Avatar answered Sep 17 '22 08:09

Nenad Bulatović