Finish cyclo

This commit is contained in:
Clément Fournier committed 2020-12-11 08:37:23 +01:00
1 parent bde3626b05
commit e46ed39b06
5 files changed
+16 -14

No files matched your search

@@ -7,6 +7,7 @@ package net.sourceforge.pmd.lang.java.internal;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTInfixExpression;
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchBranch;
import net.sourceforge.pmd.lang.java.ast.BinaryOp;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.symbols.JFieldSymbol;
@@ -83,4 +84,8 @@ public final class JavaAstUtils {
}
return false;
}
public static int numAlternatives(ASTSwitchBranch n) {
return n.isDefault() ? 1 : n.getLabel().getExprList().count();
}
}
@@ -16,12 +16,14 @@ import net.sourceforge.pmd.lang.java.ast.ASTIfStatement;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchBranch;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchExpression;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchFallthroughBranch;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLike;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement;
import net.sourceforge.pmd.lang.java.ast.ASTThrowStatement;
import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.JavaVisitorBase;
import net.sourceforge.pmd.lang.java.internal.JavaAstUtils;
import net.sourceforge.pmd.lang.java.metrics.api.JavaMetrics;
import net.sourceforge.pmd.lang.java.metrics.api.JavaMetrics.CycloOption;
import net.sourceforge.pmd.lang.metrics.MetricOptions;
@@ -75,13 +77,11 @@ public class CycloVisitor extends JavaVisitorBase<MutableInt, Void> {
}
if (considerBooleanPaths) {
data.add(branch.getLabel().getExprList().count());
data.add(JavaAstUtils.numAlternatives(branch));
} else if (branch instanceof ASTSwitchFallthroughBranch
&& ((ASTSwitchFallthroughBranch) branch).getStatements().nonEmpty()) {
data.increment();
}
// else if (branch instanceof ASTSwitchFallthroughBranch) {
// if (considerBooleanPaths && ((ASTSwitchFallthroughBranch) branch).getStatements().isEmpty())
// an empty label is only counted if we count boolean paths
// data.increment();
// }
}
return visitJavaNode(node, data);
@@ -26,6 +26,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTTryStatement;
import net.sourceforge.pmd.lang.java.ast.ASTWhileStatement;
import net.sourceforge.pmd.lang.java.ast.JavaNode;
import net.sourceforge.pmd.lang.java.ast.JavaVisitorBase;
import net.sourceforge.pmd.lang.java.internal.JavaAstUtils;
import net.sourceforge.pmd.lang.java.metrics.api.JavaMetrics;
@@ -163,7 +164,7 @@ public class NpathBaseVisitor extends JavaVisitorBase<Void, BigInteger> {
// Fall-through labels count as 1 for complexity
if (n instanceof ASTSwitchFallthroughBranch) {
caseRange += numAlternatives(n);
caseRange += JavaAstUtils.numAlternatives(n);
NodeStream<ASTStatement> statements = ((ASTSwitchFallthroughBranch) n).getStatements();
if (statements.nonEmpty()) {
BigInteger branchNpath = multiplyComplexities(statements);
@@ -171,7 +172,7 @@ public class NpathBaseVisitor extends JavaVisitorBase<Void, BigInteger> {
caseRange = 0;
}
} else if (n instanceof ASTSwitchArrowBranch) {
int numAlts = numAlternatives(n);
int numAlts = JavaAstUtils.numAlternatives(n);
BigInteger branchNpath = ((ASTSwitchArrowBranch) n).getRightHandSide().acceptVisitor(this, data);
npath = npath.add(branchNpath.multiply(BigInteger.valueOf(numAlts)));
}
@@ -180,10 +181,6 @@ public class NpathBaseVisitor extends JavaVisitorBase<Void, BigInteger> {
return npath.add(BigInteger.valueOf(boolCompSwitch));
}
private int numAlternatives(ASTSwitchBranch n) {
return n.isDefault() ? 1 : n.getLabel().getExprList().count();
}
@Override
public BigInteger visit(ASTSwitchLabel node, Void data) {
if (node.isDefault()) {
@@ -22,7 +22,7 @@ public class AllMetricsTest extends SimpleAggregatorTst {
@Override
public void setUp() {
// addRule(RULESET, "CycloTest");
addRule(RULESET, "CycloTest");
addRule(RULESET, "NcssTest");
addRule(RULESET, "WmcTest");
addRule(RULESET, "LocTest");
@@ -340,7 +340,7 @@ public class LambdaTest {
<test-code>
<description>Complexity of lambdas doesn't affect the complexity of the method, refs #837</description>
<expected-problems>4</expected-problems>
<expected-problems>1</expected-problems>
<expected-messages>
<message>'LambdaTest#notSoComplex(int)' has value 4.</message>
</expected-messages>