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.
-
Slightly different, but related question [here] (http://stackoverflow.com/questions/4330275/javac-xlintoverrides-not-working) – wolfcastle Apr 26 '11 at 22:28
-
http://stackoverflow.com/questions/2335655/why-is-javac-failing-on-override-annotation – andersoj Apr 27 '11 at 11:04
5 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

- 8,008
- 26
- 77
- 177

- 12,045
- 3
- 46
- 52
-
1FindBugs does bytecode analysis. Does @Override make it into the bytecode? – Craig P. Motlin Apr 27 '11 at 14:18
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.

- 454
- 4
- 10
-
Sonar is just a report - it cannot enforce anything. Can eclipse be run from the command line? – Craig P. Motlin Apr 27 '11 at 14:16
-
@Craig P. Motlin running eclipse from command line is possible but not easy. Here you can find a link to a similar solution. Someone is running eclipse from command line to enforce code style formatting: http://blogs.operationaldynamics.com/andrew/software/java-gnome/eclipse-code-format-from-command-line.html. Personally I have to admit, that the rule to add missing overrides helped me alot with code refactoring. I have applied it first to the code to find all overrides, then after changing some base methods I'cve got code compile errors (which is what I wanted to get) that I can fix. – bartosz.r Apr 28 '11 at 08:42
Error Prone has a MissingOverride Pattern It appears that errorprone is evaluated at compile time. By default this is a warning but I configured it to be an error.

- 16,274
- 24
- 118
- 243
-
I just tested this and it works great. Example: `BaseApplication.java:56: warning: [MissingOverride] run implements method in Application; expected @Override` – lightswitch05 Jan 18 '17 at 23:03
One way is to use TeamCity's "Inspection" runner. I'm not sure if it really qualifies as running outside the IDE since it's configured in IntelliJ and it works by running IntelliJ in headless mode on the TeamCity side.

- 26,452
- 17
- 99
- 126
-
3If you use IntelliJ IDEA it has an inspection for doing exactly this, but it isn't enabled by default, you can go to settings > inspections > Missing Override Annotation – Jaime Hablutzel Oct 05 '11 at 22:52
-
Thanks, that's the inspection I was referring to in my answer. – Craig P. Motlin Oct 06 '11 at 14:59
On IntelliJ 2017, go to Settings -> Editor -> Inspections -> Missing @Override annotation

- 73
- 1
- 3
-
That creates unnecessary dependency on the IDE. The analysis should be done with the build-tools and its plugins. – Kalle Richter May 21 '18 at 02:17
-
that's the selected answer, I just put how to configure the inspection on intelliJ 2017 since the menu changed since 2011, in any case if you think my answer deserves a negative vote you should be consistent and do the same for all the other comments with same response. Cheers. – LucianoMdA Dec 27 '18 at 15:02
-
The other answers do not refer to IDE-specific tools, but instead tools which can be integrated in the build process which is ideally already independent from the IDE and remains independent after adding the tool which enforces `@Override` annotations. Furthermore your approach doesn't enforce the annotations since they're not there if you don't click on the control you mention while your build succeeds. You're providing a solution to solve the problem of missing annotations which is helpful as a comment on one of the answers. – Kalle Richter Dec 27 '18 at 15:11