public Object visit(ASTPrimaryPrefix node, Object data) {
    if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
      return super.visit(node, data);
    }

    String image = ((ASTName) node.jjtGetChild(0)).getImage();
    if (image.startsWith("java.lang.")) {
      image = image.substring(10);
    }

    boolean checkBoolean =
        ((RuleContext) data)
                .getLanguageVersion()
                .compareTo(LanguageRegistry.getLanguage(JavaLanguageModule.NAME).getVersion("1.5"))
            >= 0;

    if (PREFIX_SET.contains(image) || (checkBoolean && "Boolean.valueOf".equals(image))) {
      ASTPrimaryExpression parent = (ASTPrimaryExpression) node.jjtGetParent();
      if (parent.jjtGetNumChildren() >= 3) {
        Node n = parent.jjtGetChild(2);
        if (n instanceof ASTPrimarySuffix) {
          ASTPrimarySuffix suffix = (ASTPrimarySuffix) n;
          image = suffix.getImage();

          if (SUFFIX_SET.contains(image) || (checkBoolean && "booleanValue".equals(image))) {
            super.addViolation(data, node);
            return data;
          }
        }
      }
    }
    return super.visit(node, data);
  }
  /**
   * Checks if the variable designed in parameter is written to a field (not local variable) in the
   * statements.
   */
  private boolean checkForDirectAssignment(
      Object ctx, final ASTFormalParameter parameter, final List<ASTBlockStatement> bs) {
    final ASTVariableDeclaratorId vid =
        parameter.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
    final String varName = vid.getImage();
    for (ASTBlockStatement b : bs) {
      if (b.hasDescendantOfType(ASTAssignmentOperator.class)) {
        final ASTStatementExpression se = b.getFirstDescendantOfType(ASTStatementExpression.class);
        if (se == null || !(se.jjtGetChild(0) instanceof ASTPrimaryExpression)) {
          continue;
        }
        String assignedVar = getExpressionVarName(se);
        if (assignedVar == null) {
          continue;
        }

        ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
        Node n = pe.getFirstParentOfType(ASTMethodDeclaration.class);
        if (n == null) {
          n = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
          if (n == null) {
            continue;
          }
        }
        if (!isLocalVariable(assignedVar, n)) {
          // TODO could this be more clumsy?  We really
          // need to build out the PMD internal framework more
          // to support simply queries like "isAssignedTo()" or something
          if (se.jjtGetNumChildren() < 3) {
            continue;
          }
          ASTExpression e = (ASTExpression) se.jjtGetChild(2);
          if (e.hasDescendantOfType(ASTEqualityExpression.class)) {
            continue;
          }
          String val = getExpressionVarName(e);
          if (val == null) {
            continue;
          }
          ASTPrimarySuffix foo = se.getFirstDescendantOfType(ASTPrimarySuffix.class);
          if (foo != null && foo.isArrayDereference()) {
            continue;
          }

          if (val.equals(varName)) {
            Node md = parameter.getFirstParentOfType(ASTMethodDeclaration.class);
            if (md == null) {
              md = pe.getFirstParentOfType(ASTConstructorDeclaration.class);
            }
            if (!isLocalVariable(varName, md)) {
              addViolation(ctx, parameter, varName);
            }
          }
        }
      }
    }
    return false;
  }
 /**
  * Simply return if the image start with keyword 'this' or 'super'.
  *
  * @return true, if keyword is used, false otherwise.
  */
 public boolean useThisOrSuper() {
   Node node = location.jjtGetParent();
   if (node instanceof ASTPrimaryExpression) {
     ASTPrimaryExpression primaryExpression = (ASTPrimaryExpression) node;
     ASTPrimaryPrefix prefix = (ASTPrimaryPrefix) primaryExpression.jjtGetChild(0);
     if (prefix != null) {
       return prefix.usesSuperModifier() || prefix.usesThisModifier();
     }
   }
   return image.startsWith(THIS_DOT) || image.startsWith(SUPER_DOT);
 }
 public Object visit(ASTPrimaryExpression node, Object data) {
   if (node.jjtGetNumChildren() == 0
       || (node.jjtGetChild(0)).jjtGetNumChildren() == 0
       || !(node.jjtGetChild(0).jjtGetChild(0) instanceof ASTAllocationExpression)) {
     return super.visit(node, data);
   }
   // TODO... hmmm... is this inPrimaryExpressionContext gibberish necessary?
   inPrimaryExpressionContext = true;
   primary = node;
   super.visit(node, data);
   inPrimaryExpressionContext = false;
   usingPrimitiveWrapperAllocation = false;
   return data;
 }
