示例#1
0
  @Override
  public Object visit(ASTVariableDeclarator node, Object data) {
    // Search Catch stmt nodes for variable used to store improperly created
    // throwable or exception
    try {
      if (node.hasDescendantMatchingXPath(FIND_THROWABLE_INSTANCE)) {
        String variableName = node.jjtGetChild(0).getImage(); // VariableDeclatorId
        ASTCatchStatement catchStmt = node.getFirstParentOfType(ASTCatchStatement.class);

        while (catchStmt != null) {
          List<Node> violations =
              catchStmt.findChildNodesWithXPath(
                  "//Expression/PrimaryExpression/PrimaryPrefix/Name[@Image = '"
                      + variableName
                      + "']");
          if (!violations.isEmpty()) {
            // If, after this allocation, the 'initCause' method is
            // called, and the ex passed
            // this is not a violation
            if (!useInitCause(violations.get(0), catchStmt)) {
              addViolation(data, node);
            }
          }

          // check ASTCatchStatement higher up
          catchStmt = catchStmt.getFirstParentOfType(ASTCatchStatement.class);
        }
      }
      return super.visit(node, data);
    } catch (JaxenException e) {
      // XPath is valid, this should never happens...
      throw new IllegalStateException(e);
    }
  }