Merge branch 'pr/3730' into 7.0.x
This commit is contained in:
@ -155,8 +155,8 @@
|
||||
<rule ref="category/java/design.xml/SimplifyConditional"/>
|
||||
<rule ref="category/java/design.xml/SingularField"/>
|
||||
<rule ref="category/java/design.xml/SwitchDensity"/>
|
||||
<!-- <rule ref="category/java/design.xml/TooManyFields"/> -->
|
||||
<!-- <rule ref="category/java/design.xml/TooManyMethods"/> -->
|
||||
<rule ref="category/java/design.xml/TooManyFields"/>
|
||||
<rule ref="category/java/design.xml/TooManyMethods"/>
|
||||
<rule ref="category/java/design.xml/UseObjectForClearerAPI"/>
|
||||
<rule ref="category/java/design.xml/UseUtilityClass"/>
|
||||
<rule ref="category/java/design.xml/UselessOverridingMethod"/>
|
||||
|
@ -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<Integer> 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<ASTFieldDeclaration> l = node.findDescendantsOfType(ASTFieldDeclaration.class);
|
||||
|
||||
for (ASTFieldDeclaration fd : l) {
|
||||
if (fd.isFinal() && fd.isStatic()) {
|
||||
continue;
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
|
||||
if (counter > maxFields) {
|
||||
addViolation(data, node);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
}
|
@ -1296,7 +1296,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">
|
||||
<description>
|
||||
Classes that have too many fields can become unwieldy and could be redesigned to have fewer fields,
|
||||
@ -1304,6 +1304,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.
|
||||
</description>
|
||||
<priority>3</priority>
|
||||
<properties>
|
||||
<property name="maxfields" type="Integer" description="Max allowable fields" min="1" max="1000" value="15"/>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<![CDATA[
|
||||
//ClassOrInterfaceDeclaration/ClassOrInterfaceBody
|
||||
[count(FieldDeclaration
|
||||
[not(pmd-java:modifiers() = 'final')]
|
||||
[not(pmd-java:modifiers() = 'static')]
|
||||
) > $maxfields]
|
||||
]]>
|
||||
</value>
|
||||
</property>
|
||||
</properties>
|
||||
<example>
|
||||
<![CDATA[
|
||||
public class Person { // too many separate fields
|
||||
@ -1337,30 +1351,10 @@ complexity and find a way to have more fine grained objects.
|
||||
<property name="maxmethods" type="Integer" description="The method count reporting threshold" min="1" max="1000" value="10"/>
|
||||
<property name="xpath">
|
||||
<value>
|
||||
<!-- FIXME: Refine XPath to discard 'get' and 'set' methods with Block no more than 3 lines,
|
||||
something like this:
|
||||
not (
|
||||
(
|
||||
starts-with(@Name,'get')
|
||||
or
|
||||
starts-with(@Name,'set')
|
||||
or
|
||||
starts-with(@Name,'is')
|
||||
)
|
||||
and (
|
||||
(
|
||||
(../Block/attribute::endLine)
|
||||
-
|
||||
(../Block/attribute::beginLine)
|
||||
) <= 3
|
||||
)
|
||||
)
|
||||
This will avoid discarding 'real' methods...
|
||||
-->
|
||||
<![CDATA[
|
||||
//ClassOrInterfaceDeclaration/ClassOrInterfaceBody
|
||||
[
|
||||
count(./ClassOrInterfaceBodyDeclaration/MethodDeclaration[
|
||||
count(MethodDeclaration[
|
||||
not (
|
||||
starts-with(@Name,'get')
|
||||
or
|
||||
|
@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.design;
|
||||
|
||||
import net.sourceforge.pmd.testframework.PmdRuleTst;
|
||||
|
||||
@org.junit.Ignore("Rule has not been updated yet")
|
||||
public class TooManyFieldsTest extends PmdRuleTst {
|
||||
// no additional unit tests
|
||||
}
|
||||
|
@ -6,7 +6,6 @@ package net.sourceforge.pmd.lang.java.rule.design;
|
||||
|
||||
import net.sourceforge.pmd.testframework.PmdRuleTst;
|
||||
|
||||
@org.junit.Ignore("Rule has not been updated yet")
|
||||
public class TooManyMethodsTest extends PmdRuleTst {
|
||||
// no additional unit tests
|
||||
}
|
||||
|
@ -22,12 +22,14 @@ public class Foo {
|
||||
<description>3 fields, reduced max to 2</description>
|
||||
<rule-property name="maxfields">2</rule-property>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>1</expected-linenumbers>
|
||||
<code-ref id="id-3-fields"/>
|
||||
</test-code>
|
||||
|
||||
<test-code>
|
||||
<description>16 fields, bad</description>
|
||||
<expected-problems>1</expected-problems>
|
||||
<expected-linenumbers>1</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
int a1;
|
||||
@ -116,6 +118,7 @@ public interface Foo {
|
||||
<test-code>
|
||||
<description>2 inner classes, each with > 10 fields</description>
|
||||
<expected-problems>2</expected-problems>
|
||||
<expected-linenumbers>2,20</expected-linenumbers>
|
||||
<code><![CDATA[
|
||||
public class Foo {
|
||||
public class Bar1 {
|
||||
|
Reference in New Issue
Block a user