From 3627167efac8e7732ccb31b4abd82717cdb0d2a6 Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Fri, 14 Jan 2022 09:13:37 +0100 Subject: [PATCH] [java] Update rules TooManyMethods and TooManyFields TooManyRules is now a XPath rule as well --- .ci/files/all-java.xml | 4 +- .../java/rule/design/TooManyFieldsRule.java | 54 ------------------- .../main/resources/category/java/design.xml | 38 ++++++------- .../java/rule/design/TooManyFieldsTest.java | 1 - .../java/rule/design/TooManyMethodsTest.java | 1 - .../java/rule/design/xml/TooManyFields.xml | 3 ++ 6 files changed, 21 insertions(+), 80 deletions(-) delete mode 100644 pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/TooManyFieldsRule.java diff --git a/.ci/files/all-java.xml b/.ci/files/all-java.xml index 040fb26ada..1fa50b951b 100644 --- a/.ci/files/all-java.xml +++ b/.ci/files/all-java.xml @@ -155,8 +155,8 @@ - - + + diff --git a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/TooManyFieldsRule.java b/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/TooManyFieldsRule.java deleted file mode 100644 index 10f1ccbbe1..0000000000 --- a/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/rule/design/TooManyFieldsRule.java +++ /dev/null @@ -1,54 +0,0 @@ -/** - * BSD-style license; for more info see http://pmd.sourceforge.net/license.html - */ - -package net.sourceforge.pmd.lang.java.rule.design; - -import static net.sourceforge.pmd.properties.constraints.NumericConstraints.positive; - -import java.util.List; - -import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration; -import net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration; -import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule; -import net.sourceforge.pmd.properties.PropertyDescriptor; -import net.sourceforge.pmd.properties.PropertyFactory; - - -public class TooManyFieldsRule extends AbstractJavaRule { - - private static final int DEFAULT_MAXFIELDS = 15; - - private static final PropertyDescriptor MAX_FIELDS_DESCRIPTOR - = PropertyFactory.intProperty("maxfields") - .desc("Max allowable fields") - .defaultValue(DEFAULT_MAXFIELDS) - .require(positive()) - .build(); - - public TooManyFieldsRule() { - definePropertyDescriptor(MAX_FIELDS_DESCRIPTOR); - addRuleChainVisit(ASTClassOrInterfaceDeclaration.class); - } - - @Override - public Object visit(ASTClassOrInterfaceDeclaration node, Object data) { - final int maxFields = getProperty(MAX_FIELDS_DESCRIPTOR); - int counter = 0; - - final List l = node.findDescendantsOfType(ASTFieldDeclaration.class); - - for (ASTFieldDeclaration fd : l) { - if (fd.isFinal() && fd.isStatic()) { - continue; - } - counter++; - } - - if (counter > maxFields) { - addViolation(data, node); - } - - return data; - } -} diff --git a/pmd-java/src/main/resources/category/java/design.xml b/pmd-java/src/main/resources/category/java/design.xml index ff4d2f590e..8b9cfb174d 100644 --- a/pmd-java/src/main/resources/category/java/design.xml +++ b/pmd-java/src/main/resources/category/java/design.xml @@ -1300,7 +1300,7 @@ public class Foo { language="java" since="3.0" message="Too many fields" - class="net.sourceforge.pmd.lang.java.rule.design.TooManyFieldsRule" + class="net.sourceforge.pmd.lang.rule.XPathRule" externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_design.html#toomanyfields"> Classes that have too many fields can become unwieldy and could be redesigned to have fewer fields, @@ -1308,6 +1308,20 @@ possibly through grouping related fields in new objects. For example, a class w city/state/zip fields could park them within a single Address field. 3 + + + + + $maxfields] +]]> + + + - 3 fields, reduced max to 2 2 1 + 1 16 fields, bad 1 + 1 2 inner classes, each with > 10 fields 2 + 2,20