Implemented FinalizeShouldBeProtected
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1870 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -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>
|
||||
|
@ -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
|
||||
|
@ -1,3 +1,4 @@
|
||||
#!/bin/bash
|
||||
|
||||
java junit.textui.TestRunner test.net.sourceforge.pmd.$1
|
||||
|
||||
|
@ -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 +
|
||||
"}";
|
||||
|
||||
}
|
@ -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>
|
||||
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user