Search in sources :

Example 1 with ConditionalExpressionTree

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

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

the class JavaTreeModelTest method conditional_expression.

/**
 * 15.25. Conditional Operator ? :
 */
@Test
public void conditional_expression() {
    ConditionalExpressionTree tree;
    tree = (ConditionalExpressionTree) expressionOfReturnStatement("class T { boolean m() { return true ? true : false; } }");
    assertThat(tree.is(Tree.Kind.CONDITIONAL_EXPRESSION)).isTrue();
    assertThat(tree.condition()).isInstanceOf(LiteralTree.class);
    assertThat(tree.questionToken().text()).isEqualTo("?");
    assertThat(tree.trueExpression()).isInstanceOf(LiteralTree.class);
    assertThat(tree.colonToken().text()).isEqualTo(":");
    assertThat(tree.falseExpression()).isInstanceOf(LiteralTree.class);
    assertThatChildrenIteratorHasSize(tree, 5);
    tree = (ConditionalExpressionTree) expressionOfReturnStatement("class T { boolean m() { return true ? true : false ? true : false; } }");
    assertThat(tree.is(Tree.Kind.CONDITIONAL_EXPRESSION)).isTrue();
    assertThat(tree.condition()).isInstanceOf(LiteralTree.class);
    assertThat(tree.trueExpression()).isInstanceOf(LiteralTree.class);
    assertThat(tree.falseExpression()).isInstanceOf(ConditionalExpressionTree.class);
    assertThatChildrenIteratorHasSize(tree, 5);
    tree = (ConditionalExpressionTree) tree.falseExpression();
    assertThat(tree.is(Tree.Kind.CONDITIONAL_EXPRESSION)).isTrue();
    assertThat(tree.condition()).isInstanceOf(LiteralTree.class);
    assertThat(tree.trueExpression()).isInstanceOf(LiteralTree.class);
    assertThat(tree.falseExpression()).isInstanceOf(LiteralTree.class);
    assertThatChildrenIteratorHasSize(tree, 5);
}
Also used : ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Test(org.junit.Test)

Example 3 with ConditionalExpressionTree

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

the class NestedTernaryOperatorsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ConditionalExpressionTree ternary = (ConditionalExpressionTree) tree;
    Stream.of(ternary.condition(), ternary.trueExpression(), ternary.falseExpression()).forEach(expr -> expr.accept(new TernaryVisitor()));
}
Also used : ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree)

Example 4 with ConditionalExpressionTree

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

the class ExplodedGraphWalker method handleBlockExit.

private void handleBlockExit(ProgramPoint programPosition) {
    CFG.Block block = (CFG.Block) programPosition.block;
    Tree terminator = block.terminator();
    cleanUpProgramState(block);
    boolean exitPath = node.exitPath;
    if (terminator != null) {
        switch(terminator.kind()) {
            case IF_STATEMENT:
                ExpressionTree ifCondition = ((IfStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(ifCondition), verifyCondition(ifCondition));
                return;
            case CONDITIONAL_OR:
            case CONDITIONAL_AND:
                handleBranch(block, ((BinaryExpressionTree) terminator).leftOperand());
                return;
            case CONDITIONAL_EXPRESSION:
                handleBranch(block, ((ConditionalExpressionTree) terminator).condition());
                return;
            case FOR_STATEMENT:
                ExpressionTree condition = ((ForStatementTree) terminator).condition();
                if (condition != null) {
                    handleBranch(block, condition, false);
                    return;
                }
                break;
            case WHILE_STATEMENT:
                ExpressionTree whileCondition = ((WhileStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(whileCondition), verifyCondition(whileCondition));
                return;
            case DO_STATEMENT:
                ExpressionTree doCondition = ((DoWhileStatementTree) terminator).condition();
                handleBranch(block, cleanupCondition(doCondition), verifyCondition(doCondition));
                return;
            case SYNCHRONIZED_STATEMENT:
                resetFieldValues(false);
                break;
            case RETURN_STATEMENT:
                ExpressionTree returnExpression = ((ReturnStatementTree) terminator).expression();
                if (returnExpression != null) {
                    programState.storeExitValue();
                }
                break;
            case THROW_STATEMENT:
                ProgramState.Pop unstack = programState.unstackValue(1);
                SymbolicValue sv = unstack.values.get(0);
                if (sv instanceof SymbolicValue.CaughtExceptionSymbolicValue) {
                    // retrowing the exception from a catch block
                    sv = ((SymbolicValue.CaughtExceptionSymbolicValue) sv).exception();
                } else {
                    sv = constraintManager.createExceptionalSymbolicValue(((ThrowStatementTree) terminator).expression().symbolType());
                }
                programState = unstack.state.stackValue(sv);
                programState.storeExitValue();
                break;
            default:
        }
    }
    // unconditional jumps, for-statement, switch-statement, synchronized:
    if (exitPath) {
        if (block.exitBlock() != null) {
            enqueue(new ProgramPoint(block.exitBlock()), programState, true);
        } else {
            for (CFG.Block successor : block.successors()) {
                enqueue(new ProgramPoint(successor), programState, true);
            }
        }
    } else {
        for (CFG.Block successor : block.successors()) {
            if (!block.isFinallyBlock() || isDirectFlowSuccessorOf(successor, block)) {
                enqueue(new ProgramPoint(successor), programState, successor == block.exitBlock());
            }
        }
    }
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) CFG(org.sonar.java.cfg.CFG) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) 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) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) 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) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 5 with ConditionalExpressionTree

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

the class ExplodedGraphWalker method isPartOfConditionalExpressionCondition.

private static boolean isPartOfConditionalExpressionCondition(Tree tree) {
    Tree current;
    Tree parent = tree;
    do {
        current = parent;
        parent = parent.parent();
    } while (parent.is(Tree.Kind.PARENTHESIZED_EXPRESSION, Tree.Kind.CONDITIONAL_AND, Tree.Kind.CONDITIONAL_OR));
    return parent.is(Tree.Kind.CONDITIONAL_EXPRESSION) && current.equals(((ConditionalExpressionTree) parent).condition());
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) 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) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree)

Aggregations

ConditionalExpressionTree (org.sonar.plugins.java.api.tree.ConditionalExpressionTree)8 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)3 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)3 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)3 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)3 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)3 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)3 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)3 Test (org.junit.Test)2 JavaTree (org.sonar.java.model.JavaTree)2 Type (org.sonar.plugins.java.api.semantic.Type)2 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)2 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2 DoWhileStatementTree (org.sonar.plugins.java.api.tree.DoWhileStatementTree)2 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)2 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)2 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)2