Search in sources :

Example 1 with ParenthesizedTree

use of org.sonar.plugins.java.api.tree.ParenthesizedTree 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 2 with ParenthesizedTree

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

the class JavaTreeModelTest method parenthesized_expression.

/**
 * 15.8.5. Parenthesized Expressions
 */
@Test
public void parenthesized_expression() {
    ParenthesizedTree tree = (ParenthesizedTree) expressionOfReturnStatement("class T { boolean m() { return (true); } }");
    assertThat(tree.is(Tree.Kind.PARENTHESIZED_EXPRESSION)).isTrue();
    assertThat(tree.openParenToken().text()).isEqualTo("(");
    assertThat(tree.expression()).isNotNull();
    assertThat(tree.closeParenToken().text()).isEqualTo(")");
    assertThatChildrenIteratorHasSize(tree, 3);
}
Also used : ParenthesizedTree(org.sonar.plugins.java.api.tree.ParenthesizedTree) Test(org.junit.Test)

Example 3 with ParenthesizedTree

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

Aggregations

ParenthesizedTree (org.sonar.plugins.java.api.tree.ParenthesizedTree)3 Test (org.junit.Test)2 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)2 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)2 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 Type (org.sonar.plugins.java.api.semantic.Type)1 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)1 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)1 ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)1 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1