MissingOverride

Since Checkstyle 5.0

Description

Verifies that the @Override annotation is present when the @inheritDoc javadoc tag is present.

Rationale: The @Override annotation helps compiler tools ensure that an override is actually occurring. It is quite easy to accidentally overload a method or hide a static method and using the @Override annotation points out these problems.

This check will log a violation if using the @inheritDoc tag on a method that is not valid (ex: private, or static method).

There is a slight difference between the @Override annotation in Java 5 versus Java 6 and above. In Java 5, any method overridden from an interface cannot be annotated with @Override. In Java 6 this behavior is allowed.

As a result of the aforementioned difference between Java 5 and Java 6, a property called javaFiveCompatibility is available. This property will only check classes, interfaces, etc. that do not contain the extends or implements keyword or are not anonymous classes. This means it only checks methods overridden from java.lang.Object. Java 5 Compatibility mode severely limits this check. It is recommended to only use it on Java 5 source.

Properties

name description type default value since
javaFiveCompatibility Enable java 5 compatibility mode. boolean false 5.0

Examples

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="MissingOverride"/>
  </module>
</module>

Example:


class Example1 extends ParentClass1 {

  /** {@inheritDoc} */
  @Override
  public void test1() {}

  /** {@inheritDoc} */
  // violation 2 lines below """Must include @java.lang.Override annotation when
  //  '@inheritDoc' Javadoc tag exists."""
  public void test2() {}

  /** {@inheritDoc} */
  // violation below '{@inheritDoc} tag is not valid at this location.'
  private void test3() {}

  /** {@inheritDoc} */
  // violation below '{@inheritDoc} tag is not valid at this location.'
  public static void test4() {}

  /** {@inheritDoc} */
  // violation 2 lines below """Must include @java.lang.Override annotation when
  //  '@inheritDoc' Javadoc tag exists."""
  public boolean equals(Object o) {
    return o == this;
  }
}

class Example1Impl implements InterfaceB {
  /** {@inheritDoc} */
  // violation 2 lines below """Must include @java.lang.Override annotation when
  //  '@inheritDoc' Javadoc tag exists."""
  public void test() {}
}

To configure the check for the javaFiveCompatibility mode:


<module name="Checker">
  <module name="TreeWalker">
    <module name="MissingOverride">
      <property name="javaFiveCompatibility"
                value="true"/>
    </module>
  </module>
</module>

Example:


class Example2 extends ParentClass1 {

  /** {@inheritDoc} */
  @Override
  public void test1() {}

  /** {@inheritDoc} */
  // ok, javaFiveCompatibility is true

  public void test2() {}

  /** {@inheritDoc} */
  // violation below '{@inheritDoc} tag is not valid at this location.'
  private void test3() {}

  /** {@inheritDoc} */
  // violation below '{@inheritDoc} tag is not valid at this location.'
  public static void test4() {}

  /** {@inheritDoc} */
  // ok, javaFiveCompatibility is true

  public boolean equals(Object o) {
    return o == this;
  }
}

class Example2Impl implements InterfaceB {
  /** {@inheritDoc} */
  // ok, javaFiveCompatibility is true

  public void test() {}
}

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker