Class MissingOverrideCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.AutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractCheck
-
- com.puppycrawl.tools.checkstyle.checks.annotation.MissingOverrideCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
public final class MissingOverrideCheck extends AbstractCheck
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 fromjava.lang.Object
. Java 5 Compatibility mode severely limits this check. It is recommended to only use it on Java 5 source.-
Property
javaFiveCompatibility
- Enable java 5 compatibility mode. Type isboolean
. Default value isfalse
.
To configure the check:
<module name="MissingOverride"/>
Example:
class Test extends SuperClass { /** {@inheritDoc} */ @Override public void test1() { // OK } /** {@inheritDoc} */ public void test2() { // violation, should be annotated with @Override } /** {@inheritDoc} */ private void test3() { // violation, using the @inheritDoc tag on private method } /** {@inheritDoc} */ public static void test4() { // violation, using the @inheritDoc tag on static method } }
To configure the check for the
javaFiveCompatibility
mode:<module name="MissingOverride"> <property name="javaFiveCompatibility" value="true"/> </module>
Example:
class Test1 { /** {@inheritDoc} */ public void equals() { // violation, should be annotated with @Override } } interface Test2 { /** {@inheritDoc} */ void test(); // violation, should be annotated with @Override } class Test3 extends SuperClass { /** {@inheritDoc} */ public void test() { // OK, is ignored because class extends other class } } class Test4 implements SuperInterface { /** {@inheritDoc} */ public void test() { // OK, is ignored because class implements interface } } class Test5 { Runnable r = new Runnable() { /** {@inheritDoc} */ public void run() { // OK, is ignored because class is anonymous class } }; }
Parent is
com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
-
annotation.missing.override
-
tag.not.valid.on
- Since:
- 5.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
AutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
MSG_KEY_ANNOTATION_MISSING_OVERRIDE
A key is pointing to the warning message text in "messages.properties" file.static java.lang.String
MSG_KEY_TAG_NOT_VALID_ON
A key is pointing to the warning message text in "messages.properties" file.
-
Constructor Summary
Constructors Constructor Description MissingOverrideCheck()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int[]
getAcceptableTokens()
The configurable token set.int[]
getDefaultTokens()
Returns the default token a check is interested in.int[]
getRequiredTokens()
The tokens that this check must be registered for.void
setJavaFiveCompatibility(boolean compatibility)
Setter to enable java 5 compatibility mode.void
visitToken(DetailAST ast)
Called to process a token.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheck
beginTree, clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean
configure, contextualize, getConfiguration, setupChild
-
-
-
-
Field Detail
-
MSG_KEY_TAG_NOT_VALID_ON
public static final java.lang.String MSG_KEY_TAG_NOT_VALID_ON
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_ANNOTATION_MISSING_OVERRIDE
public static final java.lang.String MSG_KEY_ANNOTATION_MISSING_OVERRIDE
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
-
Method Detail
-
setJavaFiveCompatibility
public void setJavaFiveCompatibility(boolean compatibility)
Setter to enable java 5 compatibility mode.- Parameters:
compatibility
- compatibility or not
-
getDefaultTokens
public int[] getDefaultTokens()
Description copied from class:AbstractCheck
Returns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
getDefaultTokens
in classAbstractCheck
- Returns:
- the default tokens
- See Also:
TokenTypes
-
getAcceptableTokens
public int[] getAcceptableTokens()
Description copied from class:AbstractCheck
The configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
getAcceptableTokens
in classAbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
TokenTypes
-
getRequiredTokens
public int[] getRequiredTokens()
Description copied from class:AbstractCheck
The tokens that this check must be registered for.- Specified by:
getRequiredTokens
in classAbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
TokenTypes
-
visitToken
public void visitToken(DetailAST ast)
Description copied from class:AbstractCheck
Called to process a token.- Overrides:
visitToken
in classAbstractCheck
- Parameters:
ast
- the token to process
-
-