United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6329722 : Warning for overriding methods not using @Override annotation

Details
Type:
Enhancement
Submit Date:
2005-09-28
Status:
Open
Updated Date:
2016-05-19
Project Name:
JDK
Resolved Date:
Component:
tools
OS:
generic
Sub-Component:
javac
CPU:
generic
Priority:
P4
Resolution:
Unresolved
Affected Versions:
5.0,7
Targeted Versions:

Related Reports

Sub Tasks

Description
It should be possible to get a warning for overriding methods
without the @Override annotation.  For example:

class Super { void foo() {} }
class Sub1 extends Super { void foo() {} } // warning
class Sub2 extends Super { @Override foo() {} } // no warning

                                    

Comments
deferring to 9 as low priority
                                     
2013-05-10
SUGGESTED FIX

Index: src/share/classes/com/sun/tools/javac/code/Lint.java
@@ -124,10 +126,15 @@
         * comment, but which do not have {@code @Deprecated} annotation.
         */
        DEP_ANN("dep-ann"),
 
        /**
+        * Warn about overriding methods without {@code @Override}.
+        */
+       AT_OVERRIDE("override"),
+
+       /**
         * Warn about division by constant integer 0.
         */
        DIVZERO("divzero"),
 
        /**

Index: src/share/classes/com/sun/tools/javac/comp/Check.java
@@ -1632,15 +1632,24 @@
        log.error(pos, "invalid.annotation.member.type");
     }
 
     /** Check the annotations of a symbol.
      */
-    public void validateAnnotations(List<Annotation> annotations, Symbol s) {
-       if (skipAnnotations) return;
-       for (Annotation a : annotations)
+    public void validateAnnotations(int pos, List<Annotation> annotations, Symbol s) {
+       if (skipAnnotations)
+           return;
+       boolean foundOverrideType = !lint.isEnabled(Lint.LintCategory.AT_OVERRIDE);
+       for (Annotation a : annotations) {
+           if (a.annotationType.type.tsym == syms.overrideType.tsym)
+               foundOverrideType = true;
            validateAnnotation(a, s);
     }
+       if (!foundOverrideType && s.kind == MTH && pos != Position.NOPOS) {
+           if (isOverrider(s))
+               log.rawWarning(pos, "not using @Override");
+       }
+    }
 
     /** Check an annotation of a symbol.
      */
     public void validateAnnotation(Annotation a, Symbol s) {
        validateAnnotation(a);
                                     
2005-09-28
EVALUATION

A good idea.
                                     
2005-09-28



Hardware and Software, Engineered to Work Together