Search in sources :

Example 41 with StatementTree

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

the class IndentationCheck method checkCaseGroup.

private void checkCaseGroup(CaseGroupTree tree) {
    List<CaseLabelTree> labels = tree.labels();
    if (labels.size() >= 2) {
        CaseLabelTree previousCaseLabelTree = labels.get(labels.size() - 2);
        excludeIssueAtLine = previousCaseLabelTree.lastToken().line();
    }
    List<StatementTree> body = tree.body();
    List<StatementTree> newBody = body;
    int bodySize = body.size();
    if (bodySize > 0 && body.get(0).is(Kind.BLOCK)) {
        expectedLevel -= indentationLevel;
        checkIndentation(body.get(0), Iterables.getLast(labels).colonToken().column() + 2);
        newBody = body.subList(1, bodySize);
    }
    checkIndentation(newBody);
    if (bodySize > 0 && body.get(0).is(Kind.BLOCK)) {
        expectedLevel += indentationLevel;
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree)

Example 42 with StatementTree

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

the class MultilineBlocksCurlyBracesCheck method getIfStatementLastBlock.

private static StatementTree getIfStatementLastBlock(StatementTree statementTree) {
    StatementTree block = statementTree;
    while (block.is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStatementTree = (IfStatementTree) block;
        StatementTree elseStatement = ifStatementTree.elseStatement();
        block = elseStatement == null ? ifStatementTree.thenStatement() : elseStatement;
    }
    return block;
}
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) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 43 with StatementTree

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

Example 44 with StatementTree

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

the class CFGTest method build_partial_cfg.

private void build_partial_cfg(String breakOrContinue) {
    String methodCode = "void meth(){ try {fun(); } catch ( Exception e) {e.printStackTrace(); " + breakOrContinue + "; } }";
    CompilationUnitTree cut = (CompilationUnitTree) parser.parse("class A {" + methodCode + "}");
    SemanticModel.createFor(cut, new SquidClassLoader(Collections.emptyList()));
    MethodTree methodTree = (MethodTree) ((ClassTree) cut.types().get(0)).members().get(0);
    List<StatementTree> body = methodTree.block().body();
    CFG cfg = CFG.buildCFG(body, true);
    cfg.setMethodSymbol(methodTree.symbol());
    assertThat(cfg.blocks()).hasSize(5);
    assertThat(cfg.methodSymbol()).isSameAs(methodTree.symbol());
    try {
        CFG.buildCFG(body, false);
        fail("IllegalStateException should have been thrown");
    } catch (IllegalStateException iae) {
        assertThat(iae).hasMessage("'" + breakOrContinue + "' statement not in loop or switch statement");
    }
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) SquidClassLoader(org.sonar.java.bytecode.loader.SquidClassLoader)

Example 45 with StatementTree

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

the class SyntaxTreeDebug method blockString.

private static String blockString(BlockTree syntaxNode) {
    StringBuilder buffer = new StringBuilder();
    boolean first = true;
    for (StatementTree tree : syntaxNode.body()) {
        if (first) {
            first = false;
        } else {
            buffer.append(", ");
        }
        buffer.append(tree);
    }
    return buffer.toString();
}
Also used : SynchronizedStatementTree(org.sonar.plugins.java.api.tree.SynchronizedStatementTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree)

Aggregations

StatementTree (org.sonar.plugins.java.api.tree.StatementTree)53 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)27 Test (org.junit.Test)26 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)26 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)24 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)21 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)17 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)15 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)13 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)9 WhileStatementTree (org.sonar.plugins.java.api.tree.WhileStatementTree)8 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)7 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)7 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)6 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)6 SwitchStatementTree (org.sonar.plugins.java.api.tree.SwitchStatementTree)6 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)6 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)5 Tree (org.sonar.plugins.java.api.tree.Tree)5 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)5