Port SwitchDensity

This commit is contained in:
Clément Fournier
2019-04-02 07:23:33 +02:00
parent 21a764a916
commit 2a2337c9da
2 changed files with 8 additions and 60 deletions

View File

@ -4,16 +4,11 @@
package net.sourceforge.pmd.lang.java.rule.design;
import static net.sourceforge.pmd.properties.constraints.NumericConstraints.positive;
import net.sourceforge.pmd.lang.java.ast.ASTExpression;
import net.sourceforge.pmd.lang.java.ast.ASTStatement;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchLabel;
import net.sourceforge.pmd.lang.java.ast.ASTSwitchStatement;
import net.sourceforge.pmd.lang.java.ast.JavaParserVisitorAdapter;
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
/**
* Switch Density - This is the number of statements over the number of
@ -25,28 +20,20 @@ import net.sourceforge.pmd.properties.PropertyFactory;
*
* @author David Dixon-Peugh
*/
public class SwitchDensityRule extends AbstractJavaRulechainRule {
private static final PropertyDescriptor<Double> REPORT_LEVEL =
// can't use CommonPropertyDescriptors because we need a double property
PropertyFactory.doubleProperty("minimum")
.desc("Threshold above which a node is reported")
.require(positive())
.defaultValue(10d)
.build();
public class SwitchDensityRule extends AbstractCounterCheckRule<ASTSwitchStatement> {
public SwitchDensityRule() {
super(ASTSwitchStatement.class);
definePropertyDescriptor(REPORT_LEVEL);
}
@Override
public Object visit(ASTSwitchStatement node, Object data) {
double density = new SwitchDensityVisitor().compute(node);
if (density >= getProperty(REPORT_LEVEL)) {
addViolation(data, node);
}
return super.visit(node, data);
protected int defaultReportLevel() {
return 10;
}
@Override
protected boolean isViolation(ASTSwitchStatement node, int reportLevel) {
return new SwitchDensityVisitor().compute(node) >= reportLevel;
}
private static class SwitchDensityVisitor extends JavaParserVisitorAdapter {

View File

@ -1,39 +0,0 @@
/**
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd.lang.vm.rule;
import java.util.List;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.rule.stat.StatisticalRule;
import net.sourceforge.pmd.lang.rule.stat.StatisticalRuleHelper;
import net.sourceforge.pmd.stat.DataPoint;
/**
* @deprecated see {@link StatisticalRule}
*/
@Deprecated
public abstract class AbstractStatisticalVmRule extends AbstractVmRule implements StatisticalRule {
private final StatisticalRuleHelper helper = new StatisticalRuleHelper(this);
@Override
public void addDataPoint(final DataPoint point) {
helper.addDataPoint(point);
}
@Override
public Object[] getViolationParameters(final DataPoint point) {
return new Object[0];
}
@Override
public void apply(final List<? extends Node> nodes, final RuleContext ctx) {
super.apply(nodes, ctx);
helper.apply(ctx);
}
}