View Javadoc

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