From 8fcaa2748f0d222a6851889a61451253f1dfbbab Mon Sep 17 00:00:00 2001 From: Tom Copeland Date: Tue, 8 Oct 2002 18:05:56 +0000 Subject: [PATCH] more refactoring git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1085 51baf565-9d33-0410-a72c-fc3788e3496d --- .../net/sourceforge/pmd/ast/ASTTryStatement.java | 13 +++++++++++++ .../pmd/rules/EmptyFinallyBlockRule.java | 10 ++-------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/pmd/src/net/sourceforge/pmd/ast/ASTTryStatement.java b/pmd/src/net/sourceforge/pmd/ast/ASTTryStatement.java index 47c952708b..dafb3ed747 100644 --- a/pmd/src/net/sourceforge/pmd/ast/ASTTryStatement.java +++ b/pmd/src/net/sourceforge/pmd/ast/ASTTryStatement.java @@ -32,6 +32,19 @@ public class ASTTryStatement extends SimpleNode { return hasFinally; } + /** + * 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); + } + /** * Call hasCatch() before you call this method */ diff --git a/pmd/src/net/sourceforge/pmd/rules/EmptyFinallyBlockRule.java b/pmd/src/net/sourceforge/pmd/rules/EmptyFinallyBlockRule.java index 60d70cac59..ae17881481 100644 --- a/pmd/src/net/sourceforge/pmd/rules/EmptyFinallyBlockRule.java +++ b/pmd/src/net/sourceforge/pmd/rules/EmptyFinallyBlockRule.java @@ -9,20 +9,14 @@ import net.sourceforge.pmd.AbstractRule; import net.sourceforge.pmd.RuleContext; import net.sourceforge.pmd.ast.ASTTryStatement; import net.sourceforge.pmd.ast.SimpleNode; +import net.sourceforge.pmd.ast.ASTBlock; public class EmptyFinallyBlockRule extends AbstractRule { public Object visit(ASTTryStatement node, Object data){ if (!node.hasFinally()) { return super.visit(node, data); } - - // assume this is a try..finally construct - int finallyNodeIndex = 1; - if (node.hasCatch()) { - // jump to the third child since there's a FormalParameter between the catch Block and the finally Block - finallyNodeIndex = 3; - } - SimpleNode finallyBlock = (SimpleNode)node.jjtGetChild(finallyNodeIndex); + ASTBlock finallyBlock = node.getFinallyBlock(); if (finallyBlock.jjtGetNumChildren() == 0) { RuleContext ctx = (RuleContext)data; ctx.getReport().addRuleViolation(createRuleViolation(ctx, finallyBlock.getBeginLine()));