From 889cac77b909201f9e7a29ba4dc985fec2a3cba0 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Tue, 29 Apr 2003 15:40:59 +0000 Subject: [PATCH] Added new FinalFieldCouldBeStatic rule git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1874 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/changelog.txt | 2 +- .../FinalFieldCouldBeStaticRuleTest.java | 51 +++++++++++++++++++ pmd/rulesets/newrules.xml | 30 +++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/FinalFieldCouldBeStaticRuleTest.java diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index 0d88367c35..1f697eb4fa 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -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 diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/FinalFieldCouldBeStaticRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/FinalFieldCouldBeStaticRuleTest.java new file mode 100644 index 0000000000..45ccb1c0d8 --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/FinalFieldCouldBeStaticRuleTest.java @@ -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 + + "}"; + +} diff --git a/pmd/rulesets/newrules.xml b/pmd/rulesets/newrules.xml index 4ee4405e5a..3df1daf385 100644 --- a/pmd/rulesets/newrules.xml +++ b/pmd/rulesets/newrules.xml @@ -73,6 +73,36 @@ public class Foo { + + + If a final field is assigned to a compile-time constant, it could be + made static, thus saving overhead in each object + + + + + + + + + 3 + + + + +