Search in sources :

Example 51 with ExpressionTree

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

the class PrimitiveTypeBoxingWithToStringCheck method visitMethodInvocation.

@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    if (TO_STRING_MATCHERS.anyMatch(tree)) {
        ExpressionTree abstractTypedTree = ((MemberSelectExpressionTree) tree.methodSelect()).expression();
        if (abstractTypedTree.is(Kind.NEW_CLASS) || isValueOfInvocation(abstractTypedTree)) {
            String typeName = abstractTypedTree.symbolType().toString();
            createIssue(tree, typeName);
        }
    }
    super.visitMethodInvocation(tree);
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 52 with ExpressionTree

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

the class PrintfMisuseCheck method verifyParameters.

private void verifyParameters(MethodInvocationTree mit, List<ExpressionTree> args, List<String> params) {
    int index = 0;
    List<ExpressionTree> unusedArgs = new ArrayList<>(args);
    for (String rawParam : params) {
        String param = rawParam;
        int argIndex = index;
        if (param.contains("$")) {
            argIndex = getIndex(param) - 1;
            if (argIndex == -1) {
                return;
            }
            param = param.substring(param.indexOf('$') + 1);
        } else if (param.charAt(0) == '<') {
            // refers to previous argument
            argIndex = Math.max(0, argIndex - 1);
        } else {
            index++;
        }
        ExpressionTree argExpressionTree = args.get(argIndex);
        unusedArgs.remove(argExpressionTree);
        Type argType = argExpressionTree.symbolType();
        checkBoolean(mit, param, argType);
    }
    reportUnusedArgs(mit, args, unusedArgs);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ArrayList(java.util.ArrayList) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 53 with ExpressionTree

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

the class PrintfMisuseCheck method checkFormatting.

private void checkFormatting(MethodInvocationTree mit, boolean isMessageFormat) {
    if (mit.arguments().stream().map(ExpressionTree::symbolType).anyMatch(Type::isUnknown)) {
        // method resolved but not all the parameters are
        return;
    }
    ExpressionTree formatStringTree;
    List<ExpressionTree> args;
    // Check type of first argument:
    if (mit.arguments().get(0).symbolType().is("java.lang.String")) {
        formatStringTree = mit.arguments().get(0);
        args = mit.arguments().subList(1, mit.arguments().size());
    } else {
        // format method with "Locale" first argument, skip that one.
        formatStringTree = mit.arguments().get(1);
        args = mit.arguments().subList(2, mit.arguments().size());
    }
    if (formatStringTree.is(Tree.Kind.STRING_LITERAL)) {
        String formatString = LiteralUtils.trimQuotes(((LiteralTree) formatStringTree).value());
        if (isMessageFormat) {
            handleMessageFormat(mit, formatString, args);
        } else {
            handlePrintfFormat(mit, formatString, args);
        }
    } else if (isConcatenationOnSameLine(formatStringTree)) {
        reportIssue(mit, "Format specifiers should be used instead of string concatenation.");
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 54 with ExpressionTree

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

the class PrintfMisuseCheck method reportUnusedArgs.

private void reportUnusedArgs(MethodInvocationTree mit, List<ExpressionTree> args, List<ExpressionTree> unusedArgs) {
    for (ExpressionTree unusedArg : unusedArgs) {
        int i = args.indexOf(unusedArg);
        String stringArgIndex = "first";
        if (i == 1) {
            stringArgIndex = "2nd";
        } else if (i == 2) {
            stringArgIndex = "3rd";
        } else if (i >= 3) {
            stringArgIndex = (i + 1) + "th";
        }
        reportIssue(mit, stringArgIndex + " argument is not used.");
    }
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 55 with ExpressionTree

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

the class NullCheckWithInstanceofCheck method nullCheckWithInstanceOf.

private static boolean nullCheckWithInstanceOf(ExpressionTree leftOp, ExpressionTree rightOp, Tree.Kind binaryExpressionKind, Tree.Kind expectedKind) {
    ExpressionTree binaryVariable = Optional.ofNullable(binaryExpressionVariable(leftOp)).orElse(binaryExpressionVariable(rightOp));
    if (binaryVariable == null || binaryExpressionKind != expectedKind) {
        return false;
    }
    ExpressionTree instanceofVariable = Optional.ofNullable(instanceofFound(rightOp, binaryExpressionKind)).orElse(instanceofFound(leftOp, binaryExpressionKind));
    return instanceofVariable != null && SyntacticEquivalence.areEquivalent(binaryVariable, instanceofVariable);
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Aggregations

ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)189 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)88 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)80 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)66 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)50 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)44 UnaryExpressionTree (org.sonar.plugins.java.api.tree.UnaryExpressionTree)35 Symbol (org.sonar.plugins.java.api.semantic.Symbol)31 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)30 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)30 Test (org.junit.Test)25 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)24 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)23 Type (org.sonar.plugins.java.api.semantic.Type)21 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)20 Tree (org.sonar.plugins.java.api.tree.Tree)20 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)17 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)14 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)13