public class TrailingCommentCheck extends AbstractCheck
The check to ensure that lines with code do not end with comment.
For the case of // comments that means that the only thing that should precede
it is whitespace. It doesn't check comments if they do not end a line; for example,
it accepts the following: Thread.sleep( 10 /*some comment here*/ );
Format property is intended to deal with the } // while example.
Rationale: Steve McConnell in Code Complete suggests that endline comments are a bad practice. An end line comment would be one that is on the same line as actual code. For example:
a = b + c; // Some insightful comment d = e / f; // Another comment for this line
Quoting Code Complete for the justification:
McConnell's comments on being hard to maintain when the size of the line changes are even more important in the age of automated refactorings.
format - Specify pattern for strings allowed before the comment.
Type is java.util.regex.Pattern.
Default value is "^[\s});]*$".
legalComment - Define pattern for text allowed in trailing comments.
This pattern will not be applied to multiline comments.
Type is java.util.regex.Pattern.
Default value is null.
To configure the check:
<module name="TrailingComment"/>
To configure the check so it enforces only comment on a line:
<module name="TrailingComment"> <property name="format" value="^\\s*$"/> </module>
Example for trailing comments check to suppress specific trailing comment:
public class Test {
int a; // SUPPRESS CHECKSTYLE
int b; // NOPMD
int c; // NOSONAR
int d; // violation, not suppressed
}
To configure check so that trailing comment with exact comments like "SUPPRESS CHECKSTYLE", "NOPMD", "NOSONAR" are suppressed:
<module name="TrailingComment"/>
<module name="SuppressionXpathSingleFilter">
<property name="checks" value="TrailingCommentCheck"/>
<property name="query" value="//SINGLE_LINE_COMMENT
[./COMMENT_CONTENT[@text=' NOSONAR\n' or @text=' NOPMD\n'
or @text=' SUPPRESS CHECKSTYLE\n']]"/>
</module>
To configure check so that trailing comment starting with "SUPPRESS CHECKSTYLE", "NOPMD", "NOSONAR" are suppressed:
<module name="TrailingComment"/> <module name="SuppressionXpathSingleFilter">
<property name="checks" value="TrailingCommentCheck"/>
<property name="query" value="//SINGLE_LINE_COMMENT
[./COMMENT_CONTENT[starts-with(@text, ' NOPMD')]]"/>
<property name="query" value="//SINGLE_LINE_COMMENT
[./COMMENT_CONTENT[starts-with(@text, ' SUPPRESS CHECKSTYLE')]]"/>
<property name="query" value="//SINGLE_LINE_COMMENT
[./COMMENT_CONTENT[starts-with(@text, ' NOSONAR')]]"/>
</module>
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
trailing.comments
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private java.util.regex.Pattern |
format
Specify pattern for strings allowed before the comment.
|
private static java.util.regex.Pattern |
FORMAT_LINE
Specify pattern for strings to be formatted without comment specifiers.
|
private java.util.regex.Pattern |
legalComment
Define pattern for text allowed in trailing comments.
|
static java.lang.String |
MSG_KEY
A key is pointing to the warning message text in "messages.properties"
file.
|
| Constructor and Description |
|---|
TrailingCommentCheck() |
| Modifier and Type | Method and Description |
|---|---|
private void |
checkBlockComment(DetailAST ast)
Method to check if block comment is in correct format.
|
private void |
checkSingleLineComment(DetailAST ast)
Checks if single line comment is legal.
|
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.
|
boolean |
isCommentNodesRequired()
Whether comment nodes are required or not.
|
private boolean |
isLegalCommentContent(java.lang.String commentContent)
Checks if given comment content is legal.
|
void |
setFormat(java.util.regex.Pattern pattern)
Setter to specify pattern for strings allowed before the comment.
|
void |
setLegalComment(java.util.regex.Pattern legalComment)
Setter to define pattern for text allowed in trailing comments.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
beginTree, clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY
private static final java.util.regex.Pattern FORMAT_LINE
private java.util.regex.Pattern legalComment
private java.util.regex.Pattern format
public TrailingCommentCheck()
public void setLegalComment(java.util.regex.Pattern legalComment)
legalComment - pattern to set.public final void setFormat(java.util.regex.Pattern pattern)
pattern - a patternpublic boolean isCommentNodesRequired()
AbstractCheckisCommentNodesRequired in class AbstractCheckpublic int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate void checkSingleLineComment(DetailAST ast)
ast - Detail ast element to be checked.private void checkBlockComment(DetailAST ast)
ast - Detail ast element to be checked.private boolean isLegalCommentContent(java.lang.String commentContent)
commentContent - comment content to check.Copyright © 2001-2022. All Rights Reserved.