From 3d4290baf0a9dabd9f5e7f5db304e7ce7b491dcb Mon Sep 17 00:00:00 2001 From: Michal Kordas Date: Fri, 17 Jul 2015 23:22:30 +0200 Subject: [PATCH] Add test coverage to MethodCallHandler in Indentation check. #1270 --- pom.xml | 1 - .../checks/indentation/MethodCallHandler.java | 69 +++++-------------- .../indentation/IndentationCheckTest.java | 21 ++++++ .../InputAnonymousClassInMethod.java | 27 ++++++++ .../indentation/InputMethodCallLineWrap.java | 10 +++ 5 files changed, 76 insertions(+), 52 deletions(-) create mode 100644 src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputAnonymousClassInMethod.java diff --git a/pom.xml b/pom.xml index ad0fa0a57..3bbdba7cc 100644 --- a/pom.xml +++ b/pom.xml @@ -1162,7 +1162,6 @@ .*.checks.indentation.ImportHandler5087 .*.checks.indentation.IndentationCheck10093 .*.checks.indentation.LineWrappingHandler8795 - .*.checks.indentation.MethodCallHandler6387 .*.checks.indentation.MethodCallLineWrapHandler00 .*.checks.indentation.MethodDefHandler87100 .*.checks.indentation.NewHandler8377 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 a368e2a2d..5dfce2d34 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 @@ -38,11 +38,7 @@ public class MethodCallHandler extends AbstractExpressionHandler { */ public MethodCallHandler(IndentationCheck indentCheck, DetailAST ast, AbstractExpressionHandler parent) { - super(indentCheck, - ast.getType() == TokenTypes.METHOD_CALL - ? "method call" : "ctor call", - ast, - parent); + super(indentCheck, "method call", ast, parent); } @Override @@ -52,30 +48,17 @@ public class MethodCallHandler extends AbstractExpressionHandler { if (getParent() instanceof MethodCallHandler) { final MethodCallHandler container = (MethodCallHandler) getParent(); - if (container != null) { - if (areOnSameLine(container.getMainAst(), getMainAst())) { - return container.getLevel(); - } - - // we should increase indentation only if this is the first - // chained method call which was moved to the next line - if (isChainedMethodCallWrapped()) { - return container.getLevel(); - } - else { - return new IndentLevel(container.getLevel(), getBasicOffset()); - } + if (areOnSameLine(container.getMainAst(), getMainAst())) { + return container.getLevel(); } - - // if we get here, we are the child of the left hand side (name - // side) of a method call with no "containing" call, use - // the first non-method call parent - - AbstractExpressionHandler p = getParent(); - while (p instanceof MethodCallHandler) { - p = p.getParent(); + // we should increase indentation only if this is the first + // chained method call which was moved to the next line + if (isChainedMethodCallWrapped()) { + return container.getLevel(); + } + else { + return new IndentLevel(container.getLevel(), getBasicOffset()); } - return p.suggestedChildLevel(this); } // if our expression isn't first on the line, just use the start @@ -100,15 +83,12 @@ public class MethodCallHandler extends AbstractExpressionHandler { final DetailAST dot = main.getFirstChild(); final DetailAST target = dot.getFirstChild(); - if (dot.getType() == TokenTypes.DOT - && target.getType() == TokenTypes.METHOD_CALL) { - final DetailAST dot1 = target.getFirstChild(); - final DetailAST target1 = dot1.getFirstChild(); + final DetailAST dot1 = target.getFirstChild(); + final DetailAST target1 = dot1.getFirstChild(); - if (dot1.getType() == TokenTypes.DOT - && target1.getType() == TokenTypes.METHOD_CALL) { - result = true; - } + if (dot1.getType() == TokenTypes.DOT + && target1.getType() == TokenTypes.METHOD_CALL) { + result = true; } return result; } @@ -126,14 +106,9 @@ public class MethodCallHandler extends AbstractExpressionHandler { // call name DetailAST astNode = ast.getFirstChild(); - while (astNode != null && astNode.getType() == TokenTypes.DOT) { + while (astNode.getType() == TokenTypes.DOT) { astNode = astNode.getFirstChild(); } - - if (astNode == null) { - astNode = ast; - } - return astNode; } @@ -157,8 +132,7 @@ public class MethodCallHandler extends AbstractExpressionHandler { @Override public void checkIndentation() { final DetailAST exprNode = getMainAst().getParent(); - if (exprNode.getParent().getType() != TokenTypes.LCURLY - && exprNode.getParent().getType() != TokenTypes.SLIST) { + if (exprNode.getParent().getType() != TokenTypes.SLIST) { return; } final DetailAST methodName = getMainAst().getFirstChild(); @@ -197,13 +171,6 @@ public class MethodCallHandler extends AbstractExpressionHandler { * method calls are chained returns right paren for last call. */ private static DetailAST getMethodCallLastNode(DetailAST firstNode) { - DetailAST lastNode; - if (firstNode.getNextSibling() == null) { - lastNode = firstNode.getLastChild(); - } - else { - lastNode = firstNode.getNextSibling(); - } - return lastNode; + return firstNode.getLastChild(); } } diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java index 6e81f0c90..0cb32856d 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/checks/indentation/IndentationCheckTest.java @@ -1470,6 +1470,27 @@ public class IndentationCheckTest extends BaseCheckTestSupport { verifyWarns(checkConfig, getPath("indentation/InputSynchronizedMethod.java"), expected); } + @Test + public void testAnonymousClassInMethod() throws Exception { + final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); + checkConfig.addAttribute("tabWidth", "8"); + checkConfig.addAttribute("basicOffset", "2"); + checkConfig.addAttribute("braceAdjustment", "0"); + checkConfig.addAttribute("caseIndent", "2"); + checkConfig.addAttribute("lineWrappingIndentation", "4"); + checkConfig.addAttribute("throwsIndent", "4"); + checkConfig.addAttribute("arrayInitIndent", "2"); + final String[] expected = { + "19: " + getCheckMessage(MSG_ERROR, "method def modifier", 8, 2), + "20: " + getCheckMessage(MSG_CHILD_ERROR, "method def", 16, 4), + "21: " + getCheckMessage(MSG_ERROR_MULTI, "method def modifier", 24, "18, 20, 22"), + "23: " + getCheckMessage(MSG_CHILD_ERROR_MULTI, "method def", 32, "20, 22, 24"), + "24: " + getCheckMessage(MSG_ERROR_MULTI, "method def rcurly", 24, "18, 20, 22"), + "26: " + getCheckMessage(MSG_ERROR, "method def rcurly", 8, 2), + }; + verifyWarns(checkConfig, getPath("indentation/InputAnonymousClassInMethod.java"), expected); + } + @Test public void testAnnotationDefinition() throws Exception { final DefaultConfiguration checkConfig = createCheckConfig(IndentationCheck.class); diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputAnonymousClassInMethod.java b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputAnonymousClassInMethod.java new file mode 100644 index 000000000..44514f1ab --- /dev/null +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputAnonymousClassInMethod.java @@ -0,0 +1,27 @@ +package com.puppycrawl.tools.checkstyle.indentation; //indent:0 exp:0 + +import java.io.File; //indent:0 exp:0 +import java.io.FileFilter; //indent:0 exp:0 + +/** //indent:0 exp:0 + * This test-input is intended to be checked using following configuration: //indent:1 exp:1 + * //indent:1 exp:1 + * arrayInitIndent = 2 //indent:1 exp:1 + * basicOffset = 2 //indent:1 exp:1 + * braceAdjustment = 0 //indent:1 exp:1 + * caseIndent = 2 //indent:1 exp:1 + * forceStrictCondition = false //indent:1 exp:1 + * lineWrappingIndentation = 4 //indent:1 exp:1 + * tabWidth = 8 //indent:1 exp:1 + * throwsIndent = 4 //indent:1 exp:1 + */ //indent:1 exp:1 +public class InputAnonymousClassInMethod { //indent:0 exp:0 + private void walkDir(File dir, FileFilter fileFilter) { //indent:8 exp:2 warn + walkDir( dir, new FileFilter() { //indent:16 exp:4 warn + @Override //indent:24 exp:8 warn + public boolean accept(File path) { //indent:24 exp:24 + return ( path.isDirectory() ); //indent:32 exp:12 warn + } //indent:24 exp:8 warn + } ); //indent:16 exp:16 + } //indent:8 exp:2 warn +} //indent:0 exp:0 diff --git a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputMethodCallLineWrap.java b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputMethodCallLineWrap.java index d9f64e725..12ba8f869 100644 --- a/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputMethodCallLineWrap.java +++ b/src/test/resources/com/puppycrawl/tools/checkstyle/indentation/InputMethodCallLineWrap.java @@ -52,4 +52,14 @@ public class InputMethodCallLineWrap { //indent:0 exp:0 ); //indent:14 exp:16 warn } //indent:8 exp:8 }; //indent:4 exp:4 + + void chaining() { //indent:4 exp:4 + toString() //indent:8 exp:8 + .getClass(); //indent:16 exp:16 + toString().contains(//indent:8 exp:8 + new String(//indent:12 exp:12 + "a" //indent:20 exp:20 + )//indent:12 exp:12 + ); //indent:8 exp:8 + } //indent:4 exp:4 } //indent:0 exp:0