com.puppycrawl.tools.checkstyle.checks

Class TrailingCommentCheck

Implemented Interfaces:
Configurable, Contextualizable

public class TrailingCommentCheck
extends AbstractFormatCheck

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 line, i.e. it accept the following: Thread.sleep( 10 <some comment here> ); Format property is intended to deal with the "} // while" example.

Rationale: Steve McConnel 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 justfication: His comments on being hard to maintain when the size of the line changes are even more important in the age of automated refactorings.

To configure the check so it enforces only comment on a line:

 <module name="TrailingComment">
    <property name="format" value="^\\s*$"/>
 </module>
 
Author:
o_sukhodolsky

Constructor Summary

TrailingCommentCheck()
Creates new instance of the check.

Method Summary

void
beginTree(DetailAST aRootAST)
int[]
getDefaultTokens()
void
setLegalComment(String aFormat)
Sets patter for legal trailing comments.
void
visitToken(DetailAST aAST)

Methods inherited from class com.puppycrawl.tools.checkstyle.checks.AbstractFormatCheck

getFormat, getRegexp, setCompileFlags, setFormat

Methods inherited from class com.puppycrawl.tools.checkstyle.api.Check

beginTree, destroy, finishTree, getAcceptableTokens, getClassLoader, getDefaultTokens, getFileContents, getLines, getRequiredTokens, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokens, visitToken

Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter

getId, getMessageBundle, getSeverity, getSeverityLevel, log, log, log, log, log, log, log, log, log, log, log, setId, setSeverity

Methods inherited from class com.puppycrawl.tools.checkstyle.api.AutomaticBean

configure, contextualize, finishLocalSetup, getConfiguration, setupChild

Constructor Details

TrailingCommentCheck

public TrailingCommentCheck()
            throws ConversionException
Creates new instance of the check.

Method Details

beginTree

public void beginTree(DetailAST aRootAST)
Overrides:
beginTree in interface Check

getDefaultTokens

public int[] getDefaultTokens()
Overrides:
getDefaultTokens in interface Check

setLegalComment

public void setLegalComment(String aFormat)
            throws ConversionException
Sets patter for legal trailing comments.
Parameters:
aFormat - format to set.

visitToken

public void visitToken(DetailAST aAST)
Overrides:
visitToken in interface Check