View Javadoc

1   /**
2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3    */
4   package net.sourceforge.pmd.lang.java.rule.strictexception;
5   
6   import net.sourceforge.pmd.lang.java.ast.ASTCatchStatement;
7   import net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType;
8   import net.sourceforge.pmd.lang.java.ast.ASTType;
9   import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
10  
11  /**
12   * Finds <code>catch</code> statements containing <code>throwable</code> as the
13   * type definition.
14   * <p/>
15   * 
16   * @author <a mailto:trondandersen@c2i.net>Trond Andersen</a>
17   */
18  public class AvoidCatchingThrowableRule extends AbstractJavaRule {
19  
20      @Override
21      public Object visit(ASTCatchStatement node, Object data) {
22          ASTType type = node.getFirstDescendantOfType(ASTType.class);
23          ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
24          if (name.hasImageEqualTo("Throwable")) {
25              addViolation(data, name);
26          }
27          return super.visit(node, data);
28      }
29  }