Next step towards real world rules

This commit is contained in:
Robert Sösemann
2016-03-23 16:20:08 +01:00
parent 9c6c3af8cc
commit ebab8d0edb
4 changed files with 99 additions and 18 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -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<ASTField> 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);

View File

@ -8,12 +8,35 @@
The Code Size ruleset contains rules that find problems related to code size or complexity.
</description>
<rule name="ExcessiveMethodLength"
since="0.6"
message="Avoid really long methods."
class="net.sourceforge.pmd.lang.apex.rule.codesize.ExcessiveMethodLengthRule"
externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#ExcessiveMethodLength">
<description>
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.
</description>
<priority>3</priority>
<example>
<![CDATA[
public void doSomething() {
System.out.println("Hello world!");
System.out.println("Hello world!");
// 98 copies omitted for brevity.
}
]]>
</example>
</rule>
<rule name="ExcessiveParameterList" since="0.9"
message="Avoid long parameter lists."
class="net.sourceforge.pmd.lang.java.rule.codesize.ExcessiveParameterListRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#ExcessiveParameterList">
class="net.sourceforge.pmd.lang.apex.rule.codesize.ExcessiveParameterListRule"
externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#ExcessiveParameterList">
<description>
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
<rule name="ExcessiveClassLength" since="0.6"
message="Avoid really long classes."
class="net.sourceforge.pmd.lang.java.rule.codesize.ExcessiveClassLengthRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#ExcessiveClassLength">
class="net.sourceforge.pmd.lang.apex.rule.codesize.ExcessiveClassLengthRule"
externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#ExcessiveClassLength">
<description>
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 {
<rule name="ExcessivePublicCount" since="1.04"
message="This class has a bunch of public methods and attributes"
class="net.sourceforge.pmd.lang.java.rule.codesize.ExcessivePublicCountRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#ExcessivePublicCount">
class="net.sourceforge.pmd.lang.apex.rule.codesize.ExcessivePublicCountRule"
externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#ExcessivePublicCount">
<description>
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 {
</rule>
<rule name="TooManyFields" since="3.0" message="Too many fields"
class="net.sourceforge.pmd.lang.java.rule.codesize.TooManyFieldsRule"
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#TooManyFields">
class="net.sourceforge.pmd.lang.apex.rule.codesize.TooManyFieldsRule"
externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#TooManyFields">
<description>
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
</example>
</rule>
<rule name="TooManyMethods" language="java" since="4.2"
<rule name="TooManyMethods" language="apex" since="4.2"
class="net.sourceforge.pmd.lang.rule.XPathRule" message="This class has too many methods, consider refactoring it."
externalInfoUrl="${pmd.website.baseurl}/rules/java/codesize.html#TooManyMethods">
externalInfoUrl="${pmd.website.baseurl}/rules/apex/codesize.html#TooManyMethods">
<description>
<![CDATA[
A class with too many methods is probably a good suspect for refactoring, in order to reduce its complexity and find a way to