Added new FinalFieldCouldBeStatic rule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1874 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-04-29 15:40:59 +00:00
parent 9f81b4e75e
commit 889cac77b9
3 changed files with 82 additions and 1 deletions

View File

@ -1,5 +1,5 @@
????? - 1.06:
Added new rule: FinalizeShouldBeProtected
Added new rules: FinalizeShouldBeProtected, FinalFieldCouldBeStatic
Removed "verbose" attribute from PMD and CPD Ant tasks; now they use built in logging so you can do a "ant -verbose cpd" or "ant -verbose pmd". Thanks to Philippe T'Seyen for the code.
Added "excludes" feature to rulesets; thanks to Gael Marziou for the suggestion.
TODO - fix it so tests and rules don't duplicate the xpath expressions

View File

@ -0,0 +1,51 @@
package test.net.sourceforge.pmd.rules;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.cpd.CPD;
import net.sourceforge.pmd.rules.XPathRule;
public class FinalFieldCouldBeStaticRuleTest extends SimpleAggregatorTst {
private Rule rule;
public void setUp() {
rule = new XPathRule();
rule.addProperty("xpath", "//FieldDeclaration[@Final='true' and @Static='false']/VariableDeclarator/VariableInitializer/Expression/ConditionalAndExpression/InstanceOfExpression/PrimaryExpression/PrimaryPrefix/Literal");
}
public void testAll() {
runTests(new TestDescriptor[] {
new TestDescriptor(TEST1, "simple failure case", 1, rule),
new TestDescriptor(TEST2, "already static, OK", 0, rule),
new TestDescriptor(TEST3, "non-final, OK", 0, rule),
new TestDescriptor(TEST4, "non-primitive failure case - only works for String", 1, rule),
new TestDescriptor(TEST5, "final field that's a thread, OK", 0, rule)
});
}
private static final String TEST1 =
"public class Foo {" + CPD.EOL +
" public final int BAR = 42;" + CPD.EOL +
"}";
private static final String TEST2 =
"public class Foo {" + CPD.EOL +
" public static final int BAR = 42;" + CPD.EOL +
"}";
private static final String TEST3 =
"public class Foo {" + CPD.EOL +
" public int BAR = 42;" + CPD.EOL +
"}";
private static final String TEST4 =
"public class Foo {" + CPD.EOL +
" public final String BAR = \"42\";" + CPD.EOL +
"}";
private static final String TEST5 =
"public class Foo {" + CPD.EOL +
" public final Thread BAR = new Thread();" + CPD.EOL +
"}";
}

View File

@ -73,6 +73,36 @@ public class Foo {
</example>
</rule>
<rule name="FinalFieldCouldBeStatic"
message="This final field could be made static"
class="net.sourceforge.pmd.rules.XpathRule">
<description>
If a final field is assigned to a compile-time constant, it could be
made static, thus saving overhead in each object
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//FieldDeclaration
[@Final='true' and @Static='false']
/VariableDeclarator/VariableInitializer
/Expression/ConditionalAndExpression/InstanceOfExpression
/PrimaryExpression/PrimaryPrefix/Literal
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public final int BAR = 42; // this could be static and save some space
}
]]>
</example>
</rule>
</ruleset>