diff --git a/config/checkstyle-examples-suppressions.xml b/config/checkstyle-examples-suppressions.xml index c405aea1b2b..e1ebe902b7c 100644 --- a/config/checkstyle-examples-suppressions.xml +++ b/config/checkstyle-examples-suppressions.xml @@ -62,6 +62,10 @@ + + + diff --git a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/metrics/CyclomaticComplexityCheckExamplesTest.java b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/metrics/CyclomaticComplexityCheckExamplesTest.java index af9b4508c12..35fdbf57592 100644 --- a/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/metrics/CyclomaticComplexityCheckExamplesTest.java +++ b/src/xdocs-examples/java/com/puppycrawl/tools/checkstyle/checks/metrics/CyclomaticComplexityCheckExamplesTest.java @@ -19,12 +19,10 @@ package com.puppycrawl.tools.checkstyle.checks.metrics; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import com.puppycrawl.tools.checkstyle.AbstractExamplesModuleTestSupport; -@Disabled("until https://github.com/checkstyle/checkstyle/issues/13345") public class CyclomaticComplexityCheckExamplesTest extends AbstractExamplesModuleTestSupport { @Override protected String getPackageLocation() { @@ -34,27 +32,27 @@ protected String getPackageLocation() { @Test public void testExample1() throws Exception { final String[] expected = { - + "32:3: " + getCheckMessage(CyclomaticComplexityCheck.MSG_KEY, 13, 10), }; - verifyWithInlineConfigParser(getPath("Example1.txt"), expected); + verifyWithInlineConfigParser(getPath("Example1.java"), expected); } @Test public void testExample2() throws Exception { final String[] expected = { - + "17:3: " + getCheckMessage(CyclomaticComplexityCheck.MSG_KEY, 5, 4), }; - verifyWithInlineConfigParser(getPath("Example2.txt"), expected); + verifyWithInlineConfigParser(getPath("Example2.java"), expected); } @Test public void testExample3() throws Exception { final String[] expected = { - + "34:3: " + getCheckMessage(CyclomaticComplexityCheck.MSG_KEY, 11, 10), }; - verifyWithInlineConfigParser(getPath("Example3.txt"), expected); + verifyWithInlineConfigParser(getPath("Example3.java"), expected); } } diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example1.java b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example1.java new file mode 100644 index 00000000000..e3955328325 --- /dev/null +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example1.java @@ -0,0 +1,67 @@ +/*xml + + + + + +*/ +package com.puppycrawl.tools.checkstyle.checks.metrics.cyclomaticcomplexity; + +// xdoc section -- start +class Example1 { + int a, b, c, d, e, n; + + public void testMethod1() { + while (a < b && a > c) { + fun1(); + } + if (a == b) { + do { + fun1(); + } while (d==a); + } else if (c == d) { + while (c > 0) { + fun1(); + } + do { + fun1(); + } while (a==d); + } + } + // violation below, 'Cyclomatic Complexity is 13 (max allowed is 10)' + public void testMethod2() { // 1, function declaration + if (a == b) { // 2, if + fun1(); + } else if (a == 0 // 3, if + && b == c) { // 4, && operator + if (c == -1) { // 5, if + fun1(); + } + } else if (a == c // 6, if + || a == d) { // 7, || operator + fun1(); + } else if (d == e) { //8, if + try { + fun1(); + } catch (Exception e) { // 9, catch + } + } else { + switch(n) { + case 1: // 10, case + fun1(); + break; + case 2: // 11, case + fun1(); + break; + case 3: // 12, case + fun1(); + break; + default: + break; + } + } + a = a > 0 ? b : c; // 13, ternary operator + } + private void fun1() {} +} +// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example1.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example1.txt deleted file mode 100644 index 0646df882b7..00000000000 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example1.txt +++ /dev/null @@ -1,44 +0,0 @@ -/*xml - - - - - -*/ - -// xdoc section -- start -class CyclomaticComplexity { - // Cyclomatic Complexity = 11 - int a, b, c, d, n; - public void foo() { // 1, function declaration - if (a == 1) { // 2, if - fun1(); - } else if (a == b // 3, if - && a == c) { // 4, && operator - if (c == 2) { // 5, if - fun2(); - } - } else if (a == d) { // 6, if - try { - fun4(); - } catch (Exception e) { // 7, catch - } - } else { - switch(n) { - case 1: // 8, case - fun1(); - break; - case 2: // 9, case - fun2(); - break; - case 3: // 10, case - fun3(); - break; - default: - break; - } - } - d = a < 0 ? -1 : 1; // 11, ternary operator - } -} -// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example2.java b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example2.java new file mode 100644 index 00000000000..428cee90025 --- /dev/null +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example2.java @@ -0,0 +1,70 @@ +/*xml + + + + + + + + +*/ +package com.puppycrawl.tools.checkstyle.checks.metrics.cyclomaticcomplexity; + +// xdoc section -- start +class Example2 { + int a, b, c, d, e, n; + // violation below, 'Cyclomatic Complexity is 5 (max allowed is 4)' + public void testMethod1() { // 1, function declaration + while (a < b && a > c) { // 2, while + fun1(); + } + if (a == b) { + do { // 3, do + fun1(); + } while (d==a); + } else if (c == d) { + while (c > 0) { // 4, while + fun1(); + } + do { // 5, do-while + fun1(); + } while (a==d); + } + } + + public void testMethod2() { + if (a == b) { + fun1(); + } else if (a == 0 + && b == c) { + if (c == -1) { + fun1(); + } + } else if (a == c + || a == d) { + fun1(); + } else if (d == e) { + try { + fun1(); + } catch (Exception e) { + } + } else { + switch(n) { + case 1: + fun1(); + break; + case 2: + fun1(); + break; + case 3: + fun1(); + break; + default: + break; + } + } + a = a > 0 ? b : c; + } + private void fun1() {} +} +// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example2.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example2.txt deleted file mode 100644 index 4af4cbcc74b..00000000000 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example2.txt +++ /dev/null @@ -1,35 +0,0 @@ -/*xml - - - - - - - - -*/ - -// xdoc section -- start -class CyclomaticComplexity { - // Cyclomatic Complexity = 5 - int a, b, c, d; - public void foo() { // 1, function declaration - while (a < b // 2, while - && a > c) { - fun(); - } - if (a == b) { - do { // 3, do - fun(); - } while (d); - } else if (c == d) { - while (c > 0) { // 4, while - fun(); - } - do { // 5, do-while - fun(); - } while (a); - } - } -} -// xdoc section -- end diff --git a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example3.txt b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example3.java similarity index 55% rename from src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example3.txt rename to src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example3.java index 4e84c5b6844..b8ba68a2d11 100644 --- a/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example3.txt +++ b/src/xdocs-examples/resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example3.java @@ -7,25 +7,44 @@ */ +package com.puppycrawl.tools.checkstyle.checks.metrics.cyclomaticcomplexity; // xdoc section -- start -class CyclomaticComplexity { - // Cyclomatic Complexity = 11 +class Example3 { int a, b, c, d, e, n; - public void foo() { // 1, function declaration + + public void testMethod1() { + while (a < b && a > c) { + fun1(); + } + if (a == b) { + do { + fun1(); + } while (d==a); + } else if (c == d) { + while (c > 0) { + fun1(); + } + do { + fun1(); + } while (a==d); + } + } + // violation below, 'Cyclomatic Complexity is 11 (max allowed is 10)' + public void testMethod2() { // 1, function declaration if (a == b) { // 2, if fun1(); } else if (a == 0 // 3, if && b == c) { // 4, && operator if (c == -1) { // 5, if - fun2(); + fun1(); } } else if (a == c // 6, if || a == d) { // 7, || operator - fun3(); + fun1(); } else if (d == e) { // 8, if try { - fun4(); + fun1(); } catch (Exception e) { // 9, catch } } else { @@ -34,7 +53,10 @@ public void foo() { // 1, function declaration fun1(); break; case 2: - fun2(); + fun1(); + break; + case 3: // 10, case + fun1(); break; default: break; @@ -42,5 +64,6 @@ public void foo() { // 1, function declaration } a = a > 0 ? b : c; // 11, ternary operator } + private void fun1() {} } // xdoc section -- end diff --git a/src/xdocs/checks/metrics/cyclomaticcomplexity.xml b/src/xdocs/checks/metrics/cyclomaticcomplexity.xml index 3efde4081ac..1e55348a64f 100644 --- a/src/xdocs/checks/metrics/cyclomaticcomplexity.xml +++ b/src/xdocs/checks/metrics/cyclomaticcomplexity.xml @@ -148,39 +148,61 @@ Example:

-class CyclomaticComplexity { - // Cyclomatic Complexity = 11 - int a, b, c, d, n; - public void foo() { // 1, function declaration - if (a == 1) { // 2, if +class Example1 { + int a, b, c, d, e, n; + + public void testMethod1() { + while (a < b && a > c) { fun1(); - } else if (a == b // 3, if - && a == c) { // 4, && operator - if (c == 2) { // 5, if - fun2(); + } + if (a == b) { + do { + fun1(); + } while (d==a); + } else if (c == d) { + while (c > 0) { + fun1(); } - } else if (a == d) { // 6, if + do { + fun1(); + } while (a==d); + } + } + // violation below, 'Cyclomatic Complexity is 13 (max allowed is 10)' + public void testMethod2() { // 1, function declaration + if (a == b) { // 2, if + fun1(); + } else if (a == 0 // 3, if + && b == c) { // 4, && operator + if (c == -1) { // 5, if + fun1(); + } + } else if (a == c // 6, if + || a == d) { // 7, || operator + fun1(); + } else if (d == e) { //8, if try { - fun4(); - } catch (Exception e) { // 7, catch + fun1(); + } catch (Exception e) { // 9, catch } } else { switch(n) { - case 1: // 8, case + case 1: // 10, case fun1(); break; - case 2: // 9, case - fun2(); + case 2: // 11, case + fun1(); break; - case 3: // 10, case - fun3(); + case 3: // 12, case + fun1(); break; default: break; } } - d = a < 0 ? -1 : 1; // 11, ternary operator + a = a > 0 ? b : c; // 13, ternary operator } + private void fun1() {} }

@@ -200,27 +222,61 @@ class CyclomaticComplexity { Example:

-class CyclomaticComplexity { - // Cyclomatic Complexity = 5 - int a, b, c, d; - public void foo() { // 1, function declaration - while (a < b // 2, while - && a > c) { - fun(); +class Example2 { + int a, b, c, d, e, n; + // violation below, 'Cyclomatic Complexity is 5 (max allowed is 4)' + public void testMethod1() { // 1, function declaration + while (a < b && a > c) { // 2, while + fun1(); } if (a == b) { do { // 3, do - fun(); - } while (d); + fun1(); + } while (d==a); } else if (c == d) { while (c > 0) { // 4, while - fun(); + fun1(); } do { // 5, do-while - fun(); - } while (a); + fun1(); + } while (a==d); } } + + public void testMethod2() { + if (a == b) { + fun1(); + } else if (a == 0 + && b == c) { + if (c == -1) { + fun1(); + } + } else if (a == c + || a == d) { + fun1(); + } else if (d == e) { + try { + fun1(); + } catch (Exception e) { + } + } else { + switch(n) { + case 1: + fun1(); + break; + case 2: + fun1(); + break; + case 3: + fun1(); + break; + default: + break; + } + } + a = a > 0 ? b : c; + } + private void fun1() {} }

@@ -239,23 +295,41 @@ class CyclomaticComplexity { Example:

-class CyclomaticComplexity { - // Cyclomatic Complexity = 11 +class Example3 { int a, b, c, d, e, n; - public void foo() { // 1, function declaration + + public void testMethod1() { + while (a < b && a > c) { + fun1(); + } + if (a == b) { + do { + fun1(); + } while (d==a); + } else if (c == d) { + while (c > 0) { + fun1(); + } + do { + fun1(); + } while (a==d); + } + } + // violation below, 'Cyclomatic Complexity is 11 (max allowed is 10)' + public void testMethod2() { // 1, function declaration if (a == b) { // 2, if fun1(); } else if (a == 0 // 3, if && b == c) { // 4, && operator if (c == -1) { // 5, if - fun2(); + fun1(); } } else if (a == c // 6, if || a == d) { // 7, || operator - fun3(); + fun1(); } else if (d == e) { // 8, if try { - fun4(); + fun1(); } catch (Exception e) { // 9, catch } } else { @@ -264,7 +338,10 @@ class CyclomaticComplexity { fun1(); break; case 2: - fun2(); + fun1(); + break; + case 3: // 10, case + fun1(); break; default: break; @@ -272,6 +349,7 @@ class CyclomaticComplexity { } a = a > 0 ? b : c; // 11, ternary operator } + private void fun1() {} } diff --git a/src/xdocs/checks/metrics/cyclomaticcomplexity.xml.template b/src/xdocs/checks/metrics/cyclomaticcomplexity.xml.template index 9f648ac4a0e..83b5aa2b917 100644 --- a/src/xdocs/checks/metrics/cyclomaticcomplexity.xml.template +++ b/src/xdocs/checks/metrics/cyclomaticcomplexity.xml.template @@ -65,7 +65,7 @@

+ value="resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example1.java"/>

@@ -73,7 +73,7 @@

+ value="resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example1.java"/>

@@ -81,7 +81,7 @@

+ value="resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example2.java"/>

@@ -89,7 +89,7 @@

+ value="resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example2.java"/>

@@ -97,7 +97,7 @@

+ value="resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example3.java"/>

@@ -105,7 +105,7 @@

+ value="resources/com/puppycrawl/tools/checkstyle/checks/metrics/cyclomaticcomplexity/Example3.java"/>