From 2959d83ed2a3b8217d9d3c8aefa748cdacdcd393 Mon Sep 17 00:00:00 2001 From: Andrei Selkin Date: Wed, 18 Nov 2015 21:07:38 +0300 Subject: [PATCH] Issue #2530: Refactoring of CommentsIndentationCheck --- .../indentation/CommentsIndentationCheck.java | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/CommentsIndentationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/CommentsIndentationCheck.java index 9d6cb6ef0..87bb0e3c8 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/CommentsIndentationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/CommentsIndentationCheck.java @@ -262,16 +262,19 @@ public class CommentsIndentationCheck extends Check { * @param comment single line comment. * @return the first token of the destributed previous statement of single line comment. */ - private static DetailAST getDistributedPreviousStatementOfSingleLineComment( - DetailAST comment) { - DetailAST previousStatement = comment.getPreviousSibling(); - if (previousStatement.getType() == TokenTypes.LITERAL_RETURN - || previousStatement.getType() == TokenTypes.LITERAL_THROW) { - return previousStatement; + private static DetailAST getDistributedPreviousStatementOfSingleLineComment(DetailAST comment) { + final DetailAST previousStatement; + DetailAST currentToken = comment.getPreviousSibling(); + if (currentToken.getType() == TokenTypes.LITERAL_RETURN + || currentToken.getType() == TokenTypes.LITERAL_THROW) { + previousStatement = currentToken; } - previousStatement = previousStatement.getPreviousSibling(); - while (previousStatement.getFirstChild() != null) { - previousStatement = previousStatement.getFirstChild(); + else { + currentToken = currentToken.getPreviousSibling(); + while (currentToken.getFirstChild() != null) { + currentToken = currentToken.getFirstChild(); + } + previousStatement = currentToken; } return previousStatement; } @@ -493,6 +496,7 @@ public class CommentsIndentationCheck extends Check { * statement. */ private static DetailAST getOneLinePreviousStatementOfSingleLineComment(DetailAST comment) { + DetailAST previousStatement = null; final Stack stack = new Stack<>(); DetailAST root = comment.getParent(); @@ -501,10 +505,11 @@ public class CommentsIndentationCheck extends Check { root = stack.pop(); } while (root != null) { - final DetailAST previousStatement = - findPreviousStatementOfSingleLineComment(comment, root); + previousStatement = findPreviousStatementOfSingleLineComment(comment, root); if (previousStatement != null) { - return previousStatement; + root = null; + stack.clear(); + break; } if (root.getNextSibling() != null) { stack.push(root.getNextSibling()); @@ -512,7 +517,7 @@ public class CommentsIndentationCheck extends Check { root = root.getFirstChild(); } } - return null; + return previousStatement; } /**