View Javadoc

    1   package net.sourceforge.pmd.rules.strictexception;
    2
    3   import net.sourceforge.pmd.AbstractRule;
    4   import net.sourceforge.pmd.ast.ASTCatchStatement;
    5   import net.sourceforge.pmd.ast.ASTClassOrInterfaceType;
    6   import net.sourceforge.pmd.ast.ASTType;
    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 AvoidCatchingThrowable extends AbstractRule {
    16
    17      public Object visit(ASTCatchStatement node, Object data) {
    18          ASTType type = node.findChildrenOfType(ASTType.class).get(0);
    19          ASTClassOrInterfaceType name = type.findChildrenOfType(ASTClassOrInterfaceType.class).get(0);
    20          if (name.hasImageEqualTo("Throwable")) {
    21              addViolation(data, name);
    22          }
    23          return super.visit(node, data);
    24      }
    25  }