diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 8b159ee9e6..f50c70ca63 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,5 +1,5 @@ ?????? - 1.04 -Added new rules: ConstructorCallsOverridableMethodRule, AtLeastOneConstructorRule, JUnitAssertionsShouldIncludeMessageRule, DoubleCheckedLockingRule +Added new rules: ConstructorCallsOverridableMethodRule, AtLeastOneConstructorRule, JUnitAssertionsShouldIncludeMessageRule, DoubleCheckedLockingRule, ExcessivePublicCountRule The Ant task has been updated; if you set "verbose=true" full stacktraces are printed. Thx to Paul Roebuck for the suggestion. February 11, 2003 - 1.03 diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/ExcessivePublicCountRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/ExcessivePublicCountRuleTest.java new file mode 100644 index 0000000000..b4f49a0442 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/ExcessivePublicCountRuleTest.java @@ -0,0 +1,22 @@ +package test.net.sourceforge.pmd.rules; + +import net.sourceforge.pmd.rules.ExcessivePublicCountRule; + +public class ExcessivePublicCountRuleTest extends RuleTst { + + private ExcessivePublicCountRule rule; + + public ExcessivePublicCountRuleTest() { + rule = new ExcessivePublicCountRule(); + } + + public void testSimpleOK() throws Throwable { + rule.addProperty("minimum", "50"); + super.runTest("ExcessivePublicCountRule1.java", 0, rule); + } + + public void testSimpleBad() throws Throwable { + rule.addProperty("minimum", "2"); + super.runTest("ExcessivePublicCountRule2.java", 1, rule); + } +} diff --git a/pmd/rulesets/newrules.xml b/pmd/rulesets/newrules.xml index 4052facf57..67aa15f27d 100644 --- a/pmd/rulesets/newrules.xml +++ b/pmd/rulesets/newrules.xml @@ -189,6 +189,38 @@ These are new rules for the next release + + + A large amount of public methods and attributes declared in an object can indicate the class may need + to be broken up as increased effort will be required to thoroughly test such a class. + + 3 + + + + + + + diff --git a/pmd/src/net/sourceforge/pmd/rules/ExcessivePublicCountRule.java b/pmd/src/net/sourceforge/pmd/rules/ExcessivePublicCountRule.java new file mode 100644 index 0000000000..dc70f77b51 --- /dev/null +++ b/pmd/src/net/sourceforge/pmd/rules/ExcessivePublicCountRule.java @@ -0,0 +1,52 @@ +package net.sourceforge.pmd.rules; + +import net.sourceforge.pmd.ast.ASTCompilationUnit; +import net.sourceforge.pmd.ast.ASTFieldDeclaration; +import net.sourceforge.pmd.ast.ASTMethodDeclarator; +import net.sourceforge.pmd.ast.AccessNode; +import net.sourceforge.pmd.rules.design.ExcessiveNodeCountRule; + +/** + * @author aglover + * + * Class Name: ExcessivePublicCountRule + * + * Rule attempts to count all public methods and public attributes defined in a class. + * + * If a class has a high number of public operations, it might be wise to consider whether + * it would be appropriate to divide it into subclasses. + * + * A large proportion of public members and operations means the class has high potential to be + * affected by external classes. Futhermore, increased effort will be required to + * thoroughly test the class. + */ +public class ExcessivePublicCountRule extends ExcessiveNodeCountRule { + + public ExcessivePublicCountRule() { + super(ASTCompilationUnit.class); + } + + /** + * Method counts ONLY public methods. + */ + public Object visit(ASTMethodDeclarator node, Object data) { + return this.getTallyOnAccessType((AccessNode)node.jjtGetParent()); + } + /** + * Method counts ONLY public class attributes + */ + public Object visit(ASTFieldDeclaration node, Object data) { + return this.getTallyOnAccessType(node); + } + /** + * Method counts a node if it is public + * @param AccessNode node + * @return Integer 1 if node is public 0 otherwise + */ + private Integer getTallyOnAccessType(AccessNode node){ + if(node.isPublic()){ + return new Integer(1); + } + return new Integer(0); + } +} diff --git a/pmd/test-data/ExcessivePublicCountRule1.java b/pmd/test-data/ExcessivePublicCountRule1.java new file mode 100644 index 0000000000..1875dd5da3 --- /dev/null +++ b/pmd/test-data/ExcessivePublicCountRule1.java @@ -0,0 +1,3 @@ +public class ExcessivePublicCountRule1 { + public int foo; +} \ No newline at end of file diff --git a/pmd/test-data/ExcessivePublicCountRule2.java b/pmd/test-data/ExcessivePublicCountRule2.java new file mode 100644 index 0000000000..d17a1c116c --- /dev/null +++ b/pmd/test-data/ExcessivePublicCountRule2.java @@ -0,0 +1,6 @@ +public class ExcessivePublicCountRule2 { + public int foo; + public int bif; + public int baz; + public int bof; +} \ No newline at end of file diff --git a/pmd/xdocs/credits.xml b/pmd/xdocs/credits.xml index 5c451ccb2c..f481996f10 100644 --- a/pmd/xdocs/credits.xml +++ b/pmd/xdocs/credits.xml @@ -10,7 +10,8 @@