Esempio n. 5
0
 private boolean isInitCauseCalled(String target, List<NameOccurrence> occurrences) {
   boolean initCauseCalled = false;
   for (NameOccurrence occurrence : occurrences) {
     String image = null;
     if (occurrence.getLocation() != null) {
       image = occurrence.getLocation().getImage();
     }
     if (image != null && image.endsWith("initCause")) {
       ASTPrimaryExpression primaryExpression =
           occurrence.getLocation().getFirstParentOfType(ASTPrimaryExpression.class);
       if (primaryExpression != null) {
         ASTArgumentList args2 = primaryExpression.getFirstDescendantOfType(ASTArgumentList.class);
         if (checkForTargetUsage(target, args2)) {
           initCauseCalled = true;
           break;
         }
       }
     }
   }
   return initCauseCalled;
 }
Esempio n. 6
0
 @Override
 public Object visit(ASTCatchStatement catchStmt, Object data) {
   String target =
       catchStmt
           .jjtGetChild(0)
           .findChildrenOfType(ASTVariableDeclaratorId.class)
           .get(0)
           .getImage();
   // Inspect all the throw stmt inside the catch stmt
   List<ASTThrowStatement> lstThrowStatements =
       catchStmt.findDescendantsOfType(ASTThrowStatement.class);
   for (ASTThrowStatement throwStatement : lstThrowStatements) {
     Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
     if (n instanceof ASTCastExpression) {
       ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
       if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
         RuleContext ctx = (RuleContext) data;
         addViolation(ctx, throwStatement);
       }
       continue;
     }
     // Retrieve all argument for the throw exception (to see if the
     // original exception is preserved)
     ASTArgumentList args = throwStatement.getFirstDescendantOfType(ASTArgumentList.class);
     if (args != null) {
       Node parent = args.jjtGetParent().jjtGetParent();
       if (parent instanceof ASTAllocationExpression) {
         // maybe it is used inside a anonymous class
         ck(data, target, throwStatement, parent);
       } else {
         ck(data, target, throwStatement, args);
       }
     } else {
       Node child = throwStatement.jjtGetChild(0);
       while (child != null && child.jjtGetNumChildren() > 0 && !(child instanceof ASTName)) {
         child = child.jjtGetChild(0);
       }
       if (child != null) {
         if (child instanceof ASTName
             && !target.equals(child.getImage())
             && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
           Map<VariableNameDeclaration, List<NameOccurrence>> vars =
               ((ASTName) child).getScope().getDeclarations(VariableNameDeclaration.class);
           for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
             VariableNameDeclaration decl = entry.getKey();
             List<NameOccurrence> occurrences = entry.getValue();
             if (decl.getImage().equals(child.getImage())) {
               if (!isInitCauseCalled(target, occurrences)) {
                 args =
                     decl.getNode().jjtGetParent().getFirstDescendantOfType(ASTArgumentList.class);
                 if (args != null) {
                   ck(data, target, throwStatement, args);
                 }
               }
             }
           }
         } else if (child instanceof ASTClassOrInterfaceType) {
           addViolation(data, throwStatement);
         }
       }
     }
   }
   return super.visit(catchStmt, data);
 }