Code examples for doc + fix Ncss ignoring asserts

This commit is contained in:
oowekyala
2017-08-07 17:08:40 +02:00
parent e9f4674f30
commit 02307ca8d5
3 changed files with 52 additions and 14 deletions

View File

@ -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

View File

@ -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);
}
}

View File

@ -104,6 +104,7 @@
e.printStackTrace();
} catch (ThemAll pokemon) {
pokemon.train();
assert pokemon.level > 12;
} finally {
// Do nothing
}
@ -125,11 +126,11 @@
<rule-property name="reportClasses">true</rule-property>
<expected-problems>5</expected-problems>
<expected-messages>
<message>'com.company.money.Foo' has value 65 highest 20.</message>
<message>'com.company.money.Foo' has value 66 highest 21.</message>
<message>'com.company.money.Foo#Foo()' has value 2.</message>
<message>'com.company.money.Foo#Foo(int)' has value 13.</message>
<message>'com.company.money.Foo#foo()' has value 14.</message>
<message>'com.company.money.Foo#main(String)' has value 20.</message>
<message>'com.company.money.Foo#main(String)' has value 21.</message>
</expected-messages>
<code-ref id="full-example"/>
</test-code>
@ -140,11 +141,11 @@
<rule-property name="metricVersion">javaNcss</rule-property>
<expected-problems>5</expected-problems>
<expected-messages>
<message>'com.company.money.Foo' has value 68 highest 20.</message>
<message>'com.company.money.Foo' has value 69 highest 21.</message>
<message>'com.company.money.Foo#Foo()' has value 2.</message>
<message>'com.company.money.Foo#Foo(int)' has value 13.</message>
<message>'com.company.money.Foo#foo()' has value 14.</message>
<message>'com.company.money.Foo#main(String)' has value 20.</message>
<message>'com.company.money.Foo#main(String)' has value 21.</message>
</expected-messages>
<code-ref id="full-example"/>
</test-code>