From 4a5372f8408fb2455c7cc8a9431f1f256f696de0 Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Mon, 28 Apr 2003 19:48:50 +0000 Subject: [PATCH] Implemented FinalizeShouldBeProtected git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1870 51baf565-9d33-0410-a72c-fc3788e3496d --- pmd/etc/build.xml | 4 +- pmd/etc/changelog.txt | 1 + pmd/etc/test.sh | 1 + .../FinalizeShouldBeProtectedRuleTest.java | 39 +++++++++++++++ pmd/rulesets/newrules.xml | 49 +++++++++++++++++++ .../net/sourceforge/pmd/rules/BracesRule.java | 13 ----- 6 files changed, 92 insertions(+), 15 deletions(-) create mode 100644 pmd/regress/test/net/sourceforge/pmd/rules/FinalizeShouldBeProtectedRuleTest.java delete mode 100644 pmd/src/net/sourceforge/pmd/rules/BracesRule.java diff --git a/pmd/etc/build.xml b/pmd/etc/build.xml index f0ba90e31d..1807fef143 100644 --- a/pmd/etc/build.xml +++ b/pmd/etc/build.xml @@ -60,9 +60,9 @@ - + - + diff --git a/pmd/etc/changelog.txt b/pmd/etc/changelog.txt index f57fe02d2c..0d88367c35 100644 --- a/pmd/etc/changelog.txt +++ b/pmd/etc/changelog.txt @@ -1,4 +1,5 @@ ????? - 1.06: +Added new rule: FinalizeShouldBeProtected 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/etc/test.sh b/pmd/etc/test.sh index d6f5a3c2fd..224ecb056c 100755 --- a/pmd/etc/test.sh +++ b/pmd/etc/test.sh @@ -1,3 +1,4 @@ #!/bin/bash java junit.textui.TestRunner test.net.sourceforge.pmd.$1 + diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/FinalizeShouldBeProtectedRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/FinalizeShouldBeProtectedRuleTest.java new file mode 100644 index 0000000000..4256a9252d --- /dev/null +++ b/pmd/regress/test/net/sourceforge/pmd/rules/FinalizeShouldBeProtectedRuleTest.java @@ -0,0 +1,39 @@ +package test.net.sourceforge.pmd.rules; + +import net.sourceforge.pmd.cpd.CPD; +import net.sourceforge.pmd.Rule; +import net.sourceforge.pmd.rules.XPathRule; + +public class FinalizeShouldBeProtectedRuleTest extends SimpleAggregatorTst { + + private Rule rule; + + public void setUp() { + rule = new XPathRule(); + rule.addProperty("xpath", "//MethodDeclaration[@Protected=\"false\"]/MethodDeclarator[@Image=\"finalize\"][not(FormalParameters/*)]"); + } + + public void testAll() { + runTests(new TestDescriptor[] { + new TestDescriptor(TEST1, "public finalize", 1, rule), + new TestDescriptor(TEST2, "finalize with some params", 0, rule), + new TestDescriptor(TEST3, "legitimate overriding", 0, rule) + }); + } + + private static final String TEST1 = + "public class Foo {" + CPD.EOL + + " public void finalize() {}" + CPD.EOL + + "}"; + + private static final String TEST2 = + "public class Foo {" + CPD.EOL + + " public void finalize(int x) {}" + CPD.EOL + + "}"; + + private static final String TEST3 = + "public class Foo {" + CPD.EOL + + " protected void finalize() {}" + CPD.EOL + + "}"; + +} diff --git a/pmd/rulesets/newrules.xml b/pmd/rulesets/newrules.xml index 8cf621d782..4ee4405e5a 100644 --- a/pmd/rulesets/newrules.xml +++ b/pmd/rulesets/newrules.xml @@ -21,8 +21,57 @@ These are new rules for the next release + + + Avoid idempotent operations - they are silly. + + 3 + + + + + + + + If you override finalize(), make it protected + + + + + + + + + 3 + + + + + diff --git a/pmd/src/net/sourceforge/pmd/rules/BracesRule.java b/pmd/src/net/sourceforge/pmd/rules/BracesRule.java deleted file mode 100644 index 68969f5dad..0000000000 --- a/pmd/src/net/sourceforge/pmd/rules/BracesRule.java +++ /dev/null @@ -1,13 +0,0 @@ -package net.sourceforge.pmd.rules; - -import net.sourceforge.pmd.AbstractRule; -import net.sourceforge.pmd.ast.ASTBlock; -import net.sourceforge.pmd.ast.SimpleNode; - -public abstract class BracesRule extends AbstractRule { - - protected boolean hasBlockAsFirstChild(SimpleNode node) { - return (node.jjtGetNumChildren() != 0 && (node.jjtGetChild(0) instanceof ASTBlock)); - } - -}