Search in sources :

Example 1 with ForStatementTree

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

the class JavaTreeModelTest method for_statement.

/**
 * 14.14. The for Statement
 */
@Test
public void for_statement() {
    ForStatementTree tree = (ForStatementTree) firstMethodFirstStatement("class T { void m() { for (int i = 0; i < 42; i ++) ; } }");
    assertThat(tree.is(Tree.Kind.FOR_STATEMENT)).isTrue();
    assertThat(tree.forKeyword().text()).isEqualTo("for");
    assertThat(tree.openParenToken().text()).isEqualTo("(");
    assertThat(tree.initializer()).hasSize(1);
    assertThat(tree.initializer().get(0)).isInstanceOf(VariableTree.class);
    assertThatChildrenIteratorHasSize(tree.initializer().get(0), 5);
    assertThat(tree.firstSemicolonToken().text()).isEqualTo(";");
    assertThat(tree.condition()).isNotNull();
    assertThat(tree.secondSemicolonToken().text()).isEqualTo(";");
    assertThat(tree.update()).isNotNull();
    assertThat(tree.closeParenToken().text()).isEqualTo(")");
    assertThat(tree.statement()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 9);
    tree = (ForStatementTree) firstMethodFirstStatement("class T { void m() { for (i = 0; i < 42; i ++) ; } }");
    assertThat(tree.is(Tree.Kind.FOR_STATEMENT)).isTrue();
    assertThat(tree.initializer()).hasSize(1);
    assertThat(tree.initializer().get(0)).isInstanceOf(ExpressionStatementTree.class);
    assertThat(tree.condition()).isNotNull();
    assertThat(tree.update()).isNotNull();
    assertThat(tree.statement()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 9);
    tree = (ForStatementTree) firstMethodFirstStatement("class T { void m() { for ( ; ; ) ; } }");
    assertThat(tree.is(Tree.Kind.FOR_STATEMENT)).isTrue();
    assertThat(tree.initializer()).isEmpty();
    assertThat(tree.condition()).isNull();
    assertThat(tree.update()).isEmpty();
    assertThat(tree.statement()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 8);
    tree = (ForStatementTree) firstMethodFirstStatement("class T { void m() { for (i = 0, j = 1; i < 42; i++, j--) ; } }");
    assertThat(tree.is(Tree.Kind.FOR_STATEMENT)).isTrue();
    assertThat(tree.initializer()).hasSize(2);
    assertThat(tree.initializer().separators()).hasSize(1);
    assertThat(tree.condition()).isNotNull();
    assertThat(tree.update()).hasSize(2);
    assertThat(tree.update().separators()).hasSize(1);
    assertThat(tree.statement()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 9);
    tree = (ForStatementTree) firstMethodFirstStatement("class T { void m() { for (int i = 0, j = 1; i < 42; i++, j--) ; } }");
    assertThat(tree.is(Tree.Kind.FOR_STATEMENT)).isTrue();
    assertThat(tree.initializer()).hasSize(2);
    assertThat(tree.initializer().separators()).hasSize(0);
    assertThat(tree.condition()).isNotNull();
    assertThat(tree.update()).hasSize(2);
    assertThat(tree.update().separators()).hasSize(1);
    assertThat(tree.statement()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 9);
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) Test(org.junit.Test)

Example 2 with ForStatementTree

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

the class AbstractForLoopRule method visitNode.

@Override
public void visitNode(Tree tree) {
    ForStatementTree forStatement = (ForStatementTree) tree;
    visitForStatement(forStatement);
}
Also used : ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree)

Example 3 with ForStatementTree

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

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

the class MissingCurlyBracesCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    switch(tree.kind()) {
        case WHILE_STATEMENT:
            WhileStatementTree whileStatementTree = (WhileStatementTree) tree;
            checkStatement(whileStatementTree.whileKeyword(), whileStatementTree.statement());
            break;
        case DO_STATEMENT:
            DoWhileStatementTree doWhileStatementTree = (DoWhileStatementTree) tree;
            checkStatement(doWhileStatementTree.doKeyword(), doWhileStatementTree.statement());
            break;
        case FOR_STATEMENT:
            ForStatementTree forStatementTree = (ForStatementTree) tree;
            checkStatement(forStatementTree.forKeyword(), forStatementTree.statement());
            break;
        case FOR_EACH_STATEMENT:
            ForEachStatement forEachStatement = (ForEachStatement) tree;
            checkStatement(forEachStatement.forKeyword(), forEachStatement.statement());
            break;
        case IF_STATEMENT:
            checkIfStatement((IfStatementTree) tree);
            break;
        default:
            break;
    }
}
Also used : DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) ForEachStatement(org.sonar.plugins.java.api.tree.ForEachStatement)

Example 5 with ForStatementTree

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

the class MultilineBlocksCurlyBracesCheck method check.

private void check(StatementTree current, StatementTree previous) {
    StatementTree block = null;
    boolean condition = false;
    if (previous.is(Tree.Kind.FOR_EACH_STATEMENT)) {
        block = ((ForEachStatement) previous).statement();
    } else if (previous.is(Tree.Kind.FOR_STATEMENT)) {
        block = ((ForStatementTree) previous).statement();
    } else if (previous.is(Tree.Kind.WHILE_STATEMENT)) {
        block = ((WhileStatementTree) previous).statement();
    } else if (previous.is(Tree.Kind.IF_STATEMENT)) {
        block = getIfStatementLastBlock(previous);
        condition = true;
    }
    if (block != null && !block.is(Tree.Kind.BLOCK)) {
        SyntaxToken previousToken = block.firstToken();
        int previousColumn = previousToken.column();
        int previousLine = previousToken.line();
        SyntaxToken currentToken = current.firstToken();
        int currentColumn = currentToken.column();
        int currentLine = currentToken.line();
        if ((previousColumn == currentColumn && previousLine + 1 == currentLine) || (previousLine == previous.firstToken().line() && previous.firstToken().column() < currentColumn)) {
            int lines = 1 + currentLine - previousLine;
            context.reportIssue(this, current, getMessage(condition, lines));
        }
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken)

Aggregations

ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)8 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)3 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2 DoWhileStatementTree (org.sonar.plugins.java.api.tree.DoWhileStatementTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 ForEachStatement (org.sonar.plugins.java.api.tree.ForEachStatement)2 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)2 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)2 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Test (org.junit.Test)1 CFG (org.sonar.java.cfg.CFG)1 JavaTree (org.sonar.java.model.JavaTree)1 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)1 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 ArrayAccessExpressionTree (org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree)1