more refactoring
git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1085 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
@ -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
|
||||
*/
|
||||
|
@ -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()));
|
||||
|
Reference in New Issue
Block a user