Add old property back and log warning + correct the property range
This commit is contained in:
@ -4,14 +4,18 @@
|
|||||||
|
|
||||||
package net.sourceforge.pmd.lang.java.rule.design;
|
package net.sourceforge.pmd.lang.java.rule.design;
|
||||||
|
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
import net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
import net.sourceforge.pmd.lang.java.ast.ASTMethodOrConstructorDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.metrics.JavaMetrics;
|
import net.sourceforge.pmd.lang.java.metrics.JavaMetrics;
|
||||||
import net.sourceforge.pmd.lang.java.metrics.api.JavaOperationMetricKey;
|
import net.sourceforge.pmd.lang.java.metrics.api.JavaOperationMetricKey;
|
||||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaMetricsRule;
|
import net.sourceforge.pmd.lang.java.rule.AbstractJavaMetricsRule;
|
||||||
|
import net.sourceforge.pmd.properties.DoubleProperty;
|
||||||
import net.sourceforge.pmd.properties.IntegerProperty;
|
import net.sourceforge.pmd.properties.IntegerProperty;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Simple n-path complexity rule.
|
* Simple n-path complexity rule.
|
||||||
*
|
*
|
||||||
@ -20,33 +24,53 @@ import net.sourceforge.pmd.properties.IntegerProperty;
|
|||||||
*/
|
*/
|
||||||
public class NPathComplexityRule extends AbstractJavaMetricsRule {
|
public class NPathComplexityRule extends AbstractJavaMetricsRule {
|
||||||
|
|
||||||
private static final IntegerProperty REPORT_LEVEL_DESCRIPTOR = new IntegerProperty(
|
private static final Logger LOG = Logger.getLogger(NPathComplexityRule.class.getName());
|
||||||
"reportLevel", "N-Path Complexity reporting threshold", 1, 30, 200, 1.0f);
|
|
||||||
|
private static final DoubleProperty MINIMUM_DESCRIPTOR
|
||||||
|
= DoubleProperty.named("minimum").desc("Deprecated! Minimum reporting threshold")
|
||||||
|
.range(0d, 2000d).defaultValue(200d).uiOrder(2.0f).build();
|
||||||
|
|
||||||
|
|
||||||
private static int reportLevel = 200;
|
private static final IntegerProperty REPORT_LEVEL_DESCRIPTOR
|
||||||
|
= IntegerProperty.named("reportLevel").desc("N-Path Complexity reporting threshold")
|
||||||
|
.range(1, 2000).defaultValue(200).uiOrder(1.0f).build();
|
||||||
|
|
||||||
|
|
||||||
|
private int reportLevel = 200;
|
||||||
|
|
||||||
|
|
||||||
public NPathComplexityRule() {
|
public NPathComplexityRule() {
|
||||||
definePropertyDescriptor(REPORT_LEVEL_DESCRIPTOR);
|
definePropertyDescriptor(REPORT_LEVEL_DESCRIPTOR);
|
||||||
|
definePropertyDescriptor(MINIMUM_DESCRIPTOR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visit(ASTCompilationUnit node, Object data) {
|
public Object visit(ASTCompilationUnit node, Object data) {
|
||||||
reportLevel = getProperty(REPORT_LEVEL_DESCRIPTOR);
|
reportLevel = getReportLevel();
|
||||||
|
|
||||||
super.visit(node, data);
|
super.visit(node, data);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private int getReportLevel() {
|
||||||
|
double oldProp = getProperty(MINIMUM_DESCRIPTOR);
|
||||||
|
if (oldProp != 200.0) {
|
||||||
|
LOG.warning("Rule NPathComplexity uses deprecated property 'minimum'. Future versions of PMD will remove support for this property. Please use 'reportLevel' instead!");
|
||||||
|
return (int) oldProp;
|
||||||
|
}
|
||||||
|
|
||||||
|
return getProperty(REPORT_LEVEL_DESCRIPTOR);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final Object visit(ASTMethodOrConstructorDeclaration node, Object data) {
|
public final Object visit(ASTMethodOrConstructorDeclaration node, Object data) {
|
||||||
int npath = (int) JavaMetrics.get(JavaOperationMetricKey.NPATH, node);
|
int npath = (int) JavaMetrics.get(JavaOperationMetricKey.NPATH, node);
|
||||||
if (npath >= reportLevel) {
|
if (npath >= reportLevel) {
|
||||||
addViolation(data, node, new String[] {node instanceof ASTMethodDeclaration ? "method" : "constructor",
|
addViolation(data, node, new String[]{node instanceof ASTMethodDeclaration ? "method" : "constructor",
|
||||||
node.getQualifiedName().getOperation(), "" + npath, });
|
node.getQualifiedName().getOperation(), "" + npath, });
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
@ -196,4 +196,26 @@ class Bar {
|
|||||||
}
|
}
|
||||||
]]></code>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
|
||||||
|
<test-code>
|
||||||
|
<description>Backwards compatibility with minimum property</description>
|
||||||
|
<rule-property name="minimum">300</rule-property>
|
||||||
|
<expected-problems>0</expected-problems>
|
||||||
|
<code><![CDATA[
|
||||||
|
class Foo {
|
||||||
|
Foo() {
|
||||||
|
boolean a, b;
|
||||||
|
int j = 23;
|
||||||
|
switch(j) {
|
||||||
|
case 1:
|
||||||
|
case 2: break;
|
||||||
|
case 3: j = 5; break;
|
||||||
|
case 4: if (b && a) { bar(); } break;
|
||||||
|
default: break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]]></code>
|
||||||
|
</test-code>
|
||||||
|
|
||||||
</test-data>
|
</test-data>
|
||||||
|
Reference in New Issue
Block a user