Class CommentsIndentationCheck
- All Implemented Interfaces:
- Configurable,- Contextualizable
- Since:
- 6.10
- 
Nested Class SummaryNested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBeanAbstractAutomaticBean.OutputStreamOptions
- 
Field SummaryFields
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionprivate static booleanChecks if the comment is inside a method call with same indentation of first expression. e.g:private booleanareSameLevelIndented(DetailAST comment, DetailAST prevStmt, DetailAST nextStmt) Checks if comment and next code statement (or previous code stmt like case in switch block) are indented at the same level, e.g.:private intcountEmptyLines(DetailAST startStatement, DetailAST endStatement) Count the number of empty lines between statements.private DetailASTfindPreviousStatement(DetailAST comment, DetailAST root) Finds a previous statement of the comment.private static DetailASTFinds the start token of method call chain.private static DetailASTFinds a token which begins the line.int[]The configurable token set.int[]Returns the default token a check is interested in.private static DetailASTgetDistributedPreviousStatement(DetailAST comment) Returns the first token of the distributed previous statement of comment.private static DetailASTgetFirstExpressionNodeFromMethodCall(DetailAST methodCall) Returns the first EXPR DetailAST child from parent of comment.private intgetLineStart(int lineNo) Get a column number where a code starts.private static StringgetMessageKey(DetailAST comment) Get a message key depending on a comment type.private static DetailASTgetNextStmt(DetailAST comment) Returns the next statement of a comment.private DetailASTgetNextToken(DetailAST checkedStatement) Get the token to start counting the number of lines to add to the distance aim from.private DetailASTgetOneLinePreviousStatement(DetailAST comment) Does pre-order traverse of abstract syntax tree to find the previous statement of the comment.private static DetailASTgetPrevCaseToken(DetailAST parentStatement) Gets previous case-token for comment.private DetailASTgetPreviousStatement(DetailAST comment) Returns the previous statement of a comment.private static DetailASTgetPrevStatementFromSwitchBlock(DetailAST comment) Gets comment's previous statement from switch block.private static DetailASTgetPrevStatementWhenCommentIsUnderCase(DetailAST parentStatement) Gets previous statement for comment which is placed immediately under case.int[]The tokens that this check must be registered for.private voidhandleCommentAtTheEndOfTheCodeBlock(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt) Handles a comment which is placed at the end of non-empty code block.private voidhandleCommentInEmptyCaseBlock(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt) Handles a comment which is placed within empty case block.private voidhandleCommentInEmptyCodeBlock(DetailAST comment, DetailAST nextStmt) Handles a comment which is placed within the empty code block.private voidhandleFallThroughComment(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt) Handles 'fall through' single-line comment.private static booleanisBlockStart(DetailAST root) Whether the AST node starts a block.private static booleanWhether the ast is a comment.private static booleanisCommentAtTheEndOfTheCodeBlock(DetailAST nextStmt) Checks whether a comment is placed at the end of the code block.private static booleanisCommentForMultiblock(DetailAST endBlockStmt) Whether the comment might have been used for the next block in a multi-block structure.booleanWhether comment nodes are required or not.private static booleanisDefinition(DetailAST previousSibling) Whether the statement is a kind of definition (method, class etc.).private booleanisDistributedExpression(DetailAST comment) Checks whether the previous statement of a comment is a method call chain or string concatenation statement distributed over two or more lines.private booleanisDistributedPreviousStatement(DetailAST comment) Checks whether the previous statement of a comment is distributed over two or more lines.private static booleanisDistributedReturnStatement(DetailAST commentPreviousSibling) Checks whether the previous statement of a comment is a distributed return statement.private static booleanisDistributedThrowStatement(DetailAST commentPreviousSibling) Checks whether the previous statement of a comment is a distributed throw statement.private static booleanisFallThroughComment(DetailAST prevStmt, DetailAST nextStmt) Checks whether comment is a 'fall through' comment.private static booleanisInEmptyCaseBlock(DetailAST prevStmt, DetailAST nextStmt) Checks whether case block is empty.private static booleanisInEmptyCodeBlock(DetailAST prevStmt, DetailAST nextStmt) Checks whether comment is placed in the empty code block.private booleanisOnPreviousLineIgnoringComments(DetailAST currentStatement, DetailAST checkedStatement) Checks whether the checked statement is on the previous line ignoring empty lines and lines which contain only comments.private static booleanisStatementWithPossibleCurlies(DetailAST previousSibling) Whether the statement can have or always have curly brackets.private booleanisTrailingBlockComment(DetailAST blockComment) Checks if current comment block is trailing comment, e.g.:private booleanisTrailingComment(DetailAST comment) Checks if current comment is a trailing comment.private booleanisTrailingSingleLineComment(DetailAST singleLineComment) Checks if current single-line comment is trailing comment, e.g.:private static booleanChecks whether there is a use of an object reference to invoke an object's method on line.private voidlogMultilineIndentation(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt) Logs comment which can have the same indentation level as next or previous statement.private voidvisitComment(DetailAST comment) Checks comment indentations over surrounding code, e.g.:voidvisitToken(DetailAST commentAst) Called to process a token.Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractCheckbeginTree, clearViolations, destroy, finishTree, getFileContents, getFilePath, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensMethods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporterfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityMethods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBeanconfigure, contextualize, getConfiguration, setupChild
- 
Field Details- 
MSG_KEY_SINGLEA key is pointing to the warning message text in "messages.properties" file.- See Also:
 
