@Override
 protected void onMethodInvocationFound(MethodInvocationTree mit) {
   Type symbolType = mit.arguments().get(0).symbolType();
   if (!(symbolType.is("long") || symbolType.is("java.lang.Long"))) {
     reportIssue(MethodsHelper.methodName(mit), "Remove this \"Double.longBitsToDouble\" call.");
   }
 }
 @Override
 public void visitMethodInvocation(MethodInvocationTree syntaxNode) {
   final ExpressionTree methodSelect = syntaxNode.methodSelect();
   if (isClosingResource(syntaxNode.symbol())) {
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       final ExpressionTree targetExpression =
           ((MemberSelectExpressionTree) methodSelect).expression();
       if (targetExpression.is(Tree.Kind.IDENTIFIER)) {
         final IdentifierTree identifier = (IdentifierTree) targetExpression;
         programState = closeResource(programState, programState.getValue(identifier.symbol()));
       } else {
         programState = closeResource(programState, programState.peekValue());
       }
     }
   } else if (syntaxNode.methodSelect().is(Tree.Kind.MEMBER_SELECT)
       && isOpeningResultSet(syntaxNode.symbol())) {
     final SymbolicValue value = getTargetValue(syntaxNode);
     constraintManager.setValueFactory(new WrappedValueFactory(value));
   } else if (isClosingResultSets(syntaxNode.symbol())) {
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       final SymbolicValue value = getTargetValue(syntaxNode);
       closeResultSetsRelatedTo(value);
     }
   } else {
     closeArguments(syntaxNode.arguments(), 1);
   }
 }
Example #3
0
 private void buildMethodInvocation(MethodInvocationTree tree) {
   MethodInvocationTree mit = tree;
   currentBlock.elements.add(mit);
   build(mit.methodSelect());
   for (ExpressionTree arg : Lists.reverse(mit.arguments())) {
     build(arg);
   }
 }
 private void checkMethodInvocationArguments(
     MethodInvocationTree methodInvocationTree, List<Type> parametersTypes) {
   List<ExpressionTree> arguments = methodInvocationTree.arguments();
   int position = 0;
   for (Type paramType : parametersTypes) {
     if (arguments.size() > position) {
       checkExpression(arguments.get(position), paramType);
     }
     position++;
   }
 }
  @Test
  public void ignore_assignation_after_starting_point_same_line() throws Exception {
    String code = newCode("int foo() {", "  int b = 0;", "  doSomething(b); b = 1;", "}");

    List<StatementTree> statements = methodBody(code);
    Tree expectedVariableDeclaration =
        initializerFromVariableDeclarationStatement(statements.get(0));
    MethodInvocationTree startingPoint =
        (MethodInvocationTree) ((ExpressionStatementTree) statements.get(1)).expression();
    Symbol searchedVariable = ((IdentifierTree) startingPoint.arguments().get(0)).symbol();
    assertThatLastReassignmentsOfVariableIsEqualTo(
        searchedVariable, startingPoint, expectedVariableDeclaration);
  }
 private static boolean isEqualsMethod(MethodInvocationTree syntaxNode) {
   if (syntaxNode.arguments().size() == 1) {
     ExpressionTree methodSelect = syntaxNode.methodSelect();
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       MemberSelectExpressionTree expression = (MemberSelectExpressionTree) methodSelect;
       if ("equals".equals(expression.identifier().name())
           && syntaxNode.symbol().isMethodSymbol()) {
         Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) syntaxNode.symbol();
         return symbol.parameterTypes().get(0).is("java.lang.Object");
       }
     }
   }
   return false;
 }
  @Override
  protected void onMethodInvocationFound(MethodInvocationTree tree) {
    Type argumentType = tree.arguments().get(0).symbolType();
    Type collectionType = getMethodOwner(tree);
    // can be null when using raw types
    Type collectionParameterType = getTypeParameter(collectionType);

    if (collectionParameterType != null
        && !collectionParameterType.isUnknown()
        && !isArgumentCompatible(argumentType, collectionParameterType)) {
      addIssue(
          tree,
          MessageFormat.format(
              "A \"{0}<{1}>\" cannot contain a \"{2}\"",
              collectionType, collectionParameterType, argumentType));
    }
  }
 private void checkForBoxing(ExpressionTree expression) {
   if (expression.is(Tree.Kind.NEW_CLASS)) {
     NewClassTree newClassTree = (NewClassTree) expression;
     Symbol.TypeSymbol classSymbol = wrapperClassSymbol(newClassTree);
     if (classSymbol != null) {
       ExpressionTree boxingArg = newClassTree.arguments().get(0);
       if (boxingArg.symbolType().isPrimitive()) {
         addBoxingIssue(newClassTree, classSymbol, boxingArg);
       }
     }
   } else if (expression.is(Tree.Kind.METHOD_INVOCATION)) {
     MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expression;
     if (isValueOfInvocation(methodInvocationTree)) {
       ExpressionTree boxingArg = methodInvocationTree.arguments().get(0);
       addBoxingIssue(expression, methodInvocationTree.symbol().owner(), boxingArg);
     }
   }
 }
 private void visitMethodInvocationTree(MethodInvocationTree methodInvocationTree) {
   if (isValueOfInvocation(methodInvocationTree)) {
     checkForUnboxing(methodInvocationTree.arguments().get(0));
   } else if (isUnboxingMethodInvocation(methodInvocationTree)) {
     ExpressionTree methodSelect = methodInvocationTree.methodSelect();
     if (methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
       MemberSelectExpressionTree memberSelectExpressionTree =
           (MemberSelectExpressionTree) methodSelect;
       checkForBoxing(memberSelectExpressionTree.expression());
     }
   } else {
     Symbol symbol = methodInvocationTree.symbol();
     if (symbol.isMethodSymbol()) {
       List<Type> parametersTypes = ((Symbol.MethodSymbol) symbol).parameterTypes();
       checkMethodInvocationArguments(methodInvocationTree, parametersTypes);
     }
   }
 }
 @Override
 public void visitNode(Tree tree) {
   MethodInvocationTree methodTree = (MethodInvocationTree) tree;
   boolean isHibernateCall = isHibernateCall(methodTree);
   if (isHibernateCall
       || isExecuteQueryOrPrepareStatement(methodTree)
       || isEntityManagerCreateNativeQuery(methodTree)) {
     // We want to check the argument for the three methods.
     ExpressionTree arg = methodTree.arguments().get(0);
     parameterName = "";
     if (isDynamicString(methodTree, arg, null, true)) {
       String message =
           "\""
               + parameterName
               + "\" is provided externally to the method and not sanitized before use.";
       if (isHibernateCall) {
         message = "Use Hibernate's parameter binding instead of concatenation.";
       }
       reportIssue(MethodsHelper.methodName(methodTree), message);
     }
   }
 }
 private static boolean isExecuteQueryOrPrepareStatement(MethodInvocationTree methodTree) {
   return !methodTree.arguments().isEmpty()
       && (STATEMENT_EXECUTE_QUERY_MATCHER.matches(methodTree)
           || CONNECTION_MATCHERS.anyMatch(methodTree));
 }