Added new rule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@90 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-06-27 17:03:46 +00:00
parent 90c9f7d744
commit d0251d53ae
9 changed files with 73 additions and 7 deletions

View File

@ -30,8 +30,8 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.PMDTask"/>
<pmd reportFile="c:\pmd.xml" verbose="false" rulesettype="general" format="xml">
<fileset dir="c:\data\pmd\pmd\src">
<!--<fileset dir="c:\j2sdk1.4.0\src">-->
<!--<fileset dir="c:\data\pmd\pmd\src">-->
<fileset dir="c:\j2sdk1.4.0\src">
<include name="**/*.java"/>
</fileset>
</pmd>

View File

@ -1,7 +1,11 @@
??? - 0.2:
Added IfElseStmtsMustUseBracesRule
Added new rules:
+IfElseStmtsMustUseBracesRule
+EmptyWhileStmtRule
Modified command line interface to accept a rule set
Fixed bug in EmptyCatchBlockRule
Fixed type in UnnecessaryConversionTemporaryRule
June 25 2002 - 0.1:
Initial release

View File

@ -1,6 +1,6 @@
update the xdocs to reflect any new release changes
update the xdocs:
+command line ui now takes a filename, format, rule set
update the changelog.txt
update the readme.txt (when we get one)
ant dist
rename jar to pmd-0.2.jar

View File

@ -138,6 +138,12 @@ public class FunctionalTest extends TestCase{
assertEquals(new EmptyIfStmtRule(), ((RuleViolation)report.violationsInCurrentFile().next()).getRule());
}
public void testEmptyWhileStmtRule() {
Report report = process("EmptyWhileStmtRule.java");
assertEquals(1, report.countViolationsInCurrentFile());
assertEquals(new EmptyWhileStmtRule(), ((RuleViolation)report.violationsInCurrentFile().next()).getRule());
}
public void testUnusedPrivateInstanceVar1() {
Report report = process("UnusedPrivateInstanceVar1.java");
assertEquals(1, report.countViolationsInCurrentFile());

View File

@ -56,6 +56,7 @@ public class RuleFactory {
List list = new ArrayList();
list.add(new EmptyCatchBlockRule());
list.add(new EmptyIfStmtRule());
list.add(new EmptyWhileStmtRule());
list.add(new UnnecessaryConversionTemporaryRule());
list.add(new UnusedLocalVariableRule());
list.add(new UnusedPrivateInstanceVariableRule());

View File

@ -12,7 +12,7 @@ import net.sourceforge.pmd.ast.ASTIfStatement;
import net.sourceforge.pmd.*;
public class EmptyIfStmtRule extends AbstractRule implements Rule {
public String getDescription() {return "Avoid empty IF statements";}
public String getDescription() {return "Avoid empty 'if' statements";}
public Object visit(ASTBlock node, Object data){
if ((node.jjtGetParent().jjtGetParent() instanceof ASTIfStatement) && node.jjtGetNumChildren()==0) {

View File

@ -0,0 +1,45 @@
/*
* User: tom
* Date: Jun 27, 2002
* Time: 11:40:07 AM
*/
package net.sourceforge.pmd.rules;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.Rule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.ast.*;
public class EmptyWhileStmtRule extends AbstractRule implements Rule{
public String getDescription() {return "Avoid empty 'while' statements";}
/**
* We're looking for anything other than
* WhileStmt
* Expression
* Statement
* Block
* BlockStmt
*/
public Object visit(ASTWhileStatement node, Object data){
ASTStatement stmt = (ASTStatement)node.jjtGetChild(1);
// can this happen? an Statement without a child?
if (stmt.jjtGetNumChildren() == 0) {
return super.visit(node, data);
}
// an Statement whose child is not a Block... this might be caught be another braces type rule
if (!(stmt.jjtGetChild(0) instanceof ASTBlock)) {
return super.visit(node, data);
}
ASTBlock block = (ASTBlock)stmt.jjtGetChild(0);
// this block must have 1 child, and that child must be a BlockStatement
if (block.jjtGetNumChildren() == 0 || (!(block.jjtGetChild(0) instanceof ASTBlockStatement))) {
(((RuleContext)data).getReport()).addRuleViolation(new RuleViolation(this, node.getBeginLine()));
}
return super.visit(node, data);
}
}

View File

@ -28,7 +28,7 @@ public class UnnecessaryConversionTemporaryRule extends AbstractRule implements
}
public String getDescription() {
return "Avoid unnecessay temporaries when converting primitives to Strings";
return "Avoid unnecessary temporaries when converting primitives to Strings";
}
public Object visit(ASTPrimaryExpression node, Object data) {

View File

@ -0,0 +1,10 @@
public class EmptyWhileStmtRule {
public EmptyWhileStmtRule() {
while (true == true) {
}
while (true == true) {
String x = "";
x = "hi";
}
}
}