diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/ExecutableStatementCountCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/ExecutableStatementCountCheck.java index 26a9102fb..40831fdd6 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/ExecutableStatementCountCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/ExecutableStatementCountCheck.java @@ -101,7 +101,7 @@ public final class ExecutableStatementCountCheck case TokenTypes.METHOD_DEF: case TokenTypes.INSTANCE_INIT: case TokenTypes.STATIC_INIT: - visitMethodDef(); + visitMemberDef(aAST); break; case TokenTypes.SLIST: visitSlist(aAST); @@ -119,7 +119,7 @@ public final class ExecutableStatementCountCheck case TokenTypes.METHOD_DEF: case TokenTypes.INSTANCE_INIT: case TokenTypes.STATIC_INIT: - leaveMethodDef(aAST); + leaveMemberDef(aAST); break; case TokenTypes.SLIST: // Do nothing @@ -129,19 +129,22 @@ public final class ExecutableStatementCountCheck } } - /** Process the start of the method definition. */ - private void visitMethodDef() + /** + * Process the start of the member definition. + * @param aAST the token representing the member definition. + */ + private void visitMemberDef(DetailAST aAST) { mContextStack.push(mContext); - mContext = new Context(); + mContext = new Context(aAST); } /** - * Process the end of a method definition. + * Process the end of a member definition. * - * @param aAST the token representing the method definition. + * @param aAST the token representing the member definition. */ - private void leaveMethodDef(DetailAST aAST) + private void leaveMemberDef(DetailAST aAST) { final int count = mContext.getCount(); if (count > getMax()) { @@ -163,7 +166,23 @@ public final class ExecutableStatementCountCheck private void visitSlist(DetailAST aAST) { if (mContext != null) { - mContext.addCount(aAST.getChildCount() / 2); + // find member AST for the statement list + final DetailAST contextAST = mContext.getAST(); + DetailAST parent = aAST.getParent(); + while (parent != null) { + final int type = parent.getType(); + if ((type == TokenTypes.CTOR_DEF) + || (type == TokenTypes.METHOD_DEF) + || (type == TokenTypes.INSTANCE_INIT) + || (type == TokenTypes.STATIC_INIT)) + { + if (parent == contextAST) { + mContext.addCount(aAST.getChildCount() / 2); + } + break; + } + parent = parent.getParent(); + } } } @@ -173,14 +192,19 @@ public final class ExecutableStatementCountCheck */ private class Context { + /** Member AST node. */ + private DetailAST mAST; + /** Counter for context elements. */ private int mCount; /** - * Creates new method context. + * Creates new member context. + * @param aAST member AST node. */ - public Context() + public Context(DetailAST aAST) { + mAST = aAST; mCount = 0; } @@ -193,6 +217,15 @@ public final class ExecutableStatementCountCheck mCount += aCount; } + /** + * Gets the member AST node. + * @return the member AST node. + */ + public DetailAST getAST() + { + return mAST; + } + /** * Gets the count. * @return the count. diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/ComplexityCheckTestInput.java b/src/testinputs/com/puppycrawl/tools/checkstyle/ComplexityCheckTestInput.java index 70f8fe97f..da054e67f 100644 --- a/src/testinputs/com/puppycrawl/tools/checkstyle/ComplexityCheckTestInput.java +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/ComplexityCheckTestInput.java @@ -71,4 +71,16 @@ public class ComplexityCheckTestInput { } else { } } + + /** Inner */ + public ComplexityCheckTestInput(int aParam) + { + Runnable runnable = new Runnable() { + public void run() { + while (true) { + } + } + }; + new Thread(runnable).start(); + } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/CyclomaticComplexityCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/CyclomaticComplexityCheckTest.java index 6ab6d1565..4fdcbecad 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/CyclomaticComplexityCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/CyclomaticComplexityCheckTest.java @@ -22,6 +22,8 @@ public class CyclomaticComplexityCheckTest "48:5: Cyclomatic Complexity is 4 (max allowed is 0).", "58:5: Cyclomatic Complexity is 4 (max allowed is 0).", "67:5: Cyclomatic Complexity is 4 (max allowed is 0).", + "76:5: Cyclomatic Complexity is 1 (max allowed is 0).", + "79:13: Cyclomatic Complexity is 2 (max allowed is 0).", }; verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/ExecutableStatementCountCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/ExecutableStatementCountCheckTest.java index 92eabc2d5..edca8d03b 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/ExecutableStatementCountCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/ExecutableStatementCountCheckTest.java @@ -22,6 +22,8 @@ public class ExecutableStatementCountCheckTest "48:5: Executable statement count is 2 (max allowed is 0).", "58:5: Executable statement count is 2 (max allowed is 0).", "67:5: Executable statement count is 2 (max allowed is 0).", + "76:5: Executable statement count is 2 (max allowed is 0).", + "79:13: Executable statement count is 1 (max allowed is 0).", }; verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected); @@ -41,6 +43,7 @@ public class ExecutableStatementCountCheckTest "17:5: Executable statement count is 2 (max allowed is 0).", "27:5: Executable statement count is 1 (max allowed is 0).", "34:5: Executable statement count is 3 (max allowed is 0).", + "79:13: Executable statement count is 1 (max allowed is 0).", }; verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected); @@ -56,6 +59,7 @@ public class ExecutableStatementCountCheckTest final String[] expected = { "48:5: Executable statement count is 2 (max allowed is 0).", + "76:5: Executable statement count is 2 (max allowed is 0).", }; verify(checkConfig, getPath("ComplexityCheckTestInput.java"), expected);