From 03a5b7549e8eae13d430a41d0f763aa9c7dc637d Mon Sep 17 00:00:00 2001 From: Andreas Dangel Date: Sat, 24 Jun 2023 10:52:08 +0200 Subject: [PATCH] [apex] ExcessivePublicCount: consider properties Fixes #4596 --- docs/pages/release_notes.md | 2 ++ .../rule/design/ExcessivePublicCountRule.java | 24 ++++++++------- .../main/resources/category/apex/design.xml | 9 ++++-- .../rule/design/xml/ExcessivePublicCount.xml | 30 +++++++++++++++++-- 4 files changed, 49 insertions(+), 16 deletions(-) diff --git a/docs/pages/release_notes.md b/docs/pages/release_notes.md index 3e25409b699..f938e4cdac7 100644 --- a/docs/pages/release_notes.md +++ b/docs/pages/release_notes.md @@ -43,6 +43,8 @@ The remaining section describes the complete release notes for 7.0.0. * miscellaneous * [#4582](https://github.com/pmd/pmd/issues/4582): \[dist] Download link broken +* apex-design + * [#4596](https://github.com/pmd/pmd/issues/4596): \[apex] ExcessivePublicCount ignores properties * java * [#4401](https://github.com/pmd/pmd/issues/4401): \[java] PMD 7 fails to build under Java 19 diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/ExcessivePublicCountRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/ExcessivePublicCountRule.java index b833424ced8..2507c6a6fbf 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/ExcessivePublicCountRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/design/ExcessivePublicCountRule.java @@ -6,6 +6,7 @@ import net.sourceforge.pmd.lang.apex.ast.ASTFieldDeclarationStatements; import net.sourceforge.pmd.lang.apex.ast.ASTMethod; +import net.sourceforge.pmd.lang.apex.ast.ASTProperty; import net.sourceforge.pmd.lang.apex.ast.ASTUserClass; import net.sourceforge.pmd.lang.apex.rule.internal.AbstractCounterCheckRule; @@ -36,20 +37,21 @@ protected int defaultReportLevel() { @Override protected int getMetric(ASTUserClass node) { - // node streams would be useful here int publicMethods = - (int) node.findChildrenOfType(ASTMethod.class) - .stream() - .filter(it -> it.getModifiers().isPublic() && !it.isSynthetic()) - .count(); - + node.children(ASTMethod.class) + .filter(it -> it.getModifiers().isPublic() && !it.isSynthetic()) + .count(); int publicFields = - (int) node.findChildrenOfType(ASTFieldDeclarationStatements.class) - .stream() - .filter(it -> it.getModifiers().isPublic() && !it.getModifiers().isStatic()) - .count(); + node.children(ASTFieldDeclarationStatements.class) + .filter(it -> it.getModifiers().isPublic() && !it.getModifiers().isStatic()) + .count(); + + int publicProperties = + node.children(ASTProperty.class) + .filter(it -> it.getModifiers().isPublic() && !it.getModifiers().isStatic()) + .count(); - return publicFields + publicMethods; + return publicFields + publicMethods + publicProperties; } @Override diff --git a/pmd-apex/src/main/resources/category/apex/design.xml b/pmd-apex/src/main/resources/category/apex/design.xml index 808ceb08b71..f0154c1c12e 100644 --- a/pmd-apex/src/main/resources/category/apex/design.xml +++ b/pmd-apex/src/main/resources/category/apex/design.xml @@ -281,12 +281,12 @@ public void addPerson(Date birthdate, BodyMeasurements measurements, int ssn) { -Classes with large numbers of public methods and attributes require disproportionate testing efforts -since combinational side effects grow rapidly and increase risk. Refactoring these classes into +Classes with large numbers of public methods, attributes, and properties require disproportionate testing efforts +since combinatorial side effects grow rapidly and increase risk. Refactoring these classes into smaller ones not only increases testability and reliability but also allows new variations to be developed easily. @@ -303,6 +303,9 @@ public class Foo { public void doMoreWork() {} public void doWorkAgain() {} // [... more more public methods ...] + + public String property1 { get; set; } + // [... more more public properties ...] } ]]> diff --git a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/xml/ExcessivePublicCount.xml b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/xml/ExcessivePublicCount.xml index e529fac558c..7d397a27384 100644 --- a/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/xml/ExcessivePublicCount.xml +++ b/pmd-apex/src/test/resources/net/sourceforge/pmd/lang/apex/rule/design/xml/ExcessivePublicCount.xml @@ -120,8 +120,8 @@ public class SomeClass { 2 1,5 - The class OuterClass has 2 public methods and attributes (limit: 1) - The class InnerClass has 4 public methods and attributes (limit: 1) + The class OuterClass has 2 public methods, attributes, and properties (limit: 1) + The class InnerClass has 4 public methods, attributes, and properties (limit: 1) + + + + [apex] ExcessivePublicCount ignores properties #4596 + 4 + 1 + 1 + + The class Example has 4 public methods, attributes, and properties (limit: 4) + +