Search in sources :

Example 1 with LambdaExpressionTree

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

the class IndentationCheck method visitLambdaExpression.

@Override
public void visitLambdaExpression(LambdaExpressionTree lambdaExpressionTree) {
    // doesn't scan lambda parameters because there's no indentation check on types and identifiers
    Tree body = lambdaExpressionTree.body();
    if (body.is(Kind.BLOCK)) {
        BlockTree block = (BlockTree) body;
        excludeIssueAtLine = block.openBraceToken().line();
        int previousLevel = expectedLevel;
        expectedLevel = block.closeBraceToken().column();
        scan(block);
        expectedLevel = previousLevel;
    } else {
        scan(body);
    }
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 2 with LambdaExpressionTree

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

the class LambdaOptionalParenthesisCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    LambdaExpressionTree let = (LambdaExpressionTree) tree;
    SyntaxToken openParenToken = let.openParenToken();
    if (openParenToken != null && let.parameters().size() == 1) {
        VariableTree param = let.parameters().get(0);
        String identifier = param.simpleName().name();
        if (param.type().is(Tree.Kind.INFERED_TYPE)) {
            reportIssue(openParenToken, "Remove the parentheses around the \"" + identifier + "\" parameter" + context.getJavaVersion().java8CompatibilityMessage());
        }
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

Example 3 with LambdaExpressionTree

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

the class SymbolTableTest method Lambdas.

@Test
public void Lambdas() throws Exception {
    Result result = Result.createFor("Lambdas");
    JavaSymbol barMethod = result.symbol("bar");
    assertThat(barMethod.usages()).hasSize(1);
    MethodTree methodTree = (MethodTree) barMethod.declaration();
    Type Ftype = result.symbol("F").type();
    assertThat(((ReturnStatementTree) methodTree.block().body().get(0)).expression().symbolType()).isSameAs(Ftype);
    JavaSymbol qixMethod = result.symbol("qix");
    LambdaExpressionTree lamdba = ((LambdaExpressionTree) ((ReturnStatementTree) ((MethodTree) qixMethod.declaration()).block().body().get(0)).expression());
    assertThat(((ReturnStatementTree) ((BlockTree) lamdba.body()).body().get(0)).expression().symbolType()).isSameAs(result.symbol("F2").type());
    JavaSymbol fieldSymbol = result.symbol("field");
    assertThat(((VariableTree) fieldSymbol.declaration()).initializer().symbolType()).isSameAs(fieldSymbol.type());
    assertThat(((AssignmentExpressionTree) fieldSymbol.usages().get(0).parent()).expression().symbolType()).isSameAs(fieldSymbol.type());
    JavaSymbol bSymbol = result.symbol("b");
    assertThat(((NewClassTree) ((VariableTree) bSymbol.declaration()).initializer()).arguments().get(0).symbolType()).isSameAs(Ftype);
    JavaSymbol condMethod = result.symbol("cond");
    ConditionalExpressionTree conditionalExpression = (ConditionalExpressionTree) ((ReturnStatementTree) ((MethodTree) condMethod.declaration()).block().body().get(0)).expression();
    assertThat(conditionalExpression.symbolType()).isSameAs(Ftype);
    JavaSymbol parenthMethod = result.symbol("parenth");
    ParenthesizedTree parenthesizedTree = (ParenthesizedTree) ((ReturnStatementTree) ((MethodTree) parenthMethod.declaration()).block().body().get(0)).expression();
    assertThat(parenthesizedTree.symbolType()).isSameAs(Ftype);
    assertThat(result.symbol("s", 33).type().is("java.lang.String")).isTrue();
    JavaSymbol sym = result.symbol("o");
    assertThat(sym.type.is("java.lang.Object")).isTrue();
    assertThat(result.reference(8, 16)).isEqualTo(result.symbol("v", 8));
    assertThat(result.reference(9, 16)).isEqualTo(result.symbol("v", 9));
    JavaSymbol operations = result.symbol("operations");
    MethodInvocationTree mit = (MethodInvocationTree) operations.usages().get(0).parent().parent();
    assertThat(((ParametrizedTypeJavaType) operations.type).typeSubstitution.substitutedTypes().get(0)).isSameAs(mit.arguments().get(0).symbolType());
    JavaSymbol myStringParam = result.symbol("myStringParam");
    Symbol.MethodSymbol stringParamMethod = (Symbol.MethodSymbol) result.symbol("stringParamMethod");
    assertThat(stringParamMethod.usages()).hasSize(1);
    assertThat(myStringParam.type.is("java.lang.String")).isTrue();
    assertThat(result.symbol("s1").type.is("java.lang.String")).as(result.symbol("s1").type.name()).isTrue();
    assertThat(result.symbol("s2").type.is("java.lang.String")).isTrue();
    assertThat(result.symbol("foo", 95).usages()).hasSize(1);
    assertThat(result.symbol("x", 103).type.is("java.lang.Integer")).as(result.symbol("x", 103).type.name()).isTrue();
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) Type(org.sonar.plugins.java.api.semantic.Type) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) Test(org.junit.Test)

Example 4 with LambdaExpressionTree

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

the class SymbolTableTest method getRSubstitution.

private JavaType getRSubstitution(Result result, String symbolName) {
    LambdaExpressionTree lambda = ((LambdaExpressionTree) result.symbol(symbolName).declaration().parent());
    JavaType lambdaType = (JavaType) lambda.symbolType();
    assertThat(lambdaType.isParameterized()).isTrue();
    assertThat(lambdaType.is("java.util.function.Function")).isTrue();
    TypeSubstitution typeSubstitution = ((ParametrizedTypeJavaType) lambdaType).typeSubstitution;
    assertThat(typeSubstitution.size()).isEqualTo(2);
    JavaType Tsubstitution = typeSubstitution.substitutedTypes().get(0);
    // check that T -> ? super String
    assertThat(Tsubstitution.isTagged(JavaType.WILDCARD)).isTrue();
    assertThat(((WildCardType) Tsubstitution).boundType).isEqualTo(WildCardType.BoundType.SUPER);
    assertThat(((WildCardType) Tsubstitution).bound.is("java.lang.String")).isTrue();
    return typeSubstitution.substitutedTypes().get(1);
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree)

Example 5 with LambdaExpressionTree

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

the class LambdaBlockReturnVisitorTest method test.

@Test
public void test() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A {\n" + "        java.util.function.Consumer<String> c = s -> {\n" + "          if(s.length()>0) {\n" + "            return;\n" + "          }\n" + "          System.out.println(s);\n" + "        };\n" + "\n" + "        java.util.function.Function<String, String> f = s -> {\n" + "          if(s.length() > 0) {\n" + "            return s.replace('a', 'b');\n" + "          }\n" + "          return unknownSymbol;\n" + "        };\n" + "      }");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    List<VariableTree> vars = ((ClassTree) cut.types().get(0)).members().stream().map(m -> (VariableTree) m).collect(Collectors.toList());
    LambdaBlockReturnVisitor visitor = new LambdaBlockReturnVisitor();
    ((LambdaExpressionTree) vars.get(0).initializer()).body().accept(visitor);
    assertThat(visitor.types).isEmpty();
    visitor = new LambdaBlockReturnVisitor();
    ((LambdaExpressionTree) vars.get(1).initializer()).body().accept(visitor);
    assertThat(visitor.types).hasSize(1);
}
Also used : JavaParser(org.sonar.java.ast.parser.JavaParser) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ActionParser(com.sonar.sslr.api.typed.ActionParser) List(java.util.List) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Collections(java.util.Collections) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader) 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