Search in sources :

Example 11 with LambdaExpressionTree

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

the class TypeAndReferenceSolver method visitReturnStatement.

@Override
public void visitReturnStatement(ReturnStatementTree tree) {
    super.visitReturnStatement(tree);
    ExpressionTree expression = tree.expression();
    if (expression != null && ((JavaType) expression.symbolType()).isTagged(JavaType.DEFERRED)) {
        // get owner of return (method or lambda)
        Tree parent = tree.parent();
        while (!parent.is(Tree.Kind.METHOD, Tree.Kind.LAMBDA_EXPRESSION)) {
            parent = parent.parent();
            if (parent == null) {
                throw new IllegalStateException("Return statement was unexpected here");
            }
        }
        Type infered;
        if (parent.is(Tree.Kind.METHOD)) {
            infered = ((MethodTree) parent).returnType().symbolType();
        } else {
            infered = ((LambdaExpressionTree) parent).symbolType();
        }
        setInferedType(infered, (DeferredType) expression.symbolType());
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) 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) AbstractTypedTree(org.sonar.java.model.AbstractTypedTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayTypeTree(org.sonar.plugins.java.api.tree.ArrayTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) ContinueStatementTree(org.sonar.plugins.java.api.tree.ContinueStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) WildcardTree(org.sonar.plugins.java.api.tree.WildcardTree) LabeledStatementTree(org.sonar.plugins.java.api.tree.LabeledStatementTree) BreakStatementTree(org.sonar.plugins.java.api.tree.BreakStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) EnumConstantTree(org.sonar.plugins.java.api.tree.EnumConstantTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ImportTree(org.sonar.plugins.java.api.tree.ImportTree) PrimitiveTypeTree(org.sonar.plugins.java.api.tree.PrimitiveTypeTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) InstanceOfTree(org.sonar.plugins.java.api.tree.InstanceOfTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 12 with LambdaExpressionTree

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

the class LambdaSingleExpressionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) tree;
    Tree lambdaBody = lambdaExpressionTree.body();
    if (isBlockWithOneStatement(lambdaBody)) {
        String message = "Remove useless curly braces around statement";
        if (singleStatementIsReturn(lambdaExpressionTree)) {
            message += " and then remove useless return keyword";
        }
        reportIssue(((BlockTree) lambdaBody).openBraceToken(), message + context.getJavaVersion().java8CompatibilityMessage());
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree)

Example 13 with LambdaExpressionTree

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

the class LambdaTypeParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    LambdaExpressionTree lambdaExpressionTree = (LambdaExpressionTree) tree;
    List<VariableTree> parameters = lambdaExpressionTree.parameters();
    if (parameters.size() <= 2 && !lambdaExpressionTree.body().is(Tree.Kind.BLOCK)) {
        // ignore lambdas with one or two params and a non-block body
        return;
    }
    String missingTypeParameters = parameters.stream().filter(variable -> variable.type().is(Tree.Kind.INFERED_TYPE)).map(VariableTree::simpleName).map(IdentifierTree::name).map(parameterName -> "'" + parameterName + "'").collect(Collectors.joining(", "));
    if (!missingTypeParameters.isEmpty()) {
        reportIssue(parameters.get(0), Iterables.getLast(parameters), String.format("Specify a type for: %s", missingTypeParameters));
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) List(java.util.List) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Tree(org.sonar.plugins.java.api.tree.Tree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collectors(java.util.stream.Collectors) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 14 with LambdaExpressionTree

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

the class LiveVariables method processBlockElements.

private void processBlockElements(CFG.Block block, Set<Symbol> blockKill, Set<Symbol> blockGen) {
    // process elements from bottom to top
    Set<Tree> assignmentLHS = new HashSet<>();
    for (Tree element : Lists.reverse(block.elements())) {
        switch(element.kind()) {
            case ASSIGNMENT:
                processAssignment((AssignmentExpressionTree) element, blockKill, blockGen, assignmentLHS);
                break;
            case IDENTIFIER:
                processIdentifier((IdentifierTree) element, blockGen, assignmentLHS);
                break;
            case MEMBER_SELECT:
                processMemberSelect((MemberSelectExpressionTree) element, assignmentLHS, blockGen);
                break;
            case VARIABLE:
                blockKill.add(((VariableTree) element).symbol());
                blockGen.remove(((VariableTree) element).symbol());
                break;
            case LAMBDA_EXPRESSION:
                blockGen.addAll(getUsedVariables(((LambdaExpressionTree) element).body(), cfg.methodSymbol()));
                break;
            case METHOD_REFERENCE:
                blockGen.addAll(getUsedVariables(((MethodReferenceTree) element).expression(), cfg.methodSymbol()));
                break;
            case NEW_CLASS:
                blockGen.addAll(getUsedVariables(((NewClassTree) element).classBody(), cfg.methodSymbol()));
                break;
            default:
        }
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) HashSet(java.util.HashSet)

Example 15 with LambdaExpressionTree

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

the class SymbolTableTest method double_lambda_type_propagation.

@Test
public void double_lambda_type_propagation() {
    Result result = Result.createFor("DoubleLambda");
    JavaSymbol my = result.symbol("my");
    assertThat(my.usages()).hasSize(2);
    LambdaExpressionTree outerLambda = ((LambdaExpressionTree) result.symbol("l").declaration().parent());
    Type nestedLambdaType = ((ExpressionTree) outerLambda.body()).symbolType();
    assertThat(nestedLambdaType.is("Issue$Flow")).as("Issue$Flow was expected but got : " + nestedLambdaType.fullyQualifiedName()).isTrue();
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) 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