BeeOnRope
BeeOnRope

Reputation: 65046

How can I statically detect missing @Override annotations?

Between Java 5 and Java 6, the rules regarding @Override annotations for methods originating in interfaces (as opposed to superclasses) were changed - before they were not allowed, but after they were. However, they are not required by javac.

Some IDEs, like eclipse, can generate errors or warnings for such missing overrides. I'm looking for any kind of static analysis tool that can detect missing overrides, so I can report/block them programmatically.

It doesn't seem like any of the big ones I'm aware of like findbugs, et al can do it - probably because @Override only has source retention level, so is not present in .class files on which these tools operate, and source level tools like checkstyle don't understand the full class hierarchy.

Upvotes: 10

Views: 1416

Answers (2)

Ira Baxter
Ira Baxter

Reputation: 95410

You obviously need something that processes Java source code, and gives you access to the code structures and the attributes.

I understand Eclipse has some parsing machinery in JDT, and you might be able to use that.

Our DMS Software Reengineering Toolkit has a full Java source parser (including attribute capture), full name and type resolution, and the ability to read many source files at once. DMS is designed to let you build your own custom analysis tool, so you can define the conditions under which you insist the attributes be present or not.

Because DMS also provides program transformation capability, you can even define customizations rules that will modify source code where it is clear what attributes should be present.

DMS and the Java languages rules are not trivial to learn; this takes some effort to configure. But if you really have thousands of developers, this cost is swamped by the inability to control what they are doing, so the investment makes sense.

Upvotes: 1

Deepak Azad
Deepak Azad

Reputation: 7923

You can just enable a save action in Eclipse to automatically add the missing @Override annotation - see Window > Preferences > Java > Editor > Save Actions > Missing Code > Add missing annotations.

Upvotes: 3

Related Questions