JDK-6329722 : Warning for overriding methods not using @Override annotation
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0,7
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: generic
  • CPU: generic
  • Submitted: 2005-09-28
  • Updated: 2016-05-19
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

EVALUATION A good idea.
2005-09-28

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