Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enforce usage of the @Override annotation? [closed]

Tags:

Is there a static analysis tool that can enforce usage of the @Override annotation consistently that can be run outside of an IDE? CheckStyle has a MissingOverride check but it only applies to methods that use the @inheritDoc Javadoc tag. I'm looking for a tool that can be run in a new build configuration on a continuous integration machine.

like image 814
Craig P. Motlin Avatar asked Apr 26 '11 17:04

Craig P. Motlin


People also ask

Is @override annotation mandatory?

@Override @Override annotation informs the compiler that the element is meant to override an element declared in a superclass. Overriding methods will be discussed in Interfaces and Inheritance. While it is not required to use this annotation when overriding a method, it helps to prevent errors.

What happens if you don't use @override Java?

If you don't use the annotation, the sub-class method will be treated as a new method in the subclass (rather than the overriding method). 2) It improves the code's readability.

What is the best reason for applying the @override annotation to a method?

The @Override annotation indicates that the child class method is over-writing its base class method. It extracts a warning from the compiler if the annotated method doesn't actually override anything. It can improve the readability of the source code.

Is it necessary to use @override in Java?

It is not necessary, but it is highly recommended. It keeps you from shooting yourself in the foot. It helps prevent the case when you write a function that you think overrides another one but you misspelled something and you get completely unexpected behavior.


2 Answers

PMD now has a MissingOverride rule.

What about PMD or Findbugs? PMD enables that you can even write your own rule (if it isn't disposable in the default rule set).

https://pmd.github.io/pmd-6.3.0/pmd_userdocs_extending_writing_pmd_rules.html

like image 124
marcospereira Avatar answered Feb 20 '23 09:02

marcospereira


I am using Sonar wich gives me the warnings but most importantly I am using eclipse (and so does my team) and I have set an option "add missing @Overrides" to be done on save action.

like image 22
bartosz.r Avatar answered Feb 20 '23 08:02

bartosz.r