Search in sources :

Example 76 with MethodInvocationTree

use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.

the class IndexOfStartPositionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ExpressionTree leftOperand = ExpressionUtils.skipParentheses(((BinaryExpressionTree) tree).leftOperand());
    ExpressionTree rightOperand = ExpressionUtils.skipParentheses(((BinaryExpressionTree) tree).rightOperand());
    if (leftOperand.is(Tree.Kind.METHOD_INVOCATION)) {
        checkIndexOfInvocation((MethodInvocationTree) leftOperand, rightOperand);
    } else if (rightOperand.is(Tree.Kind.METHOD_INVOCATION)) {
        checkIndexOfInvocation((MethodInvocationTree) rightOperand, leftOperand);
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 77 with MethodInvocationTree

use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.

the class DuplicateArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodInvocationTree mit = (MethodInvocationTree) tree;
    Arguments arguments = mit.arguments();
    int arity = arguments.size();
    if (arity <= 1) {
        return;
    }
    Set<ExpressionTree> reported = new HashSet<>();
    for (int i = 0; i < arity; i++) {
        ExpressionTree arg = ExpressionUtils.skipParentheses(arguments.get(i));
        if (isLiteral(arg) || arg.is(Tree.Kind.IDENTIFIER) || arg.is(Tree.Kind.NEW_CLASS)) {
            continue;
        }
        for (int j = i + 1; j < arity; j++) {
            ExpressionTree otherArg = ExpressionUtils.skipParentheses(arguments.get(j));
            if (!reported.contains(otherArg) && SyntacticEquivalence.areEquivalent(arg, otherArg)) {
                reportIssue(otherArg, String.format("Verify that this is the intended value; it is the same as the %s argument.", argumentNumber(i + 1)), Collections.singletonList(new JavaFileScannerContext.Location("", arg)), null);
                reported.add(otherArg);
            }
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) HashSet(java.util.HashSet)

Example 78 with MethodInvocationTree

use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.

the class UnusedReturnedDataCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        CHECKED_METHODS.stream().map(matcher -> isTreeMethodInvocation(((ExpressionStatementTree) tree).expression(), matcher)).filter(Objects::nonNull).forEach(mit -> raiseIssue(ExpressionUtils.methodName(mit)));
    } else {
        BinaryExpressionTree expressionTree = (BinaryExpressionTree) tree;
        ExpressionTree leftOperand = expressionTree.leftOperand();
        ExpressionTree rightOperand = expressionTree.rightOperand();
        for (MethodMatcher matcher : CHECKED_METHODS) {
            MethodInvocationTree leftMit = isTreeMethodInvocation(leftOperand, matcher);
            if (leftMit != null && isTreeLiteralNull(rightOperand)) {
                raiseIssue(ExpressionUtils.methodName(leftMit));
            }
            MethodInvocationTree rightMit = isTreeMethodInvocation(rightOperand, matcher);
            if (rightMit != null && isTreeLiteralNull(leftOperand)) {
                raiseIssue(ExpressionUtils.methodName(rightMit));
            }
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) MethodMatcher(org.sonar.java.matcher.MethodMatcher)

Example 79 with MethodInvocationTree

use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.

the class WeakSSLContextCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        Arguments arguments = mit.arguments();
        if (SSLCONTEXT_GETINSTANCE_MATCHER.matches(mit)) {
            ExpressionTree firstArgument = arguments.get(0);
            if (firstArgument.is(Tree.Kind.STRING_LITERAL) && !STRONG_PROTOCOLS.contains(trimQuotes(((LiteralTree) firstArgument).value()))) {
                reportIssue(firstArgument, "Change this code to use a stronger protocol.");
            }
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 80 with MethodInvocationTree

use of org.sonar.plugins.java.api.tree.MethodInvocationTree in project sonar-java by SonarSource.

the class UCFGJavaVisitor method buildCall.

private void buildCall(Tree element, UCFGBuilder.BlockBuilder blockBuilder, IdentifierGenerator idGenerator) {
    if (isStringVarDecl(element)) {
        VariableTree variableTree = (VariableTree) element;
        String lhs = idGenerator.lookupIdFor(variableTree.simpleName());
        if (!idGenerator.isConst(lhs)) {
            ExpressionTree initializer = variableTree.initializer();
            String source = idGenerator.lookupIdFor(initializer);
            blockBuilder.assignTo(variableWithId(lhs), UCFGBuilder.call("__id").withArgs(variableWithId(source)), location(element));
        }
        return;
    }
    if (element.is(METHOD_INVOCATION)) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) element;
        buildMethodInvocation(blockBuilder, idGenerator, methodInvocationTree);
    } else if (element.is(PLUS, PLUS_ASSIGNMENT, ASSIGNMENT) && isString(((ExpressionTree) element).symbolType())) {
        if (element.is(PLUS)) {
            BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) element;
            Expression lhs = idGenerator.lookupExpressionFor(binaryExpressionTree.leftOperand());
            Expression rhs = idGenerator.lookupExpressionFor(binaryExpressionTree.rightOperand());
            Expression.Variable var = variableWithId(idGenerator.newIdFor(binaryExpressionTree));
            blockBuilder.assignTo(var, call("__concat").withArgs(lhs, rhs), location(element));
        } else if (element.is(PLUS_ASSIGNMENT)) {
            Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
            Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
            if (!var.isConstant()) {
                idGenerator.varForExpression(element, ((Expression.Variable) var).id());
                blockBuilder.assignTo((Expression.Variable) var, call("__concat").withArgs(var, expr), location(element));
            }
        } else if (element.is(ASSIGNMENT)) {
            Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
            Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
            if (!var.isConstant()) {
                blockBuilder.assignTo((Expression.Variable) var, call("__id").withArgs(expr), location(element));
            }
        }
    }
}
Also used : Expression(org.sonar.ucfg.Expression) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Aggregations

MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)87 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)44 Test (org.junit.Test)30 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)30 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 Symbol (org.sonar.plugins.java.api.semantic.Symbol)26 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)23 Tree (org.sonar.plugins.java.api.tree.Tree)21 Type (org.sonar.plugins.java.api.semantic.Type)14 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)14 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)14 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)13 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)10 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)10 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)9 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)8 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)8 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)7 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)7