01: package net.sourceforge.pmd.rules.design;
02:
03: import net.sourceforge.pmd.AbstractRule;
04: import net.sourceforge.pmd.ast.ASTCatchStatement;
05: import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
06: import net.sourceforge.pmd.ast.ASTFormalParameter;
07: import net.sourceforge.pmd.ast.ASTThrowStatement;
08: import net.sourceforge.pmd.ast.ASTTryStatement;
09: import net.sourceforge.pmd.ast.ASTType;
10:
11: import java.util.List;
12:
13: /**
14: * Catches the use of exception statements as a flow control device.
15: *
16: * @author Will Sargent
17: */
18: public class ExceptionAsFlowControl extends AbstractRule {
19:
20: public Object visit(ASTThrowStatement node, Object data) {
21: ASTTryStatement parent = node
22: .getFirstParentOfType(ASTTryStatement.class);
23: if (parent == null) {
24: return data;
25: }
26: for (parent = parent
27: .getFirstParentOfType(ASTTryStatement.class); parent != null; parent = parent
28: .getFirstParentOfType(ASTTryStatement.class)) {
29:
30: List<ASTCatchStatement> list = parent
31: .findChildrenOfType(ASTCatchStatement.class);
32: for (ASTCatchStatement catchStmt : list) {
33: ASTFormalParameter fp = (ASTFormalParameter) catchStmt
34: .jjtGetChild(0);
35: ASTType type = fp.findChildrenOfType(ASTType.class)
36: .get(0);
37: ASTClassOrInterfaceType name = type.findChildrenOfType(
38: ASTClassOrInterfaceType.class).get(0);
39: if (node.getFirstClassOrInterfaceTypeImage() != null
40: && node.getFirstClassOrInterfaceTypeImage()
41: .equals(name.getImage())) {
42: addViolation(data, name);
43: }
44: }
45: }
46: return data;
47: }
48:
49: }
|