Added EmptyTryBlockRule

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@244 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-07-08 19:45:00 +00:00
parent ce81a9986a
commit 8e7ead43c0
8 changed files with 105 additions and 2 deletions

View File

@ -35,7 +35,8 @@
<target name="pmd">
<taskdef name="pmd" classname="net.sourceforge.pmd.ant.PMDTask"/>
<pmd reportFile="c:\jdk14.html" rulesetfiles="rulesets/ticbuild.xml,rulesets/unusedcode.xml,rulesets/design.xml" format="html">
<!--<pmd reportFile="c:\jdk14.html" rulesetfiles="rulesets/ticbuild.xml,rulesets/unusedcode.xml,rulesets/design.xml" format="html">-->
<pmd reportFile="c:\jdk14.html" rulesetfiles="rulesets/new_for_0_4.xml" format="html">
<fileset dir="c:\data\pmd\pmd\src">
<!--<fileset dir="c:\j2sdk1.4.0\src">-->
<include name="**/*.java"/>

View File

@ -2,4 +2,4 @@
set MAIN=net.sourceforge.pmd.PMD
set TEST_FILE=c:\\data\\pmd\\pmd\\test-data\\%1%.java
java %MAIN% %TEST_FILE% xml rulesets\unusedcode.xml
java %MAIN% %TEST_FILE% xml rulesets\new_for_0_4.xml

View File

@ -0,0 +1,35 @@
/*
* User: tom
* Date: Jul 8, 2002
* Time: 3:31:37 PM
*/
package test.net.sourceforge.pmd.rules;
import net.sourceforge.pmd.Report;
import net.sourceforge.pmd.RuleViolation;
import net.sourceforge.pmd.rules.EmptyCatchBlockRule;
import net.sourceforge.pmd.rules.EmptyTryBlockRule;
public class EmptyTryBlockRuleTest extends RuleTst {
public EmptyTryBlockRuleTest(String name) {
super(name);
}
public void testEmptyTryBlock1() throws Throwable {
Report report = process("EmptyTryBlock1.java", new EmptyTryBlockRule());
assertEquals(1, report.size());
assertEquals(new EmptyTryBlockRule(), ((RuleViolation)report.iterator().next()).getRule());
}
public void testEmptyTryBlock2() throws Throwable {
Report report = process("EmptyTryBlock2.java", new EmptyTryBlockRule());
assertEquals(1, report.size());
assertEquals(new EmptyTryBlockRule(), ((RuleViolation)report.iterator().next()).getRule());
}
public void testEmptyTryBlock3() throws Throwable {
Report report = process("EmptyTryBlock3.java", new EmptyTryBlockRule());
assertTrue(report.isEmpty());
}
}

View File

@ -36,6 +36,26 @@ public class Foo {
// return some hash value
}
}
]]>
</example>
</rule>
<rule name="EmptyTryBlock"
message="Avoid empty try blocks"
class="net.sourceforge.pmd.rules.EmptyTryBlockRule">
<description>
Avoid empty try blocks
</description>
<example>
<![CDATA[
// this is bad
public boolean equals(Object o) {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
]]>
</example>
</rule>

View File

@ -0,0 +1,22 @@
/*
* User: tom
* Date: Jul 8, 2002
* Time: 3:34:04 PM
*/
package net.sourceforge.pmd.rules;
import net.sourceforge.pmd.AbstractRule;
import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.ast.ASTTryStatement;
import net.sourceforge.pmd.ast.ASTBlock;
public class EmptyTryBlockRule extends AbstractRule{
public Object visit(ASTTryStatement node, Object data){
RuleContext ctx = (RuleContext)data;
ASTBlock tryBlock = (ASTBlock)node.jjtGetChild(0);
if (tryBlock.jjtGetNumChildren() == 0) {
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
}
return super.visit(node, data);
}
}

View File

@ -0,0 +1,8 @@
public class EmptyTryBlock1 {
public void foo() {
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}

View File

@ -0,0 +1,8 @@
public class EmptyTryBlock2 {
public void foo() {
try {
} finally {
int x = 5;
}
}
}

View File

@ -0,0 +1,9 @@
public class EmptyTryBlock3 {
public void foo() {
try {
int f =2;
} finally {
int x = 5;
}
}
}