diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/TooManyFieldsRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/TooManyFieldsRule.java index 838c78be41..b8ff3377d7 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/TooManyFieldsRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/TooManyFieldsRule.java @@ -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 stats; - private Map nodes; - private static final PropertyDescriptor 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 fields = node.findDescendantsOfType(ASTField.class); + List 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 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); - } } diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/xml/TooManyFields.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/xml/TooManyFields.xml index d26b4d8ff0..6d5bf0e68a 100644 --- a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/xml/TooManyFields.xml +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/xml/TooManyFields.xml @@ -77,6 +77,7 @@ public class Foo { 2 inner classes, each with > 10 fields 2 + 2,20