public class TrailingCommentCheck extends AbstractCheck
The check to ensure that requires that comments be the only thing on a line.
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 and the text of
the comment will be trimmed before matching.)
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>
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
trailing.comments
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static 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 |
---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
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 |
setFormat(Pattern pattern)
Setter to specify pattern for strings allowed before the comment.
|
void |
setLegalComment(Pattern legalComment)
Setter to define pattern for text allowed in trailing comments.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearMessages, destroy, finishTree, getFileContents, getLine, getLines, getMessages, getTabWidth, getTokenNames, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokens
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
public static final String MSG_KEY
public TrailingCommentCheck()
public void setLegalComment(Pattern legalComment)
legalComment
- pattern to set.public final void setFormat(Pattern pattern)
pattern
- a patternpublic 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 processpublic void beginTree(DetailAST rootAST)
AbstractCheck
beginTree
in class AbstractCheck
rootAST
- the root of the treeCopyright © 2001–2020. All rights reserved.