View Javadoc

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.ASTCastExpression;
6   import net.sourceforge.pmd.ast.ASTCatch;
7   import net.sourceforge.pmd.ast.ASTInstanceOfExpression;
8   import net.sourceforge.pmd.ast.ASTName;
9   import net.sourceforge.pmd.ast.ASTPrimaryExpression;
10  import net.sourceforge.pmd.ast.ASTTryStatement;
11  import net.sourceforge.pmd.ast.ASTType;
12  import net.sourceforge.pmd.ast.ASTVariableDeclaratorId;
13  
14  import java.util.Iterator;
15  import java.util.List;
16  
17  /***
18   * <p/>
19   *
20   * @author <a mailto:trond.andersen@nordea.com>Trond Andersen</a>
21   * @version 1.0
22   * @since 1.1?
23   */
24  public class ExceptionTypeChecking extends AbstractRule {
25  
26      public Object visit(ASTTryStatement catchStatment, Object object) {
27          if (catchStatment.hasCatch()) {
28              for (Iterator iter = catchStatment.getCatchBlocks().iterator(); iter.hasNext();) {
29                  evaluateCatchClause((ASTCatch) iter.next(), (RuleContext) object);
30              }
31          }
32  
33          return super.visit(catchStatment, object);
34      }
35  
36      private void evaluateCatchClause(ASTCatch catchStmt, RuleContext ctx) {
37          String exceptionParameter = getExceptionParameter(catchStmt);
38          // Retrieves all instance of expressions
39          List myList = catchStmt.getBlock().findChildrenOfType(ASTInstanceOfExpression.class);
40  
41          for (Iterator i = myList.iterator(); i.hasNext();) {
42              evaluateInstanceOfExpression((ASTInstanceOfExpression) i.next(), exceptionParameter, ctx);
43          }
44      }
45  
46      private void evaluateInstanceOfExpression(ASTInstanceOfExpression instanceOfExpression,
47                                                String exceptionName, RuleContext ctx) {
48          if (!hasTypeEvaluation(instanceOfExpression)) {
49              return;
50          }
51          if (exceptionName.equals(getInstanceOfObjectReference(instanceOfExpression))) {
52              ctx.getReport().addRuleViolation(createRuleViolation(ctx, instanceOfExpression));
53          }
54      }
55  
56      private boolean hasTypeEvaluation(ASTInstanceOfExpression instanceOfExpression) {
57          List typeList = instanceOfExpression.findChildrenOfType(ASTType.class);
58          if (typeList != null && typeList.size() >= 1) {
59              ASTType theType = (ASTType) typeList.get(0);
60              if (!(theType.jjtGetParent() instanceof ASTCastExpression)) {
61                  return true;
62              }
63          }
64          return false;
65      }
66  
67      private String getInstanceOfObjectReference(ASTInstanceOfExpression expression) {
68          List primaryList = expression.findChildrenOfType(ASTPrimaryExpression.class);
69          String objectReferenceName = null;
70          if (primaryList.size() == 1) {
71              List someList = ((ASTPrimaryExpression) primaryList.get(0)).findChildrenOfType(ASTName.class);
72              if (someList.size() == 1) {
73                  objectReferenceName = ((ASTName) someList.get(0)).getImage();
74              }
75          }
76          return objectReferenceName;
77      }
78  
79      private String getExceptionParameter(ASTCatch catchStmt) {
80          List declarationList = catchStmt.getFormalParameter().findChildrenOfType(ASTVariableDeclaratorId.class);
81          return ((ASTVariableDeclaratorId) declarationList.get(0)).getImage();
82      }
83  
84  }