Search in sources :

Example 6 with ConditionalExpressionTree

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

the class AllBranchesAreIdenticalCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.SWITCH_STATEMENT)) {
        SwitchStatementTree switchStatement = (SwitchStatementTree) tree;
        Multimap<CaseGroupTree, CaseGroupTree> identicalBranches = checkSwitchStatement(switchStatement);
        if (hasDefaultClause(switchStatement) && allBranchesSame(identicalBranches, switchStatement.cases().size())) {
            reportIssue(switchStatement.switchKeyword(), IF_SWITCH_MSG);
        }
    } else if (tree.is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStatementTree = (IfStatementTree) tree;
        if (hasElseClause(ifStatementTree) && !tree.parent().is(Tree.Kind.IF_STATEMENT)) {
            IfElseChain ifElseChain = checkIfStatement(ifStatementTree);
            if (allBranchesSame(ifElseChain.branches, ifElseChain.totalBranchCount)) {
                reportIssue(ifStatementTree.ifKeyword(), IF_SWITCH_MSG);
            }
        }
    } else {
        checkConditionalExpression((ConditionalExpressionTree) tree);
    }
}
Also used : CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 7 with ConditionalExpressionTree

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

the class PrimitiveWrappersInTernaryOperatorCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ConditionalExpressionTree cet = (ConditionalExpressionTree) tree;
    Type trueExpressionType = cet.trueExpression().symbolType();
    Type falseExpressionType = cet.falseExpression().symbolType();
    if (dissimilarPrimitiveTypeWrappers(trueExpressionType, falseExpressionType)) {
        reportIssue(cet.questionToken(), "Add an explicit cast to match types of operands.");
    }
}
Also used : JavaType(org.sonar.java.resolve.JavaType) Type(org.sonar.plugins.java.api.semantic.Type) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree)

Example 8 with ConditionalExpressionTree

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

the class CFG method buildConditionalExpression.

private void buildConditionalExpression(ConditionalExpressionTree cond) {
    Block next = currentBlock;
    // process else-branch
    ExpressionTree elseStatement = cond.falseExpression();
    currentBlock = createBlock(next);
    build(elseStatement);
    Block elseBlock = currentBlock;
    // process then-branch
    currentBlock = createBlock(next);
    build(cond.trueExpression());
    Block thenBlock = currentBlock;
    // process condition
    currentBlock = createBranch(cond, thenBlock, elseBlock);
    buildCondition(cond.condition(), thenBlock, elseBlock);
}
Also used : 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)

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