diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/AbstractMetric.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/AbstractMetric.java index adcb27d35f..c176975923 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/AbstractMetric.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/AbstractMetric.java @@ -35,30 +35,6 @@ public abstract class AbstractMetric implements Metric { return Metrics.getTopLevelPackageStats(); } - - protected List findAllCalls(ASTMethodOrConstructorDeclaration node) { - List result = new ArrayList<>(); - // TODO:cf - // Needs TypeRes - // Find the qualified names of all methods called in that method's block - return result; - } - - /** - * Default implementation of the supports method, which filters out abstract nodes. Metrics that support abstract - * nodes should override this method. - * - * @param node The node to check. - * - * @return True if the metric can be computed on this node. - */ - @Override - public boolean supports(AccessNode node) { - return !node.isAbstract(); - } - - - // TODO:cf all these methods mimic the behaviour of resultoptions, perhaps not great. /** * Gets the sum of the value of an operation metric over all operations in this class (excluding nested classes). * The computation is not forced (memoized results are used if they can be found). @@ -102,12 +78,14 @@ public abstract class AbstractMetric implements Metric { double total = 0; for (ASTMethodOrConstructorDeclaration op : operations) { double val = Metrics.get(key, op, version); - total += val == Double.NaN ? 1 : val; + total += val == Double.NaN ? 0 : val; } return total / operations.size(); } + // TODO:cf all these methods mimic the behaviour of resultoptions, perhaps not great. + /** * Gets the highest value of an operation metric over all operations in this class (excluding nested classes). * The computation is not forced (memoized results are used if they can be found). @@ -135,10 +113,6 @@ public abstract class AbstractMetric implements Metric { return highest; } - - // TODO:cf this one is computed every time - // TODO:cf it might not be at the best place too (used by ClassStats) - /** * Finds the declaration nodes of all methods or constructors that are declared inside a class. * @@ -169,4 +143,29 @@ public abstract class AbstractMetric implements Metric { return operations; } + protected List findAllCalls(ASTMethodOrConstructorDeclaration node) { + List result = new ArrayList<>(); + // TODO:cf + // Needs TypeRes + // Find the qualified names of all methods called in that method's block + return result; + } + + + // TODO:cf this one is computed every time + // TODO:cf it might not be at the best place too (used by ClassStats) + + /** + * Default implementation of the supports method, which filters out abstract nodes. Metrics that support abstract + * nodes should override this method. + * + * @param node The node to check. + * + * @return True if the metric can be computed on this node. + */ + @Override + public boolean supports(AccessNode node) { + return !node.isAbstract(); + } + } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/ParameterizedMetricKey.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/ParameterizedMetricKey.java index 1986f20ebd..289148cc13 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/ParameterizedMetricKey.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/ParameterizedMetricKey.java @@ -53,7 +53,7 @@ public final class ParameterizedMetricKey { public String toString() { return "ParameterizedMetricKey{" + "key=" + key - + ", options=" + version + + ", version=" + version + '}'; } diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/rule/CyclomaticComplexityRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/rule/CyclomaticComplexityRule.java index c73310664c..fe3bd441a9 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/rule/CyclomaticComplexityRule.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/rule/CyclomaticComplexityRule.java @@ -106,7 +106,7 @@ public class CyclomaticComplexityRule extends AbstractJavaRule { @Override public Object visit(ASTConstructorDeclaration node, Object data) { - int cyclo = (int) Metrics.get(OperationMetricKey.CYCLO, node); + int cyclo = (int) Metrics.get(OperationMetricKey.CYCLO, node, cycloVersion); if (showMethodsComplexity && cyclo >= reportLevel) { addViolation(data, node, new String[] {"constructor", node.getQualifiedName().getOperation(), "" + cyclo}); diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/signature/OperationSignature.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/signature/OperationSignature.java index 60c4f582c2..e456cf830e 100644 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/signature/OperationSignature.java +++ b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/oom/signature/OperationSignature.java @@ -161,7 +161,7 @@ public final class OperationSignature extends Signature { /** Attempts to determine if the method is a setter. */ private static boolean isSetter(ASTMethodDeclaration node, Map fieldNames) { - if (node.getFirstDescendantOfType(ASTFormalParameters.class).jjtGetNumChildren() == 0) { + if (node.getFirstDescendantOfType(ASTFormalParameters.class).jjtGetNumChildren() != 1) { return false; } diff --git a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/metrics/xml/CyclomaticComplexity.xml b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/metrics/xml/CyclomaticComplexity.xml index 00ec7dd44e..c180c72b87 100644 --- a/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/metrics/xml/CyclomaticComplexity.xml +++ b/pmd-java/src/test/resources/net/sourceforge/pmd/lang/java/rule/metrics/xml/CyclomaticComplexity.xml @@ -1,6 +1,6 @@ - + - + Avoid division by 0 when averaging a metric over no operations -1