Search in sources :

Example 6 with LambdaExpressionTree

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

Example 7 with LambdaExpressionTree

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

the class MethodWithExcessiveReturnsCheck method leaveNode.

@Override
public void leaveNode(Tree tree) {
    Tree reportTree = null;
    if (tree.is(Tree.Kind.METHOD)) {
        reportTree = ((MethodTree) tree).simpleName();
    } else if (tree.is(Tree.Kind.LAMBDA_EXPRESSION)) {
        reportTree = ((LambdaExpressionTree) tree).arrowToken();
    }
    if (reportTree != null) {
        int count = returnStatementCounter.count(tree);
        if (count > max) {
            reportIssue(reportTree, "Reduce the number of returns of this method " + count + ", down to the maximum allowed " + max + ".");
        }
        methods.pop();
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 8 with LambdaExpressionTree

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

the class DeadStoreCheck method checkElement.

private Set<Symbol> checkElement(Symbol.MethodSymbol methodSymbol, Set<Symbol> outVar, Set<Tree> assignmentLHS, Tree element) {
    Set<Symbol> out = outVar;
    switch(element.kind()) {
        case PLUS_ASSIGNMENT:
        case DIVIDE_ASSIGNMENT:
        case MINUS_ASSIGNMENT:
        case MULTIPLY_ASSIGNMENT:
        case OR_ASSIGNMENT:
        case XOR_ASSIGNMENT:
        case AND_ASSIGNMENT:
        case LEFT_SHIFT_ASSIGNMENT:
        case RIGHT_SHIFT_ASSIGNMENT:
        case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
        case REMAINDER_ASSIGNMENT:
        case ASSIGNMENT:
            handleAssignment(out, assignmentLHS, (AssignmentExpressionTree) element);
            break;
        case IDENTIFIER:
            handleIdentifier(out, assignmentLHS, (IdentifierTree) element);
            break;
        case VARIABLE:
            handleVariable(out, (VariableTree) element);
            break;
        case NEW_CLASS:
            handleNewClass(out, methodSymbol, (NewClassTree) element);
            break;
        case LAMBDA_EXPRESSION:
            LambdaExpressionTree lambda = (LambdaExpressionTree) element;
            out.addAll(getUsedLocalVarInSubTree(lambda.body(), methodSymbol));
            break;
        case METHOD_REFERENCE:
            MethodReferenceTree methodRef = (MethodReferenceTree) element;
            out.addAll(getUsedLocalVarInSubTree(methodRef.expression(), methodSymbol));
            break;
        case TRY_STATEMENT:
            handleTryStatement(out, methodSymbol, (TryStatementTree) element);
            break;
        case PREFIX_DECREMENT:
        case PREFIX_INCREMENT:
            handlePrefixExpression(out, (UnaryExpressionTree) element);
            break;
        case POSTFIX_INCREMENT:
        case POSTFIX_DECREMENT:
            handlePostfixExpression(out, (UnaryExpressionTree) element);
            break;
        case CLASS:
        case ENUM:
        case ANNOTATION_TYPE:
        case INTERFACE:
            ClassTree classTree = (ClassTree) element;
            out.addAll(getUsedLocalVarInSubTree(classTree, methodSymbol));
            break;
        default:
    }
    return out;
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree)

Example 9 with LambdaExpressionTree

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

the class ReplaceLambdaByMethodRefCheck method getNullCheck.

private static Optional<String> getNullCheck(@Nullable Tree statement, LambdaExpressionTree tree) {
    if (statement == null) {
        return Optional.empty();
    }
    Tree expr = statement;
    if (expr.is(Tree.Kind.PARENTHESIZED_EXPRESSION)) {
        expr = ExpressionUtils.skipParentheses((ParenthesizedTree) statement);
    }
    if (expr.is(Tree.Kind.EQUAL_TO, Tree.Kind.NOT_EQUAL_TO)) {
        BinaryExpressionTree bet = (BinaryExpressionTree) expr;
        ExpressionTree leftOperand = ExpressionUtils.skipParentheses(bet.leftOperand());
        ExpressionTree rightOperand = ExpressionUtils.skipParentheses(bet.rightOperand());
        if (nullAgainstParam(leftOperand, rightOperand, tree) || nullAgainstParam(rightOperand, leftOperand, tree)) {
            return Optional.of(expr.is(Tree.Kind.EQUAL_TO) ? "isNull" : "nonNull");
        }
    }
    return Optional.empty();
}
Also used : BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree)

Example 10 with LambdaExpressionTree

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

the class JavaTreeModelTest method lambda_expressions.

@Test
public void lambda_expressions() {
    String code = "class T { public void meth(){IntStream.range(1,12).map(x->x*x).map((int a)-> {return a*a;});}}";
    ExpressionTree expressionTree = ((ExpressionStatementTree) firstMethodFirstStatement(code)).expression();
    // parsing not broken by lambda
    assertThat(expressionTree).isNotNull();
    MethodInvocationTree mit = (MethodInvocationTree) expressionTree;
    LambdaExpressionTree tree = (LambdaExpressionTree) mit.arguments().get(0);
    assertThat(tree.openParenToken()).isNotNull();
    assertThat(tree.parameters()).hasSize(1);
    assertThat(tree.parameters().get(0).is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.closeParenToken()).isNotNull();
    assertThat(tree.arrowToken()).isNotNull();
    assertThat(tree.body().is(Tree.Kind.BLOCK)).isTrue();
    tree = (LambdaExpressionTree) ((MethodInvocationTree) ((MemberSelectExpressionTree) mit.methodSelect()).expression()).arguments().get(0);
    assertThat(tree.openParenToken()).isNull();
    assertThat(tree.parameters()).hasSize(1);
    assertThat(tree.parameters().get(0).is(Tree.Kind.VARIABLE)).isTrue();
    assertThat(tree.closeParenToken()).isNull();
    assertThat(tree.arrowToken()).isNotNull();
    assertThat(tree.body().is(Tree.Kind.MULTIPLY)).isTrue();
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) Test(org.junit.Test)

Aggregations

LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)16 Tree (org.sonar.plugins.java.api.tree.Tree)8 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)6 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)6 Test (org.junit.Test)5 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)5 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)5 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)5 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)5 Type (org.sonar.plugins.java.api.semantic.Type)4 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)4 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)4 MethodReferenceTree (org.sonar.plugins.java.api.tree.MethodReferenceTree)4 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)3 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)3 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)3 ParenthesizedTree (org.sonar.plugins.java.api.tree.ParenthesizedTree)3 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)3