From 02307ca8d5d7a76086d0f4a6a46a96693776b5bb Mon Sep 17 00:00:00 2001 From: oowekyala Date: Mon, 7 Aug 2017 17:08:40 +0200 Subject: [PATCH] Code examples for doc + fix Ncss ignoring asserts --- .../pages/pmd/languages/java_metrics_index.md | 50 +++++++++++++++---- .../impl/visitors/DefaultNcssVisitor.java | 7 +++ .../lang/java/metrics/impl/xml/NcssTest.xml | 9 ++-- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/docs/pages/pmd/languages/java_metrics_index.md b/docs/pages/pmd/languages/java_metrics_index.md index 1d96c3b193..23e5032454 100644 --- a/docs/pages/pmd/languages/java_metrics_index.md +++ b/docs/pages/pmd/languages/java_metrics_index.md @@ -44,22 +44,23 @@ control flow statement in itself. ```java class Foo { - void baseCyclo() { // Cyclo = 1 + void baseCyclo() { // Cyclo = 1 highCyclo(); } - void highCyclo() { // Cyclo = + + void highCyclo() { // Cyclo = 10 int x = 0, y = 2; boolean a = false, b = true; - if (a && (y == 1 ? b : true)) { - if (y == x) { - while (true) { - if (x++ < 20) { - break; + if (a && (y == 1 ? b : true)) { // +3 + if (y == x) { // +1 + while (true) { // +1 + if (x++ < 20) { // +1 + break; // +1 } } - } else if (y == t && !d) { - x = a ? y : x; + } else if (y == t && !d) { // +2 + x = a ? y : x; // +1 } else { x = 2; } @@ -67,7 +68,6 @@ class Foo { } } ``` - ### Versions * Version `CycloVersion#IGNORE_BOOLEAN_PATHS`: Boolean operators are not counted, nor are empty @@ -108,6 +108,36 @@ This makes it easier to compare nested classes to outer classes. Besides, it mak actually represent the size of the class and not of the file. If you don't like that behaviour, use the `JAVANCSS` version. +### Code example +```java +import java.util.Collections; // +0 +import java.io.IOException; // +0 + +class Foo { // +1, total Ncss = 12 + + public void bigMethod() // +1 + throws IOException { + int x = 0, y = 2; // +1 + boolean a = false, b = true; // +1 + + if (a || b) { // +1 + try { // +1 + do { // +1 + x += 2; // +1 + } while (x < 12); + + System.exit(0); // +1 + } catch (IOException ioe) { // +1 + throw new PatheticFailException(ioe); // +1 + } + } else { + assert false; // +1 + } + } +} +``` + + ### Versions diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/DefaultNcssVisitor.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/DefaultNcssVisitor.java index 082e55ceab..79da9a5e22 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/DefaultNcssVisitor.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/metrics/impl/visitors/DefaultNcssVisitor.java @@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.metrics.impl.visitors; import org.apache.commons.lang3.mutable.MutableInt; import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration; +import net.sourceforge.pmd.lang.java.ast.ASTAssertStatement; import net.sourceforge.pmd.lang.java.ast.ASTBreakStatement; import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement; import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; @@ -220,4 +221,10 @@ public class DefaultNcssVisitor extends JavaParserVisitorAdapter { return super.visit(node, data); } + @Override + public Object visit(ASTAssertStatement node, Object data) { + ((MutableInt) data).increment(); + return super.visit(node, data); + } + } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/NcssTest.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/NcssTest.xml index ecf84267fa..80c26d932c 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/NcssTest.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/metrics/impl/xml/NcssTest.xml @@ -104,6 +104,7 @@ e.printStackTrace(); } catch (ThemAll pokemon) { pokemon.train(); + assert pokemon.level > 12; } finally { // Do nothing } @@ -125,11 +126,11 @@ true 5 - 'com.company.money.Foo' has value 65 highest 20. + 'com.company.money.Foo' has value 66 highest 21. 'com.company.money.Foo#Foo()' has value 2. 'com.company.money.Foo#Foo(int)' has value 13. 'com.company.money.Foo#foo()' has value 14. - 'com.company.money.Foo#main(String)' has value 20. + 'com.company.money.Foo#main(String)' has value 21. @@ -140,11 +141,11 @@ javaNcss 5 - 'com.company.money.Foo' has value 68 highest 20. + 'com.company.money.Foo' has value 69 highest 21. 'com.company.money.Foo#Foo()' has value 2. 'com.company.money.Foo#Foo(int)' has value 13. 'com.company.money.Foo#foo()' has value 14. - 'com.company.money.Foo#main(String)' has value 20. + 'com.company.money.Foo#main(String)' has value 21.