fixed bug 633879

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1203 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2002-11-05 19:38:41 +00:00
parent 487a46f569
commit f9a7b1ef3d
5 changed files with 23 additions and 20 deletions

View File

@ -12,19 +12,18 @@ import net.sourceforge.pmd.rules.EmptyFinallyBlockRule;
public class EmptyFinallyBlockRuleTest extends RuleTst {
public void testEmptyFinallyBlock1() throws Throwable {
Report report = process("EmptyFinallyBlock1.java", new EmptyFinallyBlockRule());
assertEquals(1, report.size());
assertEquals(new EmptyFinallyBlockRule(), ((RuleViolation)report.iterator().next()).getRule());
runTest("EmptyFinallyBlock1.java", 1, new EmptyFinallyBlockRule());
}
public void testEmptyFinallyBlock2() throws Throwable {
Report report = process("EmptyFinallyBlock2.java", new EmptyFinallyBlockRule());
assertEquals(1, report.size());
assertEquals(new EmptyFinallyBlockRule(), ((RuleViolation)report.iterator().next()).getRule());
runTest("EmptyFinallyBlock2.java", 1, new EmptyFinallyBlockRule());
}
public void testEmptyFinallyBlock3() throws Throwable {
Report report = process("EmptyFinallyBlock3.java", new EmptyFinallyBlockRule());
assertTrue(report.isEmpty());
runTest("EmptyFinallyBlock3.java", 0, new EmptyFinallyBlockRule());
}
public void testMultipleCatchBlocksWithFinally() throws Throwable {
runTest("EmptyFinallyBlock4.java", 1, new EmptyFinallyBlockRule());
}
}

View File

@ -87,8 +87,7 @@ public class PMDTask extends Task {
File file = new File(ds.getBasedir() + System.getProperty("file.separator") + srcFiles[j]);
if (verbose) System.out.println(file.getAbsoluteFile());
String displayName = file.getPath().substring((int)fs.getDir(project).toString().length()+1);
ctx.setSourceCodeFilename(displayName);
ctx.setSourceCodeFilename(file.getPath().substring((int)fs.getDir(project).toString().length()+1));
pmd.processFile(new FileInputStream(file), rules, ctx);
} catch (FileNotFoundException fnfe) {

View File

@ -36,19 +36,13 @@ public class ASTTryStatement extends SimpleNode {
* Call hasFinally() before you call this method
*/
public ASTBlock getFinallyBlock() {
// assume this is a try..finally construct
int finallyNodeIndex = 1;
if (hasCatch()) {
// jump to the third child since there's a FormalParameter between the catch Block and the finally Block
finallyNodeIndex = 3;
}
return (ASTBlock)jjtGetChild(finallyNodeIndex);
return (ASTBlock)jjtGetChild(jjtGetNumChildren()-1);
}
/**
* Call hasCatch() before you call this method
*/
public ASTBlock getCatchBlock() {
public ASTBlock getFirstCatchBlock() {
return (ASTBlock)jjtGetChild(2);
}

View File

@ -10,14 +10,15 @@ import net.sourceforge.pmd.*;
public class EmptyCatchBlockRule extends AbstractRule implements Rule {
// TODO this only catches the first empty catch block of a try..catch stmt
public Object visit(ASTTryStatement node, Object data){
RuleContext ctx = (RuleContext)data;
// this skips try..finally constructs since they don't have catch blocks
if (!node.hasCatch()) {
return super.visit(node, data);
}
if (node.getCatchBlock().jjtGetNumChildren() == 0) {
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getCatchBlock().getBeginLine()));
if (node.getFirstCatchBlock().jjtGetNumChildren() == 0) {
ctx.getReport().addRuleViolation(createRuleViolation(ctx, node.getFirstCatchBlock().getBeginLine()));
}
return super.visit(node, data);
}

View File

@ -0,0 +1,10 @@
public class EmptyFinallyBlock4 {
public void foo() {
try {
} catch (IOException e ){
} catch (Exception e ) {
} catch (Throwable t ) {
} finally{
}
}
}