Search in sources :

Example 1 with BlockTree

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

the class IndentationCheck method visitLambdaExpression.

@Override
public void visitLambdaExpression(LambdaExpressionTree lambdaExpressionTree) {
    // doesn't scan lambda parameters because there's no indentation check on types and identifiers
    Tree body = lambdaExpressionTree.body();
    if (body.is(Kind.BLOCK)) {
        BlockTree block = (BlockTree) body;
        excludeIssueAtLine = block.openBraceToken().line();
        int previousLevel = expectedLevel;
        expectedLevel = block.closeBraceToken().column();
        scan(block);
        expectedLevel = previousLevel;
    } else {
        scan(body);
    }
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) CaseGroupTree(org.sonar.plugins.java.api.tree.CaseGroupTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) CaseLabelTree(org.sonar.plugins.java.api.tree.CaseLabelTree) SwitchStatementTree(org.sonar.plugins.java.api.tree.SwitchStatementTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 2 with BlockTree

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

the class HiddenFieldCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (isClassTree(tree)) {
        ClassTree classTree = (ClassTree) tree;
        ImmutableMap.Builder<String, VariableTree> builder = ImmutableMap.builder();
        for (Tree member : classTree.members()) {
            if (member.is(Tree.Kind.VARIABLE)) {
                VariableTree variableTree = (VariableTree) member;
                builder.put(variableTree.simpleName().name(), variableTree);
            }
        }
        fields.push(builder.build());
        excludedVariables.push(Lists.<VariableTree>newArrayList());
    } else if (tree.is(Tree.Kind.VARIABLE)) {
        VariableTree variableTree = (VariableTree) tree;
        isVariableHidingField(variableTree);
    } else if (tree.is(Tree.Kind.STATIC_INITIALIZER)) {
        excludeVariablesFromBlock((BlockTree) tree);
    } else {
        MethodTree methodTree = (MethodTree) tree;
        excludedVariables.peek().addAll(methodTree.parameters());
        flattenExcludedVariables.addAll(methodTree.parameters());
        if (ModifiersUtils.hasModifier(methodTree.modifiers(), Modifier.STATIC)) {
            excludeVariablesFromBlock(methodTree.block());
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with BlockTree

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

the class CFG method buildTryStatement.

private void buildTryStatement(TryStatementTree tryStatementTree) {
    // FIXME only path with no failure constructed for now, (not taking try with resources into consideration).
    currentBlock = createBlock(currentBlock);
    BlockTree finallyBlockTree = tryStatementTree.finallyBlock();
    if (finallyBlockTree != null) {
        currentBlock.isFinallyBlock = true;
        Block finallyBlock = currentBlock;
        build(finallyBlockTree);
        finallyBlock.addExitSuccessor(exitBlock());
        exitBlocks.push(currentBlock);
        addContinueTarget(currentBlock);
        currentBlock.isFinallyBlock = true;
        breakTargets.addLast(currentBlock);
    }
    Block finallyOrEndBlock = currentBlock;
    Block beforeFinally = createBlock(currentBlock);
    TryStatement tryStatement = new TryStatement();
    tryStatement.successorBlock = finallyOrEndBlock;
    enclosingTry.push(tryStatement);
    enclosedByCatch.push(false);
    for (CatchTree catchTree : Lists.reverse(tryStatementTree.catches())) {
        currentBlock = createBlock(finallyOrEndBlock);
        enclosedByCatch.push(true);
        build(catchTree.block());
        buildVariable(catchTree.parameter());
        currentBlock.isCatchBlock = true;
        enclosedByCatch.pop();
        tryStatement.addCatch(catchTree.parameter().type().symbolType(), currentBlock);
    }
    currentBlock = beforeFinally;
    build(tryStatementTree.block());
    build((List<? extends Tree>) tryStatementTree.resourceList());
    enclosingTry.pop();
    enclosedByCatch.pop();
    currentBlock = createBlock(currentBlock);
    currentBlock.elements.add(tryStatementTree);
    if (finallyBlockTree != null) {
        exitBlocks.pop();
        continueTargets.removeLast();
        breakTargets.removeLast();
    }
}
Also used : CatchTree(org.sonar.plugins.java.api.tree.CatchTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 4 with BlockTree

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

the class CFG method build.

public static CFG build(MethodTree tree) {
    BlockTree block = tree.block();
    Preconditions.checkArgument(block != null, "Cannot build CFG for method with no body.");
    return new CFG(block.body(), tree.symbol(), false);
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 5 with BlockTree

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

the class JavaTreeModelTest method class_init_declaration.

@Test
public void class_init_declaration() {
    BlockTree tree = (BlockTree) firstTypeMember("class T { { ; ; } }");
    assertThat(tree.is(Tree.Kind.INITIALIZER)).isTrue();
    assertThat(tree.body()).hasSize(2);
    assertThat(tree.openBraceToken().text()).isEqualTo("{");
    assertThat(tree.closeBraceToken().text()).isEqualTo("}");
    assertThatChildrenIteratorHasSize(tree, 4);
    tree = (BlockTree) firstTypeMember("class T { static { ; ; } }");
    assertThat(tree.is(Tree.Kind.STATIC_INITIALIZER)).isTrue();
    StaticInitializerTree staticInitializerTree = (StaticInitializerTree) tree;
    assertThat(staticInitializerTree.body()).hasSize(2);
    assertThat(staticInitializerTree.staticKeyword().text()).isEqualTo("static");
    assertThat(staticInitializerTree.openBraceToken().text()).isEqualTo("{");
    assertThat(staticInitializerTree.closeBraceToken().text()).isEqualTo("}");
    assertThatChildrenIteratorHasSize(tree, 5);
}
Also used : StaticInitializerTree(org.sonar.plugins.java.api.tree.StaticInitializerTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) Test(org.junit.Test)

Aggregations

BlockTree (org.sonar.plugins.java.api.tree.BlockTree)34 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)14 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)13 Test (org.junit.Test)10 ExpressionStatementTree (org.sonar.plugins.java.api.tree.ExpressionStatementTree)9 Tree (org.sonar.plugins.java.api.tree.Tree)5 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)4 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)4 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)4 JavaTree (org.sonar.java.model.JavaTree)3 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)3 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 ThrowStatementTree (org.sonar.plugins.java.api.tree.ThrowStatementTree)3 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 CatchTree (org.sonar.plugins.java.api.tree.CatchTree)2 ForEachStatement (org.sonar.plugins.java.api.tree.ForEachStatement)2 ForStatementTree (org.sonar.plugins.java.api.tree.ForStatementTree)2 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)2