MetricOptions bundle, decorator doesn't work

The decorator needs some work because the decorated visitor takes over control to visit the children
This commit is contained in:
oowekyala
2017-08-17 14:11:32 +02:00
committed by Clément Fournier
parent 6408b57b8e
commit ceba5afece
28 changed files with 183 additions and 155 deletions

View File

@ -9,7 +9,7 @@ import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
import net.sourceforge.pmd.lang.apex.ast.ASTUserClassOrInterface;
import net.sourceforge.pmd.lang.metrics.Metric.Version;
import net.sourceforge.pmd.lang.metrics.MetricKey;
import net.sourceforge.pmd.lang.metrics.MetricVersion;
import net.sourceforge.pmd.lang.metrics.MetricOptions;
import net.sourceforge.pmd.lang.metrics.ResultOption;
/**
@ -58,7 +58,7 @@ public final class ApexMetrics {
/**
* Computes a metric identified by its code on a class AST node, possibly selecting a variant with the {@code
* MetricVersion} parameter.
* MetricOptions} parameter.
*
* @param key The key identifying the metric to be computed
* @param node The node on which to compute the metric
@ -66,7 +66,7 @@ public final class ApexMetrics {
*
* @return The value of the metric, or {@code Double.NaN} if the value couln't be computed
*/
public static double get(MetricKey<ASTUserClassOrInterface<?>> key, ASTUserClass node, MetricVersion version) {
public static double get(MetricKey<ASTUserClassOrInterface<?>> key, ASTUserClass node, MetricOptions version) {
return FACADE.computeForType(key, node, version);
}
@ -93,7 +93,7 @@ public final class ApexMetrics {
*
* @return The value of the metric, or {@code Double.NaN} if the value couln't be computed
*/
public static double get(MetricKey<ASTMethod> key, ASTMethod node, MetricVersion version) {
public static double get(MetricKey<ASTMethod> key, ASTMethod node, MetricOptions version) {
return FACADE.computeForOperation(key, node, version);
}
@ -126,7 +126,7 @@ public final class ApexMetrics {
* @return The value of the metric, or {@code Double.NaN} if the value couln't be computed or {@code option} is
* {@code null}
*/
public static double get(MetricKey<ASTMethod> key, ASTUserClassOrInterface<?> node, MetricVersion version,
public static double get(MetricKey<ASTMethod> key, ASTUserClassOrInterface<?> node, MetricOptions version,
ResultOption option) {
return FACADE.computeWithResultOption(key, node, version, option);
}

View File

@ -13,7 +13,7 @@ import net.sourceforge.pmd.lang.apex.ast.ASTBooleanExpression;
import net.sourceforge.pmd.lang.apex.ast.ASTMethod;
import net.sourceforge.pmd.lang.apex.ast.ASTStandardCondition;
import net.sourceforge.pmd.lang.apex.metrics.impl.visitors.StandardCycloVisitor;
import net.sourceforge.pmd.lang.metrics.MetricVersion;
import net.sourceforge.pmd.lang.metrics.MetricOptions;
import apex.jorje.data.ast.BooleanOp;
@ -26,7 +26,7 @@ public class CycloMetric extends AbstractApexOperationMetric {
@Override
public double computeFor(ASTMethod node, MetricVersion version) {
public double computeFor(ASTMethod node, MetricOptions options) {
return ((MutableInt) node.jjtAccept(new StandardCycloVisitor(), new MutableInt(1))).doubleValue();
}

View File

@ -7,7 +7,7 @@ package net.sourceforge.pmd.lang.apex.metrics.impl;
import net.sourceforge.pmd.lang.apex.ast.ASTUserClassOrInterface;
import net.sourceforge.pmd.lang.apex.metrics.ApexMetrics;
import net.sourceforge.pmd.lang.apex.metrics.api.ApexOperationMetricKey;
import net.sourceforge.pmd.lang.metrics.MetricVersion;
import net.sourceforge.pmd.lang.metrics.MetricOptions;
import net.sourceforge.pmd.lang.metrics.ResultOption;
/**
@ -18,7 +18,7 @@ import net.sourceforge.pmd.lang.metrics.ResultOption;
public class WmcMetric extends AbstractApexClassMetric {
@Override
public double computeFor(ASTUserClassOrInterface<?> node, MetricVersion version) {
public double computeFor(ASTUserClassOrInterface<?> node, MetricOptions options) {
return ApexMetrics.get(ApexOperationMetricKey.CYCLO, node, ResultOption.SUM);
}
}

View File

@ -27,7 +27,7 @@ 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;
import net.sourceforge.pmd.lang.metrics.MetricOptions;
import apex.jorje.semantic.ast.compilation.Compilation;
@ -109,7 +109,7 @@ public class ApexProjectMirrorTest {
@Override
public double computeFor(ASTMethod node, MetricVersion version) {
public double computeFor(ASTMethod node, MetricOptions options) {
return random.nextInt();
}
}
@ -120,7 +120,7 @@ public class ApexProjectMirrorTest {
@Override
public double computeFor(ASTUserClassOrInterface<?> node, MetricVersion version) {
public double computeFor(ASTUserClassOrInterface<?> node, MetricOptions options) {
return random.nextInt();
}
}

View File

@ -4,6 +4,7 @@
package net.sourceforge.pmd.lang.apex.metrics.impl;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@ -13,24 +14,24 @@ import net.sourceforge.pmd.lang.apex.metrics.ApexMetrics;
import net.sourceforge.pmd.lang.apex.metrics.api.ApexClassMetricKey;
import net.sourceforge.pmd.lang.apex.metrics.api.ApexOperationMetricKey;
import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
import net.sourceforge.pmd.lang.metrics.Metric.Version;
import net.sourceforge.pmd.lang.metrics.MetricVersion;
import net.sourceforge.pmd.lang.metrics.MetricOption;
import net.sourceforge.pmd.lang.metrics.MetricOptions;
import net.sourceforge.pmd.lang.metrics.ResultOption;
import net.sourceforge.pmd.lang.rule.properties.BooleanProperty;
import net.sourceforge.pmd.lang.rule.properties.DoubleProperty;
import net.sourceforge.pmd.lang.rule.properties.EnumeratedProperty;
import net.sourceforge.pmd.lang.rule.properties.EnumeratedMultiProperty;
/**
* Abstract test rule for a metric. Tests of metrics use the standard framework for rule testing, using one dummy rule
* per metric. Default parameters can be overriden by overriding the protected methods of this class.
* per metric. Default parameters can be overridden by overriding the protected methods of this class.
*
* @author Clément Fournier
*/
public abstract class AbstractApexMetricTestRule extends AbstractApexRule {
private final EnumeratedProperty<MetricVersion> versionDescriptor = new EnumeratedProperty<>(
"metricVersion", "Choose a variant of the metric or the standard",
versionMappings(), Version.STANDARD, MetricVersion.class, 3.0f);
private final EnumeratedMultiProperty<MetricOption> optionsDescriptor = new EnumeratedMultiProperty<>(
"metricOptions", "Choose a variant of the metric or the standard",
optionMappings(), Collections.emptyList(), MetricOption.class, 3.0f);
private final BooleanProperty reportClassesDescriptor = new BooleanProperty(
"reportClasses", "Add class violations to the report", isReportClasses(), 2.0f);
private final BooleanProperty reportMethodsDescriptor = new BooleanProperty(
@ -38,7 +39,7 @@ public abstract class AbstractApexMetricTestRule extends AbstractApexRule {
private final DoubleProperty reportLevelDescriptor = new DoubleProperty(
"reportLevel", "Minimum value required to report", -1., Double.POSITIVE_INFINITY, defaultReportLevel(), 3.0f);
private MetricVersion metricVersion;
private MetricOptions metricOptions;
private boolean reportClasses;
private boolean reportMethods;
private double reportLevel;
@ -53,7 +54,7 @@ public abstract class AbstractApexMetricTestRule extends AbstractApexRule {
definePropertyDescriptor(reportClassesDescriptor);
definePropertyDescriptor(reportMethodsDescriptor);
definePropertyDescriptor(reportLevelDescriptor);
definePropertyDescriptor(versionDescriptor);
definePropertyDescriptor(optionsDescriptor);
}
@ -98,10 +99,8 @@ public abstract class AbstractApexMetricTestRule extends AbstractApexRule {
*
* @return A map of labels to versions
*/
protected Map<String, MetricVersion> versionMappings() {
Map<String, MetricVersion> mappings = new HashMap<>();
mappings.put("standard", Version.STANDARD);
return mappings;
protected Map<String, MetricOption> optionMappings() {
return new HashMap<>();
}
@ -120,16 +119,17 @@ public abstract class AbstractApexMetricTestRule extends AbstractApexRule {
reportClasses = getProperty(reportClassesDescriptor);
reportMethods = getProperty(reportMethodsDescriptor);
reportLevel = getProperty(reportLevelDescriptor);
metricVersion = getProperty(versionDescriptor);
if (metricOptions == null) {
metricOptions = MetricOptions.ofOptions(getProperty(optionsDescriptor));
}
if (classKey != null && reportClasses && classKey.supports(node)) {
int classValue = (int) ApexMetrics.get(classKey, node, metricVersion);
int classValue = (int) ApexMetrics.get(classKey, node, metricOptions);
String valueReport = String.valueOf(classValue);
if (opKey != null) {
int highest = (int) ApexMetrics.get(opKey, node, metricVersion, ResultOption.HIGHEST);
int highest = (int) ApexMetrics.get(opKey, node, metricOptions, ResultOption.HIGHEST);
valueReport += " highest " + highest;
}
if (classValue >= reportLevel) {
@ -143,7 +143,7 @@ public abstract class AbstractApexMetricTestRule extends AbstractApexRule {
@Override
public Object visit(ASTMethod node, Object data) {
if (opKey != null && reportMethods && opKey.supports(node)) {
int methodValue = (int) ApexMetrics.get(opKey, node, metricVersion);
int methodValue = (int) ApexMetrics.get(opKey, node, metricOptions);
if (methodValue >= reportLevel) {
addViolation(data, node, new String[] {node.getQualifiedName().toString(), "" + methodValue});
}