diff --git a/pmd/regress/test/net/sourceforge/pmd/rules/EmptyFinallyBlockRuleTest.java b/pmd/regress/test/net/sourceforge/pmd/rules/EmptyFinallyBlockRuleTest.java index f9e42ae5c5..723d5b69fe 100644 --- a/pmd/regress/test/net/sourceforge/pmd/rules/EmptyFinallyBlockRuleTest.java +++ b/pmd/regress/test/net/sourceforge/pmd/rules/EmptyFinallyBlockRuleTest.java @@ -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()); } } diff --git a/pmd/src/net/sourceforge/pmd/ant/PMDTask.java b/pmd/src/net/sourceforge/pmd/ant/PMDTask.java index e117e8c836..3018459753 100644 --- a/pmd/src/net/sourceforge/pmd/ant/PMDTask.java +++ b/pmd/src/net/sourceforge/pmd/ant/PMDTask.java @@ -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) { diff --git a/pmd/src/net/sourceforge/pmd/ast/ASTTryStatement.java b/pmd/src/net/sourceforge/pmd/ast/ASTTryStatement.java index dafb3ed747..ee2a57f3d5 100644 --- a/pmd/src/net/sourceforge/pmd/ast/ASTTryStatement.java +++ b/pmd/src/net/sourceforge/pmd/ast/ASTTryStatement.java @@ -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); } diff --git a/pmd/src/net/sourceforge/pmd/rules/EmptyCatchBlockRule.java b/pmd/src/net/sourceforge/pmd/rules/EmptyCatchBlockRule.java index 260dc4ed79..d5d6c947cc 100644 --- a/pmd/src/net/sourceforge/pmd/rules/EmptyCatchBlockRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/EmptyCatchBlockRule.java @@ -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); } diff --git a/pmd/test-data/EmptyFinallyBlock4.java b/pmd/test-data/EmptyFinallyBlock4.java new file mode 100644 index 0000000000..ee2c50803d --- /dev/null +++ b/pmd/test-data/EmptyFinallyBlock4.java @@ -0,0 +1,10 @@ +public class EmptyFinallyBlock4 { + public void foo() { + try { + } catch (IOException e ){ + } catch (Exception e ) { + } catch (Throwable t ) { + } finally{ + } + } +}