Code examples for doc + fix Ncss ignoring asserts
This commit is contained in:
@ -47,19 +47,20 @@ class Foo {
|
|||||||
void baseCyclo() { // Cyclo = 1
|
void baseCyclo() { // Cyclo = 1
|
||||||
highCyclo();
|
highCyclo();
|
||||||
}
|
}
|
||||||
void highCyclo() { // Cyclo =
|
|
||||||
|
void highCyclo() { // Cyclo = 10
|
||||||
int x = 0, y = 2;
|
int x = 0, y = 2;
|
||||||
boolean a = false, b = true;
|
boolean a = false, b = true;
|
||||||
|
|
||||||
if (a && (y == 1 ? b : true)) {
|
if (a && (y == 1 ? b : true)) { // +3
|
||||||
if (y == x) {
|
if (y == x) { // +1
|
||||||
while (true) {
|
while (true) { // +1
|
||||||
if (x++ < 20) {
|
if (x++ < 20) { // +1
|
||||||
break;
|
break; // +1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (y == t && !d) {
|
} else if (y == t && !d) { // +2
|
||||||
x = a ? y : x;
|
x = a ? y : x; // +1
|
||||||
} else {
|
} else {
|
||||||
x = 2;
|
x = 2;
|
||||||
}
|
}
|
||||||
@ -67,7 +68,6 @@ class Foo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Versions
|
### Versions
|
||||||
|
|
||||||
* Version `CycloVersion#IGNORE_BOOLEAN_PATHS`: Boolean operators are not counted, nor are empty
|
* 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`
|
actually represent the size of the class and not of the file. If you don't like that behaviour, use the `JAVANCSS`
|
||||||
version.
|
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
|
### Versions
|
||||||
|
|
||||||
|
@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.metrics.impl.visitors;
|
|||||||
import org.apache.commons.lang3.mutable.MutableInt;
|
import org.apache.commons.lang3.mutable.MutableInt;
|
||||||
|
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration;
|
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.ASTBreakStatement;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement;
|
import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration;
|
||||||
@ -220,4 +221,10 @@ public class DefaultNcssVisitor extends JavaParserVisitorAdapter {
|
|||||||
return super.visit(node, data);
|
return super.visit(node, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object visit(ASTAssertStatement node, Object data) {
|
||||||
|
((MutableInt) data).increment();
|
||||||
|
return super.visit(node, data);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -104,6 +104,7 @@
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
} catch (ThemAll pokemon) {
|
} catch (ThemAll pokemon) {
|
||||||
pokemon.train();
|
pokemon.train();
|
||||||
|
assert pokemon.level > 12;
|
||||||
} finally {
|
} finally {
|
||||||
// Do nothing
|
// Do nothing
|
||||||
}
|
}
|
||||||
@ -125,11 +126,11 @@
|
|||||||
<rule-property name="reportClasses">true</rule-property>
|
<rule-property name="reportClasses">true</rule-property>
|
||||||
<expected-problems>5</expected-problems>
|
<expected-problems>5</expected-problems>
|
||||||
<expected-messages>
|
<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()' has value 2.</message>
|
||||||
<message>'com.company.money.Foo#Foo(int)' has value 13.</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#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>
|
</expected-messages>
|
||||||
<code-ref id="full-example"/>
|
<code-ref id="full-example"/>
|
||||||
</test-code>
|
</test-code>
|
||||||
@ -140,11 +141,11 @@
|
|||||||
<rule-property name="metricVersion">javaNcss</rule-property>
|
<rule-property name="metricVersion">javaNcss</rule-property>
|
||||||
<expected-problems>5</expected-problems>
|
<expected-problems>5</expected-problems>
|
||||||
<expected-messages>
|
<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()' has value 2.</message>
|
||||||
<message>'com.company.money.Foo#Foo(int)' has value 13.</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#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>
|
</expected-messages>
|
||||||
<code-ref id="full-example"/>
|
<code-ref id="full-example"/>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
Reference in New Issue
Block a user