From a0fed4c0e90c488101c4ecb27f3f6a412c8f9a4e Mon Sep 17 00:00:00 2001 From: rnveach Date: Sat, 20 Feb 2016 15:23:26 -0500 Subject: [PATCH] minor: refactored LineWrappingHandler --- .../AbstractExpressionHandler.java | 12 +++- .../checks/indentation/ClassDefHandler.java | 4 +- .../checks/indentation/ForHandler.java | 5 +- .../checks/indentation/IfHandler.java | 5 +- .../checks/indentation/IndentationCheck.java | 12 ++++ .../indentation/LineWrappingHandler.java | 62 +++++++------------ .../checks/indentation/MemberDefHandler.java | 10 +-- .../checks/indentation/MethodCallHandler.java | 5 +- .../checks/indentation/MethodDefHandler.java | 5 +- .../indentation/SynchronizedHandler.java | 6 +- 10 files changed, 58 insertions(+), 68 deletions(-) diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java index 343698188..fa9628a2f 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/AbstractExpressionHandler.java @@ -344,7 +344,6 @@ public abstract class AbstractExpressionHandler { * @param indentLevel the indentation level * @param mustMatch whether or not the indentation level must match */ - private void checkLineIndent(int lineNum, int colNum, IndentLevel indentLevel, boolean mustMatch) { final String line = indentCheck.getLine(lineNum - 1); @@ -359,6 +358,17 @@ public abstract class AbstractExpressionHandler { } } + /** + * Checks indentation on wrapped lines between and including + * {@code firstNode} and {@code lastNode}. + * + * @param firstNode First node to start examining. + * @param lastNode Last node to examine inclusively. + */ + protected void checkWrappingIndentation(DetailAST firstNode, DetailAST lastNode) { + indentCheck.getLineWrappingHandler().checkIndentation(firstNode, lastNode); + } + /** * Check the indent level of the children of the specified parent * expression. diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java index fdcc33ba5..5c8db6223 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ClassDefHandler.java @@ -80,9 +80,7 @@ public class ClassDefHandler extends BlockParentHandler { checkModifiers(); } - final LineWrappingHandler lineWrap = - new LineWrappingHandler(getIndentCheck(), getMainAst(), getMainAst().getLastChild()); - lineWrap.checkIndentation(); + checkWrappingIndentation(getMainAst(), getMainAst().getLastChild()); super.checkIndentation(); } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ForHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ForHandler.java index 4fa880883..25f5e2817 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ForHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/ForHandler.java @@ -72,10 +72,7 @@ public class ForHandler extends BlockParentHandler { public void checkIndentation() { checkForParams(); super.checkIndentation(); - final LineWrappingHandler lineWrap = - new LineWrappingHandler(getIndentCheck(), getMainAst(), - getForLoopRightParen(getMainAst())); - lineWrap.checkIndentation(); + checkWrappingIndentation(getMainAst(), getForLoopRightParen(getMainAst())); } /** diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IfHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IfHandler.java index 04f11aa87..97eea3969 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IfHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IfHandler.java @@ -94,10 +94,7 @@ public class IfHandler extends BlockParentHandler { public void checkIndentation() { super.checkIndentation(); checkCondExpr(); - final LineWrappingHandler lineWrap = - new LineWrappingHandler(getIndentCheck(), getMainAst(), - getIfStatementRightParen(getMainAst())); - lineWrap.checkIndentation(); + checkWrappingIndentation(getMainAst(), getIfStatementRightParen(getMainAst())); } /** diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java index ba601f92a..a2d8d0d0b 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheck.java @@ -112,6 +112,9 @@ public class IndentationCheck extends AbstractCheck { /** Handlers currently in use. */ private final Deque handlers = new ArrayDeque<>(); + /** Instance of line wrapping handler to use. */ + private final LineWrappingHandler lineWrappingHandler = new LineWrappingHandler(this); + /** Factory from which handlers are distributed. */ private final HandlerFactory handlerFactory = new HandlerFactory(); @@ -330,6 +333,15 @@ public class IndentationCheck extends AbstractCheck { handlers.pop(); } + /** + * Accessor for the line wrapping handler. + * + * @return the line wrapping handler + */ + public LineWrappingHandler getLineWrappingHandler() { + return lineWrappingHandler; + } + /** * Accessor for the handler factory. * diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java index 35bbd3ed6..c9c58c9f5 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/LineWrappingHandler.java @@ -45,61 +45,41 @@ public class LineWrappingHandler { */ private final IndentationCheck indentCheck; - /** - * Root node for current expression. - */ - private final DetailAST firstNode; - - /** - * Last node for current expression. - */ - private final DetailAST lastNode; - - /** - * User's value of line wrapping indentation. - */ - private final int indentLevel; - - /** - * Force strict condition in line wrapping case. - */ - private final boolean forceStrictCondition; - /** * Sets values of class field, finds last node and calculates indentation level. * * @param instance * instance of IndentationCheck. - * @param firstNode - * root node for current expression. - * @param lastNode - * last node for current expression. */ - public LineWrappingHandler(IndentationCheck instance, DetailAST firstNode, DetailAST lastNode) { + public LineWrappingHandler(IndentationCheck instance) { indentCheck = instance; - this.firstNode = firstNode; - this.lastNode = lastNode; - indentLevel = indentCheck.getLineWrappingIndentation(); - forceStrictCondition = indentCheck.isForceStrictCondition(); } /** - * Getter for lastNode field. - * @return lastNode field + * Checks line wrapping into expressions and definitions using property + * 'lineWrappingIndentation'. + * + * @param firstNode First node to start examining. + * @param lastNode Last node to examine inclusively. */ - protected final DetailAST getLastNode() { - return lastNode; + public void checkIndentation(DetailAST firstNode, DetailAST lastNode) { + checkIndentation(firstNode, lastNode, indentCheck.getLineWrappingIndentation()); } /** * Checks line wrapping into expressions and definitions. + * + * @param firstNode First node to start examining. + * @param lastNode Last node to examine inclusively. + * @param indentLevel Indentation all wrapped lines should use. */ - public void checkIndentation() { - final NavigableMap firstNodesOnLines = collectFirstNodes(); + public void checkIndentation(DetailAST firstNode, DetailAST lastNode, int indentLevel) { + final NavigableMap firstNodesOnLines = collectFirstNodes(firstNode, + lastNode); final DetailAST firstLineNode = firstNodesOnLines.get(firstNodesOnLines.firstKey()); if (firstLineNode.getType() == TokenTypes.AT) { - checkAnnotationIndentation(firstLineNode, firstNodesOnLines); + checkAnnotationIndentation(firstLineNode, firstNodesOnLines, indentLevel); } // First node should be removed because it was already checked before. @@ -153,10 +133,13 @@ public class LineWrappingHandler { /** * Finds first nodes on line and puts them into Map. * + * @param firstNode First node to start examining. + * @param lastNode Last node to examine inclusively. * @return NavigableMap which contains lines numbers as a key and first * nodes on lines as a values. */ - private NavigableMap collectFirstNodes() { + private NavigableMap collectFirstNodes(DetailAST firstNode, + DetailAST lastNode) { final NavigableMap result = new TreeMap<>(); result.put(firstNode.getLineNo(), firstNode); @@ -205,9 +188,10 @@ public class LineWrappingHandler { * @param atNode at-clause node. * @param firstNodesOnLines map which contains * first nodes as values and line numbers as keys. + * @param indentLevel line wrapping indentation. */ private void checkAnnotationIndentation(DetailAST atNode, - NavigableMap firstNodesOnLines) { + NavigableMap firstNodesOnLines, int indentLevel) { final int firstNodeIndent = expandedTabsColumnNo(atNode); final int currentIndent = firstNodeIndent + indentLevel; final Collection values = firstNodesOnLines.values(); @@ -278,7 +262,7 @@ public class LineWrappingHandler { * correct indentation. */ private void logWarningMessage(DetailAST currentNode, int currentIndent) { - if (forceStrictCondition) { + if (indentCheck.isForceStrictCondition()) { if (expandedTabsColumnNo(currentNode) != currentIndent) { indentCheck.indentationLog(currentNode.getLineNo(), IndentationCheck.MSG_ERROR, currentNode.getText(), diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java index c4b4395ca..5d9373b66 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MemberDefHandler.java @@ -50,11 +50,11 @@ public class MemberDefHandler extends AbstractExpressionHandler { else { checkModifiers(); } - final LineWrappingHandler lineWrap = - new LineWrappingHandler(getIndentCheck(), getMainAst(), - getVarDefStatementSemicolon(getMainAst())); - if (lineWrap.getLastNode() != null && !isArrayDeclaration(getMainAst())) { - lineWrap.checkIndentation(); + final DetailAST firstNode = getMainAst(); + final DetailAST lastNode = getVarDefStatementSemicolon(firstNode); + + if (lastNode != null && !isArrayDeclaration(firstNode)) { + checkWrappingIndentation(firstNode, lastNode); } } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java index 3d1267e51..c626fdaad 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodCallHandler.java @@ -168,10 +168,7 @@ public class MethodCallHandler extends AbstractExpressionHandler { false, true); checkRParen(lparen, rparen); - final LineWrappingHandler lineWrap = - new LineWrappingHandler(getIndentCheck(), getMainAst(), - getMethodCallLastNode(getMainAst())); - lineWrap.checkIndentation(); + checkWrappingIndentation(getMainAst(), getMethodCallLastNode(getMainAst())); } @Override diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodDefHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodDefHandler.java index 773af50c5..185f2fb13 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodDefHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/MethodDefHandler.java @@ -61,10 +61,7 @@ public class MethodDefHandler extends BlockParentHandler { public void checkIndentation() { checkModifiers(); - final LineWrappingHandler lineWrap = - new LineWrappingHandler(getIndentCheck(), getMainAst(), - getMethodDefParamRightParen(getMainAst())); - lineWrap.checkIndentation(); + checkWrappingIndentation(getMainAst(), getMethodDefParamRightParen(getMainAst())); if (getLCurly() == null) { // abstract method def -- no body return; diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/SynchronizedHandler.java b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/SynchronizedHandler.java index 3f6aeef74..87ad53906 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/SynchronizedHandler.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/checks/indentation/SynchronizedHandler.java @@ -53,10 +53,8 @@ public class SynchronizedHandler extends BlockParentHandler { if (!methodModifier) { super.checkIndentation(); checkSynchronizedExpr(); - final LineWrappingHandler lineWrap = - new LineWrappingHandler(getIndentCheck(), getMainAst(), - getSynchronizedStatementRightParen(getMainAst())); - lineWrap.checkIndentation(); + checkWrappingIndentation(getMainAst(), + getSynchronizedStatementRightParen(getMainAst())); } }