- 
MSG_KEY_BLOCKA key is pointing to the warning message text in "messages.properties" file.- See Also:
 
 
- 
- 
Constructor Details- 
CommentsIndentationCheckpublic CommentsIndentationCheck()
 
- 
- 
Method Details- 
getDefaultTokensDescription copied from class:AbstractCheckReturns the default token a check is interested in. Only used if the configuration for a check does not define the tokens.- Specified by:
- getDefaultTokensin class- AbstractCheck
- Returns:
- the default tokens
- See Also:
 
- 
getAcceptableTokensDescription copied from class:AbstractCheckThe configurable token set. Used to protect Checks against malicious users who specify an unacceptable token set in the configuration file. The default implementation returns the check's default tokens.- Specified by:
- getAcceptableTokensin class- AbstractCheck
- Returns:
- the token set this check is designed for.
- See Also:
 
- 
getRequiredTokensDescription copied from class:AbstractCheckThe tokens that this check must be registered for.- Specified by:
- getRequiredTokensin class- AbstractCheck
- Returns:
- the token set this must be registered for.
- See Also:
 
- 
isCommentNodesRequiredDescription copied from class:AbstractCheckWhether comment nodes are required or not.- Overrides:
- isCommentNodesRequiredin class- AbstractCheck
- Returns:
- false as a default value.
 
- 
visitTokenDescription copied from class:AbstractCheckCalled to process a token.- Overrides:
- visitTokenin class- AbstractCheck
- Parameters:
- commentAst- the token to process
 
- 
visitCommentChecks comment indentations over surrounding code, e.g.:// some comment - this is ok double d = 3.14; // some comment - this is <b>not</b> ok. double d1 = 5.0;- Parameters:
- comment- comment to check.
 
- 
getNextStmtReturns the next statement of a comment.- Parameters:
- comment- comment.
- Returns:
- the next statement of a comment.
 
- 
getPreviousStatementReturns the previous statement of a comment.- Parameters:
- comment- comment.
- Returns:
- the previous statement of a comment.
 
- 
isDistributedPreviousStatementChecks whether the previous statement of a comment is distributed over two or more lines.- Parameters:
- comment- comment to check.
- Returns:
- true if the previous statement of a comment is distributed over two or more lines.
 
- 
isDistributedExpressionChecks whether the previous statement of a comment is a method call chain or string concatenation statement distributed over two or more lines.- Parameters:
- comment- comment to check.
- Returns:
- true if the previous statement is a distributed expression.
 
- 
isStatementWithPossibleCurliesWhether the statement can have or always have curly brackets.- Parameters:
- previousSibling- the statement to check.
- Returns:
- true if the statement can have or always have curly brackets.
 
- 
isDefinitionWhether the statement is a kind of definition (method, class etc.).- Parameters:
- previousSibling- the statement to check.
- Returns:
- true if the statement is a kind of definition.
 
- 
isDistributedReturnStatementChecks whether the previous statement of a comment is a distributed return statement.- Parameters:
- commentPreviousSibling- previous sibling of the comment.
- Returns:
- true if the previous statement of a comment is a distributed return statement.
 
