Implemented FinalizeShouldBeProtected

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1870 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-04-28 19:48:50 +00:00
parent 56d6991063
commit 4a5372f840
6 changed files with 92 additions and 15 deletions

View File

@ -60,9 +60,9 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<pmd rulesetfiles="rulesets/favorites.xml">
<pmd rulesetfiles="rulesets/newrules.xml">
<formatter type="net.sourceforge.pmd.renderers.HTMLRenderer" toFile="foo.html"/>
<fileset dir="/usr/local/java/src/java/lang/ref">
<fileset dir="/usr/local/java/src/java/lang/">
<include name="**/*.java"/>
</fileset>
</pmd>

View File

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

View File

@ -1,3 +1,4 @@
#!/bin/bash
java junit.textui.TestRunner test.net.sourceforge.pmd.$1

View File

@ -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 +
"}";
}

View File

@ -21,8 +21,57 @@ These are new rules for the next release
</example>
</rule>
<rule name="IdempotentOperationsRule"
message="Avoid idempotent operations"
class="net.sourceforge.pmd.rules.IdempotentOperationsRule">
<description>
Avoid idempotent operations - they are silly.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public void bar() {
int x = 2;
x = x;
}
}
]]>
</example>
</rule>
<rule name="FinalizeShouldBeProtected"
message="If you override finalize(), make it protected"
class="net.sourceforge.pmd.rules.XpathRule">
<description>
If you override finalize(), make it protected
</description>
<properties>
<property name="xpath">
<value>
<![CDATA[
//MethodDeclaration[@Protected="false"]
/MethodDeclarator[@Image="finalize"]
[not(FormalParameters/*)]
]]>
</value>
</property>
</properties>
<priority>3</priority>
<example>
<![CDATA[
public class Foo {
public void bar() {
int x = 2;
x = x;
}
}
]]>
</example>
</rule>
</ruleset>

View File

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