public final class MissingOverrideCheck extends AbstractCheck
This class is used to verify 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
<module name="MissingOverride"> <property name="javaFiveCompatibility" value="true"/> </module>
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
FQ_OVERRIDE
Fully-qualified
Override annotation name. |
private boolean |
javaFiveCompatibility
Java 5 compatibility option.
|
private static java.util.regex.Pattern |
MATCH_INHERIT_DOC
Compiled regexp to match Javadoc tags with no argument and {}.
|
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.
|
private static java.lang.String |
OVERRIDE
Override annotation name. |
Constructor and Description |
---|
MissingOverrideCheck() |
Modifier and Type | Method and Description |
---|---|
private static boolean |
containsJavadocTag(TextBlock javadoc)
Checks to see if the text block contains a inheritDoc tag.
|
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)
Sets Java 5 compatibility mode.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens
getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverity
configure, contextualize, finishLocalSetup, getConfiguration, setupChild
public static final java.lang.String MSG_KEY_TAG_NOT_VALID_ON
public static final java.lang.String MSG_KEY_ANNOTATION_MISSING_OVERRIDE
private static final java.lang.String OVERRIDE
Override
annotation name.private static final java.lang.String FQ_OVERRIDE
Override
annotation name.private static final java.util.regex.Pattern MATCH_INHERIT_DOC
private boolean javaFiveCompatibility
setJavaFiveCompatibility(boolean)
public void setJavaFiveCompatibility(boolean compatibility)
In Java 5, this check could flag code that is not valid for the Override annotation even though it is a proper override. See the class documentation for more information.
Set this to true to turn on Java 5 compatibility mode. Set this to false to turn off Java 5 compatibility mode.
compatibility
- compatibility or notpublic int[] getDefaultTokens()
AbstractCheck
getDefaultTokens
in class AbstractCheck
TokenTypes
public int[] getAcceptableTokens()
AbstractCheck
getAcceptableTokens
in class AbstractCheck
TokenTypes
public int[] getRequiredTokens()
AbstractCheck
getRequiredTokens
in class AbstractCheck
TokenTypes
public void visitToken(DetailAST ast)
AbstractCheck
visitToken
in class AbstractCheck
ast
- the token to processprivate static boolean containsJavadocTag(TextBlock javadoc)
javadoc
- the javadoc of the AST