From 9d41bddb46e5fbe291be4cdb347dee18ea4424fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20K=C3=BChne?= Date: Tue, 20 Mar 2007 20:29:35 +0000 Subject: [PATCH] Fixed bug #1579227: Fixed docs, added unit tests and changed the check implementation to allow turning off the | and & operators --- .../BooleanExpressionComplexityCheck.java | 8 +++- ...eanExpressionComplexityCheckTestInput.java | 7 +++- .../BooleanExpressionComplexityCheckTest.java | 15 ++++++- src/xdocs/config_metrics.xml | 41 ++++++++++++++++++- src/xdocs/releasenotes.xml | 9 ++++ 5 files changed, 75 insertions(+), 5 deletions(-) diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheck.java index ec57bb15d..96d589ad1 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheck.java @@ -27,7 +27,7 @@ import com.puppycrawl.tools.checkstyle.checks.CheckUtils; import java.util.Stack; /** - * Restricts nested boolean operators (&&, || and ^) to + * Restricts nested boolean operators (&&, ||, &, | and ^) to * a specified depth (default = 3). * * @author Simon Harris @@ -69,7 +69,11 @@ public final class BooleanExpressionComplexityCheck extends Check /** {@inheritDoc} */ public int[] getRequiredTokens() { - return getDefaultTokens(); + return new int[] { + TokenTypes.CTOR_DEF, + TokenTypes.METHOD_DEF, + TokenTypes.EXPR, + }; } /** diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/metrics/BooleanExpressionComplexityCheckTestInput.java b/src/testinputs/com/puppycrawl/tools/checkstyle/metrics/BooleanExpressionComplexityCheckTestInput.java index cc39f3cbc..18b17e1cd 100644 --- a/src/testinputs/com/puppycrawl/tools/checkstyle/metrics/BooleanExpressionComplexityCheckTestInput.java +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/metrics/BooleanExpressionComplexityCheckTestInput.java @@ -24,6 +24,11 @@ public class BooleanExpressionComplexityCheckTestInput { } public boolean equals(Object object) { - return (((_a && (_b & _c)) || (_c ^ _d))); + return (((_a && (_b & _c)) || (_c ^ _d) || (_a && _d))); + } + + public boolean bitwise() + { + return (((_a & (_b & _c)) | (_c ^ _d) | (_a & _d))); } } diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheckTest.java index 26170b6ed..5fd443fcb 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheckTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/metrics/BooleanExpressionComplexityCheckTest.java @@ -12,7 +12,20 @@ public class BooleanExpressionComplexityCheckTest extends BaseCheckTestCase createCheckConfig(BooleanExpressionComplexityCheck.class); String[] expected = { - "13:9: Boolean expression complexity is 4 (max allowed is 3).", + "13:9: Boolean expression complexity is 4 (max allowed is 3).", + "32:9: Boolean expression complexity is 6 (max allowed is 3).", + }; + + verify(checkConfig, getPath("metrics" + File.separator + "BooleanExpressionComplexityCheckTestInput.java"), expected); + } + + public void testNoBitwise() throws Exception { + DefaultConfiguration checkConfig = + createCheckConfig(BooleanExpressionComplexityCheck.class); + checkConfig.addAttribute("max", "5"); + checkConfig.addAttribute("tokens", "BXOR,LAND,LOR"); + + String[] expected = { }; verify(checkConfig, getPath("metrics" + File.separator + "BooleanExpressionComplexityCheckTestInput.java"), expected); diff --git a/src/xdocs/config_metrics.xml b/src/xdocs/config_metrics.xml index 795276146..587b39b76 100755 --- a/src/xdocs/config_metrics.xml +++ b/src/xdocs/config_metrics.xml @@ -13,7 +13,8 @@

Restrict the number of number of &&, || + class="code">&&, ||, + &, | and ^ in an expression.

@@ -21,6 +22,14 @@ Rationale: Too many conditions leads to code that is difficult to read and hence debug and maintain.

+ +

+ Note that the operators & and + | are not only integer bitwise operators, they are also the + + non-shortcut versions of the boolean operators + && and ||. +

@@ -40,6 +49,25 @@ integer 3 + + tokens + tokens to check + + subset of tokens + LAND, + BAND, + LOR, + BOR, + BXOR + + + LAND, + BAND, + LOR, + BOR, + BXOR + + @@ -60,6 +88,17 @@ <property name="max" value="7"/> </module> + +

+ To configure the check to ignore & and + |: +

+ +<module name="BooleanExpressionComplexity"> + <property name="tokens" value="BXOR,LAND,LOR"/> +</module> + + diff --git a/src/xdocs/releasenotes.xml b/src/xdocs/releasenotes.xml index 96353c25d..6a37bc9d5 100755 --- a/src/xdocs/releasenotes.xml +++ b/src/xdocs/releasenotes.xml @@ -25,10 +25,19 @@ The calculated value of NPath complexity was subject to integer overflow, so complex code was not always flagged (bug 1654769). +
  • + Fixed misleading documentation for BooleanExpressionComplexity check (bug 1579227). +
  • New features:

    +