Search in sources :

Example 6 with Arguments

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

the class OSCommandInjectionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        if (tree.is(Tree.Kind.METHOD_INVOCATION)) {
            MethodInvocationTree mit = (MethodInvocationTree) tree;
            Arguments arguments = mit.arguments();
            if (RUNTIME_EXEC_MATCHER.matches(mit)) {
                checkForIssue(tree, arguments.get(0));
            } else if (PROCESS_BUILDER_COMMAND_MATCHER.matches(mit) && !arguments.isEmpty()) {
                checkForIssue(tree, arguments);
            }
        } else if (((NewClassTree) tree).symbolType().is("java.lang.ProcessBuilder")) {
            checkForIssue(tree, ((NewClassTree) tree).arguments());
        }
    }
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 7 with Arguments

use of org.sonar.plugins.java.api.tree.Arguments 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 8 with Arguments

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

the class FlowComputation method getArgumentIdentifier.

@CheckForNull
public static IdentifierTree getArgumentIdentifier(MethodInvocationTree mit, int index) {
    Arguments arguments = mit.arguments();
    if (index < 0 || index > arguments.size()) {
        throw new IllegalArgumentException("index must be within arguments range.");
    }
    ExpressionTree expr = ExpressionUtils.skipParentheses(arguments.get(index));
    switch(expr.kind()) {
        case MEMBER_SELECT:
            return ((MemberSelectExpressionTree) expr).identifier();
        case IDENTIFIER:
            return ((IdentifierTree) expr);
        default:
            return null;
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) CheckForNull(javax.annotation.CheckForNull)

Example 9 with Arguments

use of org.sonar.plugins.java.api.tree.Arguments 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 10 with Arguments

use of org.sonar.plugins.java.api.tree.Arguments 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)

Aggregations

Arguments (org.sonar.plugins.java.api.tree.Arguments)10 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)8 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)5 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)5 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)4 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)3 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)3 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)2 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)2 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)2 TypeArguments (org.sonar.plugins.java.api.tree.TypeArguments)2 HashSet (java.util.HashSet)1 CheckForNull (javax.annotation.CheckForNull)1 AbstractTypedTree (org.sonar.java.model.AbstractTypedTree)1 JavaSymbol (org.sonar.java.resolve.JavaSymbol)1 MethodJavaType (org.sonar.java.resolve.MethodJavaType)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1