Search in sources :

Example 31 with MethodInvocationTree

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

the class ReplaceLambdaByMethodRefCheck method isMethodInvocation.

private static boolean isMethodInvocation(@Nullable Tree tree, LambdaExpressionTree lambdaTree) {
    if (tree != null && tree.is(Tree.Kind.METHOD_INVOCATION, Tree.Kind.NEW_CLASS)) {
        Arguments arguments;
        if (tree.is(Tree.Kind.NEW_CLASS)) {
            if (((NewClassTree) tree).classBody() != null) {
                return false;
            }
            arguments = ((NewClassTree) tree).arguments();
        } else {
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            if (hasMethodInvocationInMethodSelect(mit) || hasNonFinalFieldInMethodSelect(mit)) {
                return false;
            }
            arguments = mit.arguments();
        }
        List<VariableTree> parameters = lambdaTree.parameters();
        return matchingParameters(parameters, arguments) || (arguments.isEmpty() && isNoArgMethodInvocationFromLambdaParam(tree, parameters));
    }
    return false;
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 32 with MethodInvocationTree

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

the class SwitchInsteadOfIfSequenceCheck method getEqualMethodInvocationOperands.

private static Optional<EqualsOperands> getEqualMethodInvocationOperands(ExpressionTree expressionTree) {
    ExpressionTree arg = null;
    ExpressionTree expression = null;
    if (expressionTree.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
        Symbol symbol = mit.symbol();
        ExpressionTree methodSelect = mit.methodSelect();
        if (mit.arguments().size() == 1) {
            arg = mit.arguments().get(0);
            if ("equals".equals(symbol.name()) && arg.symbolType().is("java.lang.String") && methodSelect.is(Tree.Kind.MEMBER_SELECT)) {
                expression = ((MemberSelectExpressionTree) methodSelect).expression();
            }
        }
    } else if (expressionTree.is(Tree.Kind.EQUAL_TO)) {
        BinaryExpressionTree equalTo = (BinaryExpressionTree) expressionTree;
        arg = equalTo.leftOperand();
        expression = equalTo.rightOperand();
    }
    if (arg != null && expression != null) {
        if (arg.is(Tree.Kind.STRING_LITERAL) && expression.is(Tree.Kind.IDENTIFIER)) {
            return Optional.of(new EqualsOperands((LiteralTree) arg, (IdentifierTree) expression));
        } else if (arg.is(Tree.Kind.IDENTIFIER) && expression.is(Tree.Kind.STRING_LITERAL)) {
            return Optional.of(new EqualsOperands((LiteralTree) expression, (IdentifierTree) arg));
        }
    }
    return Optional.empty();
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree)

Example 33 with MethodInvocationTree

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

the class ToStringUsingBoxingCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodInvocationTree mit = (MethodInvocationTree) tree;
    String callingToStringOrCompareTo = isCallingToStringOrCompareTo(mit.methodSelect());
    if (callingToStringOrCompareTo != null) {
        String newlyCreatedClassName = getNewlyCreatedClassName(mit);
        if (PRIMITIVE_WRAPPERS.contains(newlyCreatedClassName)) {
            reportIssue(((MemberSelectExpressionTree) mit.methodSelect()).expression(), "Call the static method " + newlyCreatedClassName + "." + callingToStringOrCompareTo + "(...) instead of instantiating a temporary object to perform this to string conversion.");
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree)

Example 34 with MethodInvocationTree

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

the class ThreadAsRunnableArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    List<ExpressionTree> arguments;
    Symbol methodSymbol;
    if (tree.is(Kind.NEW_CLASS)) {
        NewClassTree nct = (NewClassTree) tree;
        methodSymbol = nct.constructorSymbol();
        arguments = nct.arguments();
    } else {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        methodSymbol = mit.symbol();
        arguments = mit.arguments();
    }
    if (!arguments.isEmpty() && methodSymbol.isMethodSymbol()) {
        checkArgumentsTypes(arguments, (MethodJavaSymbol) methodSymbol);
    }
}
Also used : MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 35 with MethodInvocationTree

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

the class SecureCookieCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.VARIABLE)) {
            VariableTree variableTree = (VariableTree) tree;
            addToUnsecuredCookies(variableTree);
        } else if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            checkSecureCall(mit);
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

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