diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/StdCycloMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/StdCycloMetric.java index 400dcd429c..7c80204264 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/StdCycloMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/metrics/StdCycloMetric.java @@ -40,7 +40,6 @@ public class StdCycloMetric extends AbstractMetric implements OperationMetric, C @Override public double computeFor(ASTMethodOrConstructorDeclaration node, PackageStats holder) { - System.err.println("STD CYCLO!"); Accumulator cyclo = (Accumulator) node.jjtAccept(new OperationVisitor(), new Accumulator()); return cyclo.val; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java index 726aa7a7e0..c9795afde0 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/CyclomaticComplexityRule.java @@ -4,40 +4,22 @@ package net.sourceforge.pmd.lang.java.rule.codesize; -import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; -import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; -import net.sourceforge.pmd.lang.java.oom.Metrics; import net.sourceforge.pmd.lang.java.oom.Metrics.OperationMetricKey; /** - * @author Donald A. Leckie, - * @version $Revision: 5956 $, $Date: 2008-04-04 04:59:25 -0500 (Fri, 04 Apr 2008) $ + * This version calculates the cyclomatic complexity of operations by taking into account the number of paths of the + * boolean expressions of control flow statements. It uses the metrics framework. + * + * @author Donald A. Leckie + * @version Revised June 12th, 2017 (Clément Fournier) + * @see net.sourceforge.pmd.lang.java.oom.metrics.CycloMetric * @since January 14, 2003 */ public class CyclomaticComplexityRule extends StdCyclomaticComplexityRule { - @Override - public Object visit(ASTMethodOrConstructorDeclaration node, Object data) { - if (!isSuppressed(node)) { - ClassEntry classEntry = entryStack.peek(); - - int cyclo = (int) Metrics.get(OperationMetricKey.CYCLO, node); - classEntry.numMethods++; - classEntry.totalCyclo += cyclo; - if (cyclo > classEntry.maxCyclo) { - classEntry.maxCyclo = cyclo; - } - - if (showMethodsComplexity && cyclo >= reportLevel) { - addViolation(data, node, new String[] - {node instanceof ASTMethodDeclaration ? "method" : "constructor", - node.getQualifiedName().getOperation(), - String.valueOf(cyclo),}); - System.err.println(new String[] {node instanceof ASTMethodDeclaration ? "method" : "constructor", - node.getQualifiedName().getOperation(), - String.valueOf(cyclo),}); - } - } - return data; + public CyclomaticComplexityRule() { + super(); + metricKey = OperationMetricKey.CYCLO; } + } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/ModifiedCyclomaticComplexityRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/ModifiedCyclomaticComplexityRule.java index 5e7f11dd18..4e3722486a 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/ModifiedCyclomaticComplexityRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/ModifiedCyclomaticComplexityRule.java @@ -4,48 +4,25 @@ package net.sourceforge.pmd.lang.java.rule.codesize; -import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration; -import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration; -import net.sourceforge.pmd.lang.java.oom.Metrics; import net.sourceforge.pmd.lang.java.oom.Metrics.OperationMetricKey; /** - * Implements the modified cyclomatic complexity rule - *

- * Modified rules: Same as standard cyclomatic complexity, but switch statement + * Implements the modified cyclomatic complexity rule. + * + *

Modified rules: Same as standard cyclomatic complexity, but switch statement * plus all cases count as 1. * * @author Alan Hohn, based on work by Donald A. Leckie + * @version Revised June 12th, 2017 (Clément Fournier) + * @see net.sourceforge.pmd.lang.java.oom.metrics.ModifiedCycloMetric * @since June 18, 2014 */ public class ModifiedCyclomaticComplexityRule extends StdCyclomaticComplexityRule { public ModifiedCyclomaticComplexityRule() { super(); + metricKey = OperationMetricKey.ModifiedCYCLO; } - @Override - public Object visit(ASTMethodOrConstructorDeclaration node, Object data) { - if (!isSuppressed(node)) { - ClassEntry classEntry = entryStack.peek(); - int cyclo = (int) Metrics.get(OperationMetricKey.ModifiedCYCLO, node); - classEntry.numMethods++; - classEntry.totalCyclo += cyclo; - if (cyclo > classEntry.maxCyclo) { - classEntry.maxCyclo = cyclo; - } - - if (showMethodsComplexity && cyclo >= reportLevel) { - addViolation(data, node, new String[] - {node instanceof ASTMethodDeclaration ? "method" : "constructor", - node.getQualifiedName().getOperation(), - String.valueOf(cyclo),}); - System.err.println(new String[] {node instanceof ASTMethodDeclaration ? "method" : "constructor", - node.getQualifiedName().getOperation(), - String.valueOf(cyclo),}); - } - } - return data; - } } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/StdCyclomaticComplexityRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/StdCyclomaticComplexityRule.java index fec927463f..466b35de70 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/StdCyclomaticComplexityRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/codesize/StdCyclomaticComplexityRule.java @@ -25,6 +25,8 @@ import net.sourceforge.pmd.lang.rule.properties.IntegerProperty; * including boolean operators unlike CyclomaticComplexityRule. * * @author Alan Hohn, based on work by Donald A. Leckie + * @version Revised June 12th, 2017 (Clément Fournier) + * @see net.sourceforge.pmd.lang.java.oom.metrics.StdCycloMetric * @since June 18, 2014 */ public class StdCyclomaticComplexityRule extends AbstractJavaRule { @@ -49,6 +51,7 @@ public class StdCyclomaticComplexityRule extends AbstractJavaRule { protected boolean showMethodsComplexity = true; Stack entryStack = new Stack<>(); + protected OperationMetricKey metricKey = OperationMetricKey.StdCYCLO; public StdCyclomaticComplexityRule() { definePropertyDescriptor(REPORT_LEVEL_DESCRIPTOR); @@ -80,7 +83,7 @@ public class StdCyclomaticComplexityRule extends AbstractJavaRule { addViolation(data, node, new String[] {"class", node.getImage(), - classEntry.getCycloAverage() + " (Highest = " + classEntry.maxCyclo + ')',}); + classEntry.getCycloAverage() + " (Highest = " + classEntry.maxCyclo + ')', }); } } return data; @@ -97,12 +100,7 @@ public class StdCyclomaticComplexityRule extends AbstractJavaRule { addViolation(data, node, new String[] {"class", node.getImage(), - classEntry.getCycloAverage() + " (Highest = " + classEntry.maxCyclo + ')',}); - - System.err.println(new String[] - {"class", - node.getImage(), - classEntry.getCycloAverage() + " (Highest = " + classEntry.maxCyclo + ')',}); + classEntry.getCycloAverage() + " (Highest = " + classEntry.maxCyclo + ')', }); } } return data; @@ -123,7 +121,7 @@ public class StdCyclomaticComplexityRule extends AbstractJavaRule { if (!isSuppressed(node)) { ClassEntry classEntry = entryStack.peek(); - int cyclo = (int) Metrics.get(OperationMetricKey.StdCYCLO, node); + int cyclo = (int) Metrics.get(metricKey, node); classEntry.numMethods++; classEntry.totalCyclo += cyclo; if (cyclo > classEntry.maxCyclo) { @@ -134,10 +132,7 @@ public class StdCyclomaticComplexityRule extends AbstractJavaRule { addViolation(data, node, new String[] {node instanceof ASTMethodDeclaration ? "method" : "constructor", node.getQualifiedName().getOperation(), - String.valueOf(cyclo),}); - System.err.println(new String[] {node instanceof ASTMethodDeclaration ? "method" : "constructor", - node.getQualifiedName().getOperation(), - String.valueOf(cyclo), }); + String.valueOf(cyclo), }); } } return data;