1 |
| package net.sourceforge.pmd.rules.strictexception; |
2 |
| |
3 |
| import net.sourceforge.pmd.AbstractRule; |
4 |
| import net.sourceforge.pmd.RuleContext; |
5 |
| import net.sourceforge.pmd.ast.ASTBlock; |
6 |
| import net.sourceforge.pmd.ast.ASTCastExpression; |
7 |
| import net.sourceforge.pmd.ast.ASTCatchStatement; |
8 |
| import net.sourceforge.pmd.ast.ASTInstanceOfExpression; |
9 |
| import net.sourceforge.pmd.ast.ASTName; |
10 |
| import net.sourceforge.pmd.ast.ASTPrimaryExpression; |
11 |
| import net.sourceforge.pmd.ast.ASTType; |
12 |
| import net.sourceforge.pmd.ast.ASTVariableDeclaratorId; |
13 |
| import net.sourceforge.pmd.ast.SimpleNode; |
14 |
| |
15 |
| import java.util.Iterator; |
16 |
| import java.util.List; |
17 |
| |
18 |
| |
19 |
| |
20 |
| |
21 |
| public class ExceptionTypeChecking extends AbstractRule { |
22 |
| |
23 |
0
| public Object visit(ASTCatchStatement node, Object data) {
|
24 |
0
| String exceptionParameter = getExceptionParameter(node);
|
25 |
| |
26 |
0
| ASTBlock block = (ASTBlock)(node.jjtGetChild(1));
|
27 |
0
| List myList = block.findChildrenOfType(ASTInstanceOfExpression.class);
|
28 |
0
| for (Iterator i = myList.iterator(); i.hasNext();) {
|
29 |
0
| evaluateInstanceOfExpression((ASTInstanceOfExpression) i.next(), exceptionParameter, (RuleContext)data);
|
30 |
| } |
31 |
0
| return super.visit(node, data);
|
32 |
| } |
33 |
| |
34 |
0
| private void evaluateInstanceOfExpression(ASTInstanceOfExpression instanceOfExpression,
|
35 |
| String exceptionName, RuleContext ctx) { |
36 |
0
| if (!hasTypeEvaluation(instanceOfExpression)) {
|
37 |
0
| return;
|
38 |
| } |
39 |
0
| if (exceptionName.equals(getInstanceOfObjectReference(instanceOfExpression))) {
|
40 |
0
| ctx.getReport().addRuleViolation(createRuleViolation(ctx, instanceOfExpression));
|
41 |
| } |
42 |
| } |
43 |
| |
44 |
0
| private boolean hasTypeEvaluation(ASTInstanceOfExpression instanceOfExpression) {
|
45 |
0
| List typeList = instanceOfExpression.findChildrenOfType(ASTType.class);
|
46 |
0
| if (!typeList.isEmpty()) {
|
47 |
0
| ASTType theType = (ASTType) typeList.get(0);
|
48 |
0
| if (!(theType.jjtGetParent() instanceof ASTCastExpression)) {
|
49 |
0
| return true;
|
50 |
| } |
51 |
| } |
52 |
0
| return false;
|
53 |
| } |
54 |
| |
55 |
0
| private String getInstanceOfObjectReference(ASTInstanceOfExpression expression) {
|
56 |
0
| List primaryList = expression.findChildrenOfType(ASTPrimaryExpression.class);
|
57 |
0
| String objectReferenceName = null;
|
58 |
0
| if (primaryList.size() == 1) {
|
59 |
0
| List someList = ((ASTPrimaryExpression) primaryList.get(0)).findChildrenOfType(ASTName.class);
|
60 |
0
| if (someList.size() == 1) {
|
61 |
0
| objectReferenceName = ((ASTName) someList.get(0)).getImage();
|
62 |
| } |
63 |
| } |
64 |
0
| return objectReferenceName;
|
65 |
| } |
66 |
| |
67 |
0
| private String getExceptionParameter(ASTCatchStatement catchStmt) {
|
68 |
0
| ASTVariableDeclaratorId id = (ASTVariableDeclaratorId)((SimpleNode)catchStmt.jjtGetChild(0)).findChildrenOfType(ASTVariableDeclaratorId.class).get(0);
|
69 |
0
| return id.getImage();
|
70 |
| } |
71 |
| |
72 |
| } |