- 
isDistributedThrowStatementChecks whether the previous statement of a comment is a distributed throw statement.- Parameters:
- commentPreviousSibling- previous sibling of the comment.
- Returns:
- true if the previous statement of a comment is a distributed throw statement.
 
- 
getDistributedPreviousStatementReturns the first token of the distributed previous statement of comment.- Parameters:
- comment- comment to check.
- Returns:
- the first token of the distributed previous statement of comment.
 
- 
isInEmptyCaseBlockChecks whether case block is empty.- Parameters:
- prevStmt- next statement.
- nextStmt- previous statement.
- Returns:
- true if case block is empty.
 
- 
isFallThroughCommentChecks whether comment is a 'fall through' comment. For example:... case OPTION_ONE: int someVariable = 1; // fall through case OPTION_TWO: int a = 5; break; ...- Parameters:
- prevStmt- previous statement.
- nextStmt- next statement.
- Returns:
- true if a comment is a 'fall through' comment.
 
- 
isCommentAtTheEndOfTheCodeBlockChecks whether a comment is placed at the end of the code block.- Parameters:
- nextStmt- next statement.
- Returns:
- true if a comment is placed at the end of the block.
 
- 
isInEmptyCodeBlockChecks whether comment is placed in the empty code block. For example:... Note, the method does not treat empty case blocks.// empty code block...- Parameters:
- prevStmt- previous statement.
- nextStmt- next statement.
- Returns:
- true if comment is placed in the empty code block.
 
- 
handleCommentInEmptyCaseBlockprivate void handleCommentInEmptyCaseBlock(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt) Handles a comment which is placed within empty case block. Note, if comment is placed at the end of the empty case block, we have Checkstyle's limitations to clearly detect user intention of explanation target - above or below. The only case we can assume as a violation is when a single-line comment within the empty case block has indentation level that is lower than the indentation level of the next case token. For example:... case OPTION_ONE: // violation case OPTION_TWO: ...- Parameters:
- prevStmt- previous statement.
- comment- single-line comment.
- nextStmt- next statement.
 
- 
handleFallThroughCommentHandles 'fall through' single-line comment. Note, 'fall through' and similar comments can have indentation level as next or previous statement. For example:... case OPTION_ONE: int someVariable = 1; // fall through - OK case OPTION_TWO: int a = 5; break; ...... case OPTION_ONE: int someVariable = 1; // then init variable a - OK case OPTION_TWO: int a = 5; break; ...- Parameters:
- prevStmt- previous statement.
- comment- single-line comment.
- nextStmt- next statement.
 
