diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/AccessNode.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/AccessNode.java new file mode 100644 index 0000000000..4bffb204ec --- /dev/null +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/ast/AccessNode.java @@ -0,0 +1,52 @@ +/** + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html + */ +package net.sourceforge.pmd.lang.apex.ast; + +import net.sourceforge.pmd.lang.ast.Node; + +/** + * This interface captures Java access modifiers. + */ +public interface AccessNode extends Node { + + int PUBLIC = 0x0001; + int PROTECTED = 0x0002; + int PRIVATE = 0x0004; + int ABSTRACT = 0x0008; + int STATIC = 0x0010; + int FINAL = 0x0020; + int TRANSIENT = 0x0100; + + int getModifiers(); + + void setModifiers(int modifiers); + + boolean isPublic(); + + void setPublic(boolean isPublic); + + boolean isProtected(); + + void setProtected(boolean isProtected); + + boolean isPrivate(); + + void setPrivate(boolean isPrivate); + + boolean isAbstract(); + + void setAbstract(boolean isAbstract); + + boolean isStatic(); + + void setStatic(boolean isStatic); + + boolean isFinal(); + + void setFinal(boolean isFinal); + + void setDefault(boolean isDefault); + + boolean isDefault(); +} diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codesize/ExcessivePublicCountRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codesize/ExcessivePublicCountRule.java index f6db66d0c6..3601a58960 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codesize/ExcessivePublicCountRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codesize/ExcessivePublicCountRule.java @@ -3,9 +3,13 @@ */ package net.sourceforge.pmd.lang.apex.rule.codesize; +import static apex.jorje.semantic.symbol.type.ModifierTypeInfos.PUBLIC; +import static apex.jorje.semantic.symbol.type.ModifierTypeInfos.STATIC; + import net.sourceforge.pmd.lang.apex.ast.ASTCompilation; import net.sourceforge.pmd.lang.apex.ast.ASTField; import net.sourceforge.pmd.lang.apex.ast.ASTMethod; +import net.sourceforge.pmd.lang.apex.ast.AccessNode; import net.sourceforge.pmd.lang.apex.rule.design.ExcessiveNodeCountRule; import net.sourceforge.pmd.util.NumericConstants; @@ -34,7 +38,7 @@ public class ExcessivePublicCountRule extends ExcessiveNodeCountRule { * Method counts ONLY public methods. */ public Object visit(ASTMethod node, Object data) { - return this.getTallyOnAccessType(node.jjtGetParent()); + return this.getTallyOnAccessType((AccessNode) node.jjtGetParent()); } /** @@ -42,10 +46,10 @@ public class ExcessivePublicCountRule extends ExcessiveNodeCountRule { * static- these usually represent constants.... */ public Object visit(ASTField node, Object data) { - if (node.isFinal() && node.isStatic()) { + if (node.getNode().getModifierInfo().all(PUBLIC, STATIC)) { return NumericConstants.ZERO; } - return this.getTallyOnAccessType(node); + return this.getTallyOnAccessType((AccessNode) node); } /** @@ -54,8 +58,8 @@ public class ExcessivePublicCountRule extends ExcessiveNodeCountRule { * @param node The access node. * @return Integer 1 if node is public 0 otherwise */ - private Integer getTallyOnAccessType(ASTCompilation node) { - if (node.isPublic()) { + private Integer getTallyOnAccessType(AccessNode node) { + if(node.isPublic()) { return NumericConstants.ONE; } return NumericConstants.ZERO; diff --git a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codesize/TooManyFieldsRule.java b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codesize/TooManyFieldsRule.java index 2c2e85d542..bc60fe4021 100644 --- a/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codesize/TooManyFieldsRule.java +++ b/pmd-apex/src/main/java/net/sourceforge/pmd/lang/apex/rule/codesize/TooManyFieldsRule.java @@ -3,6 +3,9 @@ */ package net.sourceforge.pmd.lang.apex.rule.codesize; +import static apex.jorje.semantic.symbol.type.ModifierTypeInfos.FINAL; +import static apex.jorje.semantic.symbol.type.ModifierTypeInfos.STATIC; + import java.util.HashMap; import java.util.List; import java.util.Map; @@ -11,7 +14,6 @@ import apex.jorje.semantic.ast.compilation.UserInterface; import net.sourceforge.pmd.lang.apex.ast.ASTCompilation; import net.sourceforge.pmd.lang.apex.ast.ASTField; import net.sourceforge.pmd.lang.apex.ast.ASTUserClass; -import net.sourceforge.pmd.lang.apex.ast.ASTUserInterface; import net.sourceforge.pmd.lang.apex.rule.AbstractApexRule; import net.sourceforge.pmd.lang.ast.Node; import net.sourceforge.pmd.lang.rule.properties.IntegerProperty; @@ -42,7 +44,7 @@ public class TooManyFieldsRule extends AbstractApexRule { List l = node.findDescendantsOfType(ASTField.class); for (ASTField fd: l) { - if (fd.isFinal() && fd.isStatic()) { + if (fd.getNode().getModifierInfo().all(FINAL, STATIC)) { continue; } ASTCompilation clazz = fd.getFirstParentOfType(ASTCompilation.class); diff --git a/pmd-apex/src/main/resources/rulesets/apex/codesize.xml b/pmd-apex/src/main/resources/rulesets/apex/codesize.xml index 56a8625d2b..7e5d55820e 100644 --- a/pmd-apex/src/main/resources/rulesets/apex/codesize.xml +++ b/pmd-apex/src/main/resources/rulesets/apex/codesize.xml @@ -8,12 +8,35 @@ The Code Size ruleset contains rules that find problems related to code size or complexity. - + + + When methods are excessively long this usually indicates that the method is doing more than its + name/signature might suggest. They also become challenging for others to digest since excessive + scrolling causes readers to lose focus. + Try to reduce the method length by creating helper methods and removing any copy/pasted code. + + 3 + + + + + + class="net.sourceforge.pmd.lang.apex.rule.codesize.ExcessiveParameterListRule" + externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#ExcessiveParameterList"> Methods with numerous parameters are a challenge to maintain, especially if most of them share the same datatype. These situations usually denote the need for new objects to wrap the numerous parameters. @@ -40,8 +63,8 @@ public void addPerson( // preferred approach + class="net.sourceforge.pmd.lang.apex.rule.codesize.ExcessiveClassLengthRule" + externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#ExcessiveClassLength"> Excessive class file lengths are usually indications that the class may be burdened with excessive responsibilities that could be provided by external classes or functions. In breaking these methods @@ -72,8 +95,8 @@ public class Foo { + class="net.sourceforge.pmd.lang.apex.rule.codesize.ExcessivePublicCountRule" + externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#ExcessivePublicCount"> 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 @@ -99,8 +122,8 @@ public class Foo { + class="net.sourceforge.pmd.lang.apex.rule.codesize.TooManyFieldsRule" + externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#TooManyFields"> Classes that have too many fields can become unwieldy and could be redesigned to have fewer fields, possibly through grouping related fields in new objects. For example, a class with individual @@ -125,9 +148,9 @@ public class Person { // this is more manageable - + externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#TooManyMethods">