Search in sources :

Example 46 with ExpressionTree

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

the class ArrayForVarArgCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    Symbol sym;
    Arguments args;
    Tree methodName;
    if (tree.is(Tree.Kind.NEW_CLASS)) {
        NewClassTree nct = (NewClassTree) tree;
        sym = nct.constructorSymbol();
        args = nct.arguments();
        methodName = nct.identifier();
    } else {
        MethodInvocationTree mit = (MethodInvocationTree) tree;
        sym = mit.symbol();
        args = mit.arguments();
        methodName = mit.methodSelect();
    }
    if (sym.isMethodSymbol() && !args.isEmpty()) {
        ExpressionTree lastArg = args.get(args.size() - 1);
        JavaSymbol.MethodJavaSymbol methodSymbol = (JavaSymbol.MethodJavaSymbol) sym;
        MethodJavaType methodType = getMethodType(methodSymbol, methodName);
        checkInvokedMethod(methodSymbol, methodType, lastArg);
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Arguments(org.sonar.plugins.java.api.tree.Arguments) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) MethodJavaType(org.sonar.java.resolve.MethodJavaType)

Example 47 with ExpressionTree

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

the class AssertionArgumentOrderCheck method onMethodInvocationFound.

@Override
protected void onMethodInvocationFound(MethodInvocationTree mit) {
    ExpressionTree argToCheck = getActualArgument(mit);
    if (isConstant(argToCheck)) {
        List<JavaFileScannerContext.Location> secondaries = Collections.singletonList(new JavaFileScannerContext.Location("", previousArg(argToCheck, mit)));
        context.reportIssue(this, argToCheck, "Swap these 2 arguments so they are in the correct order: expected value, actual value.", secondaries, null);
    }
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 48 with ExpressionTree

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

the class AssertionArgumentOrderCheck method getActualArgument.

private static ExpressionTree getActualArgument(MethodInvocationTree mit) {
    int arity = mit.arguments().size();
    ExpressionTree arg = mit.arguments().get(arity - 1);
    // Check for assert equals method with delta
    if (arity > 2 && (arity == 4 || ((Symbol.MethodSymbol) mit.symbol()).parameterTypes().stream().allMatch(AssertionArgumentOrderCheck::isDoubleOrFloat))) {
        // last arg is actually delta, take the previous last to get the actual arg.
        arg = mit.arguments().get(arity - 2);
    }
    return arg;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)

Example 49 with ExpressionTree

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

the class PublicStaticMutableMembersCheck method checkAssignment.

private void checkAssignment(AssignmentExpressionTree node) {
    ExpressionTree variable = ExpressionUtils.skipParentheses(node.variable());
    if (variable.is(Tree.Kind.MEMBER_SELECT)) {
        variable = ((MemberSelectExpressionTree) variable).identifier();
    }
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifierTree = (IdentifierTree) variable;
        Symbol symbol = identifierTree.symbol();
        if (IMMUTABLE_CANDIDATES.contains(symbol) && isMutable(node.expression(), symbol)) {
            reportIssue(identifierTree, "Make member \"" + symbol.name() + "\" \"protected\".");
            IMMUTABLE_CANDIDATES.remove(symbol);
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 50 with ExpressionTree

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

the class PreferStreamAnyMatchCheck method handleAnyMatch.

private void handleAnyMatch(MethodInvocationTree anyMatchMIT) {
    ExpressionTree predicate = anyMatchMIT.arguments().get(0);
    IdentifierTree reportTree = ExpressionUtils.methodName(anyMatchMIT);
    if (anyMatchMIT.parent().is(Tree.Kind.LOGICAL_COMPLEMENT)) {
        if (predicate.is(Tree.Kind.LAMBDA_EXPRESSION) && ((LambdaExpressionTree) predicate).body().is(Tree.Kind.LOGICAL_COMPLEMENT)) {
            // !stream.anyMatch(x -> !(...))
            context.reportIssue(this, reportTree, "Replace this double negation with \"allMatch()\" and positive predicate.");
        } else {
            context.reportIssue(this, reportTree, "Replace this negation and \"anyMatch()\" with \"noneMatch()\".");
        }
    }
    if (predicate.is(Tree.Kind.METHOD_REFERENCE) && isBooleanValueReference((MethodReferenceTree) predicate)) {
        previousMITInChain(anyMatchMIT).filter(MAP_METHODS::anyMatch).ifPresent(mapMIT -> context.reportIssue(this, reportTree, "Use mapper from \"map()\" directly as predicate in \"anyMatch()\"."));
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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