Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 258
BeeOnRope Avatar asked Jun 15 '12 01:06

BeeOnRope


People also ask

What happens if @override is not used Java?

It will throw a compilation error if you have the annotation on a method you're not actually overriding the superclass method. The most common case where this is useful is when you are changing a method in the base class to have a different parameter list.

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.

Is it necessary to write @override annotation of overridden method?

@Override annotation is used when we override a method in sub class. Generally novice developers overlook this feature as it is not mandatory to use this annotation while overriding the method.

What does @override annotation mean to which methods ITS is applied?

The @Override annotation indicates that the child class method is over-writing its base class method. The @Override annotation can be useful for two reasons. 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.


2 Answers

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.

like image 179
Deepak Azad Avatar answered Oct 13 '22 21:10

Deepak Azad


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.

like image 28
Ira Baxter Avatar answered Oct 13 '22 21:10

Ira Baxter