public class CommentsIndentationCheck extends AbstractCheck
Examples:
To configure the Check:
<module name="CommentsIndentation"/>/* * comment * some comment */ boolean bool = true; - such comment indentation is ok /* * comment * some comment */ double d = 3.14; - Block Comment has incorrect indentation level 7, expected 4. // some comment - comment is ok String str = ""; // some comment Comment has incorrect indentation level 8, expected 4. String str1 = "";
| Modifier and Type | Field and Description |
|---|---|
static java.lang.String |
MSG_KEY_BLOCK
A key is pointing to the warning message text in "messages.properties" file.
|
static java.lang.String |
MSG_KEY_SINGLE
A key is pointing to the warning message text in "messages.properties" file.
|
| Constructor and Description |
|---|
CommentsIndentationCheck() |
| Modifier and Type | Method and Description |
|---|---|
private boolean |
areSameLevelIndented(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 int |
countEmptyLines(DetailAST startStatement,
DetailAST endStatement)
Count the number of empty lines between statements.
|
private DetailAST |
findPreviousStatement(DetailAST comment,
DetailAST root)
Finds a previous statement of the comment.
|
private static DetailAST |
findStartTokenOfMethodCallChain(DetailAST root)
Finds the start token of method call chain.
|
private static DetailAST |
findTokenWhichBeginsTheLine(DetailAST root)
Finds a token which begins the line.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
private static DetailAST |
getDistributedPreviousStatement(DetailAST comment)
Returns the first token of the distributed previous statement of comment.
|
private int |
getLineStart(int lineNo)
Get a column number where a code starts.
|
private static java.lang.String |
getMessageKey(DetailAST comment)
Get a message key depending on a comment type.
|
private static DetailAST |
getNextStmt(DetailAST comment)
Returns the next statement of a comment.
|
private DetailAST |
getNextToken(DetailAST checkedStatement)
Get the token to start counting the number of lines to add to the distance aim from.
|
private DetailAST |
getOneLinePreviousStatement(DetailAST comment)
Does pre-order traverse of abstract syntax tree to find the previous statement of the
comment.
|
private static DetailAST |
getPrevCaseToken(DetailAST parentStatement)
Gets previous case-token for comment.
|
private DetailAST |
getPreviousStatement(DetailAST comment)
Returns the previous statement of a comment.
|
private static DetailAST |
getPrevStatementFromSwitchBlock(DetailAST comment)
Gets comment's previous statement from switch block.
|
private static DetailAST |
getPrevStatementWhenCommentIsUnderCase(DetailAST parentStatement)
Gets previous statement for comment which is placed immediately under case.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private void |
handleCommentAtTheEndOfTheCodeBlock(DetailAST prevStmt,
DetailAST comment,
DetailAST nextStmt)
Handles a comment which is placed at the end of non empty code block.
|
private void |
handleCommentInEmptyCaseBlock(DetailAST prevStmt,
DetailAST comment,
DetailAST nextStmt)
Handles a comment which is placed within empty case block.
|
private void |
handleCommentInEmptyCodeBlock(DetailAST comment,
DetailAST nextStmt)
Handles a comment which is placed within the empty code block.
|
private void |
handleFallThroughComment(DetailAST prevStmt,
DetailAST comment,
DetailAST nextStmt)
Handles 'fall through' single line comment.
|
private static boolean |
isBlockStart(DetailAST root)
Whether the AST node starts a block.
|
private static boolean |
isComment(DetailAST ast)
Whether the ast is a comment.
|
private static boolean |
isCommentAtTheEndOfTheCodeBlock(DetailAST nextStmt)
Checks whether a comment is placed at the end of the code block.
|
private static boolean |
isCommentForMultiblock(DetailAST endBlockStmt)
Whether the comment might have been used for the next block in a multi-block structure.
|
boolean |
isCommentNodesRequired()
Whether comment nodes are required or not.
|
private static boolean |
isDefinition(DetailAST previousSibling)
Whether the statement is a kind of definition (method, class etc.).
|
private boolean |
isDistributedExpression(DetailAST comment)
Checks whether the previous statement of a comment is a method call chain or
string concatenation statement distributed over two ore more lines.
|
private boolean |
isDistributedPreviousStatement(DetailAST comment)
Checks whether the previous statement of a comment is distributed over two or more lines.
|
private static boolean |
isDistributedReturnStatement(DetailAST commentPreviousSibling)
Checks whether the previous statement of a comment is a distributed return statement.
|
private static boolean |
isDistributedThrowStatement(DetailAST commentPreviousSibling)
Checks whether the previous statement of a comment is a distributed throw statement.
|
private static boolean |
isFallThroughComment(DetailAST prevStmt,
DetailAST nextStmt)
Checks whether comment is a 'fall through' comment.
|
private static boolean |
isInEmptyCaseBlock(DetailAST prevStmt,
DetailAST nextStmt)
Checks whether case block is empty.
|
private static boolean |
isInEmptyCodeBlock(DetailAST prevStmt,
DetailAST nextStmt)
Checks whether comment is placed in the empty code block.
|
private 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.
|
private static boolean |
isStatementWithPossibleCurlies(DetailAST previousSibling)
Whether the statement can have or always have curly brackets.
|
private boolean |
isTrailingBlockComment(DetailAST blockComment)
Checks if current comment block is trailing comment, e.g.:
|
private boolean |
isTrailingComment(DetailAST comment)
Checks if current comment is a trailing comment.
|
private boolean |
isTrailingSingleLineComment(DetailAST singleLineComment)
Checks if current single line comment is trailing comment, e.g.:
|
private static boolean |
isUsingOfObjectReferenceToInvokeMethod(DetailAST root)
Checks whether there is a use of an object reference to invoke an object's method on line.
|
private void |
logMultilineIndentation(DetailAST prevStmt,
DetailAST comment,
DetailAST nextStmt)
Logs comment which can have the same indentation level as next or previous statement.
|
private void |
visitComment(DetailAST comment)
Checks comment indentations over surrounding code, e.g.:
|
void |
visitToken(DetailAST commentAst)
Called to process a token.
|
beginTree, destroy, finishTree, getClassLoader, getFileContents, getLine, getLines, getTabWidth, getTokenNames, init, leaveToken, log, log, setClassLoader, setFileContents, setMessages, setTabWidth, setTokensgetCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, log, setId, setSeverityconfigure, contextualize, finishLocalSetup, getConfiguration, setupChildpublic static final java.lang.String MSG_KEY_SINGLE
public static final java.lang.String MSG_KEY_BLOCK
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic boolean isCommentNodesRequired()
AbstractCheckisCommentNodesRequired in class AbstractCheckpublic void visitToken(DetailAST commentAst)
AbstractCheckvisitToken in class AbstractCheckcommentAst - the token to processprivate void visitComment(DetailAST comment)
// some comment - this is ok
double d = 3.14;
// some comment - this is <b>not</b> ok.
double d1 = 5.0;
comment - comment to check.private static DetailAST getNextStmt(DetailAST comment)
comment - comment.private DetailAST getPreviousStatement(DetailAST comment)
comment - comment.private boolean isDistributedPreviousStatement(DetailAST comment)
comment - comment to check.private boolean isDistributedExpression(DetailAST comment)
comment - comment to check.private static boolean isStatementWithPossibleCurlies(DetailAST previousSibling)
previousSibling - the statement to check.private static boolean isDefinition(DetailAST previousSibling)
previousSibling - the statement to check.private static boolean isDistributedReturnStatement(DetailAST commentPreviousSibling)
commentPreviousSibling - previous sibling of the comment.private static boolean isDistributedThrowStatement(DetailAST commentPreviousSibling)
commentPreviousSibling - previous sibling of the comment.private static DetailAST getDistributedPreviousStatement(DetailAST comment)
comment - comment to check.private static boolean isInEmptyCaseBlock(DetailAST prevStmt, DetailAST nextStmt)
nextStmt - previous statement.prevStmt - next statement.private static boolean isFallThroughComment(DetailAST prevStmt, DetailAST nextStmt)
...
case OPTION_ONE:
int someVariable = 1;
// fall through
case OPTION_TWO:
int a = 5;
break;
...
prevStmt - previous statement.nextStmt - next statement.private static boolean isCommentAtTheEndOfTheCodeBlock(DetailAST nextStmt)
nextStmt - next statement.private static boolean isInEmptyCodeBlock(DetailAST prevStmt, DetailAST nextStmt)
...
// empty code block
...
prevStmt - previous statement.nextStmt - next statement.private void handleCommentInEmptyCaseBlock(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt)
...
case OPTION_ONE:
// violation
case OPTION_TWO:
...
prevStmt - previous statement.comment - single line comment.nextStmt - next statement.private void handleFallThroughComment(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt)
...
case OPTION_ONE:
int someVariable = 1;
// fall through - OK
case OPTION_TWO:
int a = 5;
break;
...
...
case OPTION_ONE:
int someVariable = 1;
// than init variable a - OK
case OPTION_TWO:
int a = 5;
break;
...
prevStmt - previous statement.comment - single line comment.nextStmt - next statement.private void handleCommentAtTheEndOfTheCodeBlock(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt)
if (a == true) {
int b = 1;
// comment
}
prevStmt - previous statement.comment - comment to check.nextStmt - next statement.private static boolean isCommentForMultiblock(DetailAST endBlockStmt)
endBlockStmt - the end of the current block.private void handleCommentInEmptyCodeBlock(DetailAST comment, DetailAST nextStmt)
if (a == true) {
// violation
}
comment - comment to check.nextStmt - next statement.private DetailAST getOneLinePreviousStatement(DetailAST comment)
comment - current statement.private static boolean isComment(DetailAST ast)
ast - the ast to check.private static boolean isBlockStart(DetailAST root)
root - the AST node to check.private DetailAST findPreviousStatement(DetailAST comment, DetailAST root)
comment - comment.root - root token of the line.private static DetailAST findTokenWhichBeginsTheLine(DetailAST root)
root - root token of the line.private static boolean isUsingOfObjectReferenceToInvokeMethod(DetailAST root)
root - root token of the line.private static DetailAST findStartTokenOfMethodCallChain(DetailAST root)
root - root token of the line.private boolean isOnPreviousLineIgnoringComments(DetailAST currentStatement, DetailAST checkedStatement)
currentStatement - current statement.checkedStatement - checked statement.private DetailAST getNextToken(DetailAST checkedStatement)
checkedStatement - the checked statement.private int countEmptyLines(DetailAST startStatement, DetailAST endStatement)
startStatement - start statement.endStatement - end statement.private void logMultilineIndentation(DetailAST prevStmt, DetailAST comment, DetailAST nextStmt)
comment - comment.nextStmt - next statement.prevStmt - previous statement.private static java.lang.String getMessageKey(DetailAST comment)
comment - the comment to process.private static DetailAST getPrevStatementFromSwitchBlock(DetailAST comment)
comment - single-line comment.private static DetailAST getPrevStatementWhenCommentIsUnderCase(DetailAST parentStatement)
parentStatement - comment's parent statement.private static DetailAST getPrevCaseToken(DetailAST parentStatement)
parentStatement - comment's parent statement.private boolean areSameLevelIndented(DetailAST comment, DetailAST prevStmt, DetailAST nextStmt)
// some comment - same indentation level
int x = 10;
// some comment - different indentation level
int x1 = 5;
/*
*
*/
boolean bool = true; - same indentation level
comment - single line comment.prevStmt - previous code statement.nextStmt - next code statement.private int getLineStart(int lineNo)
lineNo - the line number to get column number in.private boolean isTrailingComment(DetailAST comment)
comment - comment to check.private boolean isTrailingSingleLineComment(DetailAST singleLineComment)
double d = 3.14; // some comment
singleLineComment - single line comment.private boolean isTrailingBlockComment(DetailAST blockComment)
double d = 3.14; /* some comment */
/* some comment */ double d = 18.5;
blockComment - block comment begin.