コード例 #1
0
 private boolean ifVerify(ASTIfStatement is, String varname) {
   List finder = new ArrayList();
   is.findChildrenOfType(ASTPrimaryExpression.class, finder, true);
   if (finder.size() > 1) {
     ASTPrimaryExpression apeLeft = (ASTPrimaryExpression) finder.get(0);
     if (matchName(apeLeft, varname)) {
       ASTPrimaryExpression apeRight = (ASTPrimaryExpression) finder.get(1);
       if ((apeRight.jjtGetNumChildren() == 1)
           && (apeRight.jjtGetChild(0) instanceof ASTPrimaryPrefix)) {
         ASTPrimaryPrefix pp2 = (ASTPrimaryPrefix) apeRight.jjtGetChild(0);
         if ((pp2.jjtGetNumChildren() == 1) && (pp2.jjtGetChild(0) instanceof ASTLiteral)) {
           ASTLiteral lit = (ASTLiteral) pp2.jjtGetChild(0);
           if ((lit.jjtGetNumChildren() == 1) && (lit.jjtGetChild(0) instanceof ASTNullLiteral)) {
             return true;
           }
         }
       }
     }
   }
   return false;
 }
コード例 #2
0
  public Object visit(ASTMethodDeclaration node, Object data) {
    if (interfaceSkipper == true) { // skip methods in interfaces
      return super.visit(node, data);
    }
    ASTResultType rt = (ASTResultType) node.jjtGetChild(0);
    if (rt.isVoid() == true) {
      return super.visit(node, data);
    }
    ASTType t = (ASTType) rt.jjtGetChild(0);
    if (t.jjtGetNumChildren() == 0 || !(t.jjtGetChild(0) instanceof ASTName)) {
      return super.visit(node, data);
    }
    String returnVariableName = null;
    List finder = new ArrayList();
    GET_RETURN_VARIABLE_NAME:
    {
      node.findChildrenOfType(ASTReturnStatement.class, finder, true);
      if (finder.size() != 1) {
        return super.visit(node, data);
      }
      ASTReturnStatement rs =
          (ASTReturnStatement)
              finder.get(0); // EXPLODES IF THE CLASS IS AN INTERFACE SINCE NO RETURN

      finder.clear();
      rs.findChildrenOfType(ASTPrimaryExpression.class, finder, true);
      ASTPrimaryExpression ape = (ASTPrimaryExpression) finder.get(0);
      Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
      if (lastChild instanceof ASTPrimaryPrefix) {
        returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
      }
      if (returnVariableName == null) {
        return super.visit(node, data);
      }
    }
    CHECK_OUTER_IF:
    {
      finder.clear();
      node.findChildrenOfType(ASTIfStatement.class, finder, true);
      if (finder.size() == 2) {
        ASTIfStatement is = (ASTIfStatement) finder.get(0);
        if (ifVerify(is, returnVariableName)) {
          // find synchronized
          finder.clear();
          is.findChildrenOfType(ASTSynchronizedStatement.class, finder, true);
          if (finder.size() == 1) {
            ASTSynchronizedStatement ss = (ASTSynchronizedStatement) finder.get(0);
            finder.clear();
            ss.findChildrenOfType(ASTIfStatement.class, finder, true);
            if (finder.size() == 1) {
              ASTIfStatement is2 = (ASTIfStatement) finder.get(0);
              if (ifVerify(is2, returnVariableName)) {
                finder.clear();
                is2.findChildrenOfType(ASTStatementExpression.class, finder, true);
                if (finder.size() == 1) {
                  ASTStatementExpression se = (ASTStatementExpression) finder.get(0);
                  if (se.jjtGetNumChildren()
                      == 3) { // primaryExpression, AssignmentOperator, Expression
                    if (se.jjtGetChild(0) instanceof ASTPrimaryExpression) {
                      ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
                      if (matchName(pe, returnVariableName)) {
                        if (se.jjtGetChild(1) instanceof ASTAssignmentOperator) {
                          RuleContext ctx = (RuleContext) data;
                          ctx.getReport()
                              .addRuleViolation(createRuleViolation(ctx, node.getBeginLine()));
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    return super.visit(node, data);
  }