- 
handleCommentAtTheEndOfTheCodeBlockprivate void handleCommentAtTheEndOfTheCodeBlock(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt) Handles a comment which is placed at the end of non-empty code block. Note, if single-line comment is placed at the end of non-empty block the comment should have the same indentation level as the previous statement. For example:if (a == true) { int b = 1; // comment }- Parameters:
- prevStmt- previous statement.
- comment- comment to check.
- nextStmt- next statement.
 
- 
isCommentForMultiblockWhether the comment might have been used for the next block in a multi-block structure.- Parameters:
- endBlockStmt- the end of the current block.
- Returns:
- true, if the comment might have been used for the next block in a multi-block structure.
 
- 
handleCommentInEmptyCodeBlockHandles a comment which is placed within the empty code block. Note, if comment is placed at the end of the empty code block, we have Checkstyle's limitations to clearly detect user intention of explanation target - above or below. The only case we can assume as a violation is when a single-line comment within the empty code block has indentation level that is lower than the indentation level of the closing right curly brace. For example:if (a == true) { // violation }- Parameters:
- comment- comment to check.
- nextStmt- next statement.
 
- 
getOneLinePreviousStatementDoes pre-order traverse of abstract syntax tree to find the previous statement of the comment. If previous statement of the comment is found, then the traverse will be finished.- Parameters:
- comment- current statement.
- Returns:
- previous statement of the comment or null if the comment does not have previous statement.
 
- 
isCommentWhether the ast is a comment.- Parameters:
- ast- the ast to check.
- Returns:
- true if the ast is a comment.
 
- 
isBlockStartWhether the AST node starts a block.- Parameters:
- root- the AST node to check.
- Returns:
- true if the AST node starts a block.
 
- 
findPreviousStatementFinds a previous statement of the comment. Uses root token of the line while searching.- Parameters:
- comment- comment.
- root- root token of the line.
- Returns:
- previous statement of the comment or null if previous statement was not found.
 
- 
findTokenWhichBeginsTheLineFinds a token which begins the line.- Parameters:
- root- root token of the line.
- Returns:
- token which begins the line.
 
- 
isUsingOfObjectReferenceToInvokeMethodChecks whether there is a use of an object reference to invoke an object's method on line.- Parameters:
- root- root token of the line.
- Returns:
- true if there is a use of an object reference to invoke an object's method on line.
 
- 
findStartTokenOfMethodCallChainFinds the start token of method call chain.- Parameters:
- root- root token of the line.
- Returns:
- the start token of method call chain.
 
- 
isOnPreviousLineIgnoringCommentsprivate boolean isOnPreviousLineIgnoringComments(DetailAST currentStatement, DetailAST checkedStatement) Checks whether the checked statement is on the previous line ignoring empty lines and lines which contain only comments.- Parameters:
- currentStatement- current statement.
- checkedStatement- checked statement.
- Returns:
- true if checked statement is on the line which is previous to current statement ignoring empty lines and lines which contain only comments.
 
- 
getNextTokenGet the token to start counting the number of lines to add to the distance aim from.- Parameters:
- checkedStatement- the checked statement.
- Returns:
- the token to start counting the number of lines to add to the distance aim from.
 
- 
countEmptyLinesCount the number of empty lines between statements.- Parameters:
- startStatement- start statement.
- endStatement- end statement.
- Returns:
- the number of empty lines between statements.
 
- 
logMultilineIndentationLogs comment which can have the same indentation level as next or previous statement.- Parameters:
- prevStmt- previous statement.
- comment- comment.
- nextStmt- next statement.
 
- 
getMessageKeyGet a message key depending on a comment type.- Parameters:
- comment- the comment to process.
- Returns:
- a message key.
 
- 
getPrevStatementFromSwitchBlockGets comment's previous statement from switch block.- Parameters:
- comment-- single-line comment.
- Returns:
- comment's previous statement or null if previous statement is absent.
 
- 
getPrevStatementWhenCommentIsUnderCaseGets previous statement for comment which is placed immediately under case.- Parameters:
- parentStatement- comment's parent statement.
- Returns:
- comment's previous statement or null if previous statement is absent.
 
- 
getPrevCaseTokenGets previous case-token for comment.- Parameters:
- parentStatement- comment's parent statement.
- Returns:
- previous case-token or null if previous case-token is absent.
 
- 
areSameLevelIndentedChecks if comment and next code statement (or previous code stmt like case in switch block) are indented at the same level, e.g.:// some comment - same indentation level int x = 10; // some comment - different indentation level int x1 = 5; /* * */ boolean bool = true; - same indentation level- Parameters:
- comment-- single-line comment.
- prevStmt- previous code statement.
- nextStmt- next code statement.
- Returns:
- true if comment and next code statement are indented at the same level.
 
- 
getLineStartGet a column number where a code starts.- Parameters:
- lineNo- the line number to get column number in.
- Returns:
- the column number where a code starts.
 
- 
isTrailingCommentChecks if current comment is a trailing comment.- Parameters:
- comment- comment to check.
- Returns:
- true if current comment is a trailing comment.
 
- 
isTrailingSingleLineCommentChecks if current single-line comment is trailing comment, e.g.:double d = 3.14; // some comment- Parameters:
- singleLineComment-- single-line comment.
- Returns:
- true if current single-line comment is trailing comment.
 
- 
isTrailingBlockCommentChecks if current comment block is trailing comment, e.g.:double d = 3.14; /* some comment */ /* some comment */ double d = 18.5;- Parameters:
- blockComment-- block comment begin.
- Returns:
- true if current comment block is trailing comment.
 
- 
areInSameMethodCallWithSameIndentChecks if the comment is inside a method call with same indentation of first expression. e.g:private final boolean myList = someMethod( // Some comment here s1, s2, s3 // ok );- Parameters:
- comment- comment to check.
- Returns:
- true, if comment is inside a method call with same indentation.
 
- 
getFirstExpressionNodeFromMethodCallReturns the first EXPR DetailAST child from parent of comment.- Parameters:
- methodCall- methodCall DetailAst from which node to be extracted.
- Returns:
- first EXPR DetailAST child from parent of comment.
 
 
-