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 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 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 |
|---|---|
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.
|
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, 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
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 processCopyright © 2001-2021. All Rights Reserved.