Merge branch 'master' into 7.0.x

This commit is contained in:
Clément Fournier
2020-04-23 19:39:20 +02:00
2 changed files with 7 additions and 32 deletions

View File

@ -6,25 +6,18 @@ package net.sourceforge.pmd.lang.apex.rule.design;
import static net.sourceforge.pmd.properties.constraints.NumericConstraints.positive;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sourceforge.pmd.lang.apex.ast.ASTField;
import net.sourceforge.pmd.lang.apex.ast.ASTUserClass;
import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.properties.PropertyDescriptor;
import net.sourceforge.pmd.properties.PropertyFactory;
import net.sourceforge.pmd.util.NumericConstants;
public class TooManyFieldsRule extends AbstractApexRule {
private static final int DEFAULT_MAXFIELDS = 15;
private Map<String, Integer> stats;
private Map<String, ASTUserClass> nodes;
private static final PropertyDescriptor<Integer> MAX_FIELDS_DESCRIPTOR
= PropertyFactory.intProperty("maxfields")
.desc("Max allowable fields")
@ -35,44 +28,25 @@ public class TooManyFieldsRule extends AbstractApexRule {
public TooManyFieldsRule() {
definePropertyDescriptor(MAX_FIELDS_DESCRIPTOR);
addRuleChainVisit(ASTUserClass.class);
}
@Override
public Object visit(ASTUserClass node, Object data) {
int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR);
stats = new HashMap<>(5);
nodes = new HashMap<>(5);
List<ASTField> fields = node.findDescendantsOfType(ASTField.class);
List<ASTField> fields = node.findChildrenOfType(ASTField.class);
int val = 0;
for (ASTField field : fields) {
if (field.getModifiers().isFinal() && field.getModifiers().isStatic()) {
continue;
}
ASTUserClass clazz = field.getFirstParentOfType(ASTUserClass.class);
if (clazz != null) {
bumpCounterFor(clazz);
}
val++;
}
for (Map.Entry<String, Integer> entry : stats.entrySet()) {
int val = entry.getValue();
Node n = nodes.get(entry.getKey());
if (val > maxFields) {
addViolation(data, n);
}
if (val > getProperty(MAX_FIELDS_DESCRIPTOR)) {
addViolation(data, node);
}
return data;
}
private void bumpCounterFor(ASTUserClass clazz) {
String key = clazz.getImage();
if (!stats.containsKey(key)) {
stats.put(key, NumericConstants.ZERO);
nodes.put(key, clazz);
}
Integer i = stats.get(key) + 1;
stats.put(key, i);
}
}

View File

@ -77,6 +77,7 @@ public class Foo {
<test-code>
<description>2 inner classes, each with > 10 fields</description>
<expected-problems>2</expected-problems>
<expected-linenumbers>2,20</expected-linenumbers>
<code><![CDATA[
public class Foo {
public class Bar1 {