Merge branch 'master' of https://github.com/pmd/pmd into metrics-apex

This commit is contained in:
oowekyala
2017-08-07 18:01:39 +02:00
10 changed files with 40 additions and 9 deletions

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.lang.apex.metrics;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.pmd.lang.apex.ast.ASTMethod;
@ -12,14 +13,23 @@ import net.sourceforge.pmd.lang.metrics.AbstractMetricsComputer;
/**
* Computes metrics for the Apex framework.
*
* @author Clément Fournier
*/
public class ApexMetricsComputer extends AbstractMetricsComputer<ASTUserClassOrInterface<?>, ASTMethod> {
public static final ApexMetricsComputer INSTANCE = new ApexMetricsComputer();
static final ApexMetricsComputer INSTANCE = new ApexMetricsComputer();
@Override
protected List<ASTMethod> findOperations(ASTUserClassOrInterface<?> node) {
return node.findChildrenOfType(ASTMethod.class);
List<ASTMethod> candidates = node.findChildrenOfType(ASTMethod.class);
List<ASTMethod> result = new ArrayList<>(candidates);
for (ASTMethod method : candidates) {
if (method.getImage().matches("(<clinit>|<init>|clone)")) {
result.remove(method);
}
}
return result;
}
}

View File

@ -10,6 +10,8 @@ import net.sourceforge.pmd.lang.apex.metrics.AbstractApexMetric;
import net.sourceforge.pmd.lang.apex.metrics.api.ApexClassMetric;
/**
* Base class for Apex metrics.
*
* @author Clément Fournier
*/
public abstract class AbstractApexClassMetric extends AbstractApexMetric implements ApexClassMetric {

View File

@ -10,10 +10,20 @@ import net.sourceforge.pmd.lang.apex.metrics.AbstractApexMetric;
import net.sourceforge.pmd.lang.apex.metrics.api.ApexOperationMetric;
/**
* Base class for Apex operation metrics.
*
* @author Clément Fournier
*/
public abstract class AbstractApexOperationMetric extends AbstractApexMetric implements ApexOperationMetric {
/**
* Checks if the metric can be computed on the node. For now, we filter out {@literal <clinit>, <init> and clone},
* which are present in all apex class nodes even if they're not implemented, which may yield unexpected results.
*
* @param node The node to check
*
* @return True if the metric can be computed
*/
@Override
public boolean supports(ASTMethod node) {
return !node.getImage().matches("(<clinit>|<init>|clone)")

View File

@ -29,8 +29,16 @@ public class CycloMetric extends AbstractApexOperationMetric {
public double computeFor(ASTMethod node, MetricVersion version) {
return ((MutableInt) node.jjtAccept(new StandardCycloVisitor(), new MutableInt(1))).doubleValue();
}
/**
* Computes the number of control flow paths through that expression, which is the number of {@code ||} and {@code
* &&} operators. Used both by Npath and Cyclo.
*
* @param expression Boolean expression
*
* @return The complexity of the expression
*/
public static int booleanExpressionComplexity(ASTStandardCondition expression) {
Set<ASTBooleanExpression> subs = new HashSet<>(expression.findDescendantsOfType(ASTBooleanExpression.class));
int complexity = 0;

View File

@ -11,6 +11,8 @@ import net.sourceforge.pmd.lang.metrics.MetricVersion;
import net.sourceforge.pmd.lang.metrics.ResultOption;
/**
* See the doc for the Java metric.
*
* @author Clément Fournier
*/
public class WmcMetric extends AbstractApexClassMetric {

View File

@ -14,7 +14,6 @@ import java.util.Random;
import org.junit.Test;
import net.sourceforge.pmd.lang.MetricKeyUtil;
import net.sourceforge.pmd.lang.apex.ast.ASTMethod;
import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
import net.sourceforge.pmd.lang.apex.ast.ASTUserClassOrInterface;
@ -24,6 +23,7 @@ import net.sourceforge.pmd.lang.apex.metrics.impl.AbstractApexClassMetric;
import net.sourceforge.pmd.lang.apex.metrics.impl.AbstractApexOperationMetric;
import net.sourceforge.pmd.lang.metrics.Metric.Version;
import net.sourceforge.pmd.lang.metrics.MetricKey;
import net.sourceforge.pmd.lang.metrics.MetricKeyUtil;
import net.sourceforge.pmd.lang.metrics.MetricMemoizer;
import net.sourceforge.pmd.lang.metrics.MetricVersion;

View File

@ -2,13 +2,11 @@
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang;
package net.sourceforge.pmd.lang.metrics;
import java.util.Objects;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.metrics.Metric;
import net.sourceforge.pmd.lang.metrics.MetricKey;
/**
* Holds the key creation method until we move it to the MetricKey interface.

View File

@ -12,11 +12,11 @@ import static org.junit.Assert.assertTrue;
import org.junit.Test;
import net.sourceforge.pmd.lang.MetricKeyUtil;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.metrics.api.JavaClassMetricKey;
import net.sourceforge.pmd.lang.java.metrics.api.JavaOperationMetricKey;
import net.sourceforge.pmd.lang.metrics.MetricKey;
import net.sourceforge.pmd.lang.metrics.MetricKeyUtil;
import net.sourceforge.pmd.lang.metrics.MetricVersion;
import net.sourceforge.pmd.lang.metrics.ParameterizedMetricKey;

View File

@ -14,7 +14,6 @@ import java.util.Random;
import org.junit.Test;
import net.sourceforge.pmd.lang.MetricKeyUtil;
import net.sourceforge.pmd.lang.java.ast.ASTAnyTypeDeclaration;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
@ -24,6 +23,7 @@ import net.sourceforge.pmd.lang.java.metrics.impl.AbstractJavaOperationMetric;
import net.sourceforge.pmd.lang.java.metrics.testdata.MetricsVisitorTestData;
import net.sourceforge.pmd.lang.metrics.Metric.Version;
import net.sourceforge.pmd.lang.metrics.MetricKey;
import net.sourceforge.pmd.lang.metrics.MetricKeyUtil;
import net.sourceforge.pmd.lang.metrics.MetricMemoizer;
import net.sourceforge.pmd.lang.metrics.MetricVersion;

View File

@ -132,4 +132,5 @@ and include them to such reports.
* [#530](https://github.com/pmd/pmd/pull/530): \[java] Fix issue #527: Lombok getter annotation on enum is not recognized correctly - [Clément Fournier](https://github.com/oowekyala)
* [#535](https://github.com/pmd/pmd/pull/535): \[apex] Fix broken Apex visitor adapter - [Clément Fournier](https://github.com/oowekyala)
* [#529](https://github.com/pmd/pmd/pull/529): \[java] Abstracted the Java metrics framework - [Clément Fournier](https://github.com/oowekyala)
* [#542](https://github.com/pmd/pmd/pull/542): \[java] Metrics abstraction - [Clément Fournier](https://github.com/oowekyala)