Search in sources :

Example 16 with BlockTree

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

the class ReturnInFinallyCheck method visitTryStatement.

@Override
public void visitTryStatement(TryStatementTree tree) {
    scan(tree.resourceList());
    scan(tree.block());
    scan(tree.catches());
    BlockTree finallyBlock = tree.finallyBlock();
    if (finallyBlock != null) {
        treeKindStack.push(finallyBlock.kind());
        scan(finallyBlock);
        treeKindStack.pop();
    }
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 17 with BlockTree

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

the class RightCurlyBraceStartLineCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.BLOCK, Tree.Kind.STATIC_INITIALIZER, Tree.Kind.INITIALIZER)) {
        BlockTree blockTree = (BlockTree) tree;
        checkBlockBody(blockTree.openBraceToken(), blockTree.closeBraceToken(), blockTree.body());
    } else {
        ClassTree classTree = (ClassTree) tree;
        checkBlockBody(classTree.openBraceToken(), classTree.closeBraceToken(), classTree.members());
    }
}
Also used : ClassTree(org.sonar.plugins.java.api.tree.ClassTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 18 with BlockTree

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

the class SelectorMethodArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    if (Boolean.TRUE.equals(methodTree.isOverriding())) {
        return;
    }
    List<Symbol> booleanParameterSymbols = getBooleanParametersAsSymbol(methodTree.parameters());
    BlockTree blockTree = methodTree.block();
    if (isPublic(methodTree) && blockTree != null && !booleanParameterSymbols.isEmpty()) {
        for (Symbol variable : booleanParameterSymbols) {
            Collection<IdentifierTree> usages = variable.usages();
            if (usages.size() == 1) {
                blockTree.accept(new ConditionalStatementVisitor(variable.name(), Iterables.get(usages, 0), methodTree));
            }
        }
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 19 with BlockTree

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

the class RightCurlyBraceToNextBlockAbstractVisitor method visitNode.

@Override
public void visitNode(Tree tree) {
    if (tree.is(Tree.Kind.IF_STATEMENT)) {
        IfStatementTree ifStatementTree = (IfStatementTree) tree;
        StatementTree thenStatement = ifStatementTree.thenStatement();
        if (ifStatementTree.elseKeyword() != null && thenStatement.is(Tree.Kind.BLOCK)) {
            checkTokenPosition(ifStatementTree.elseKeyword(), (BlockTree) thenStatement);
        }
    } else {
        TryStatementTree tryStatementTree = (TryStatementTree) tree;
        BlockTree block = tryStatementTree.block();
        for (CatchTree catchTree : tryStatementTree.catches()) {
            checkTokenPosition(catchTree.catchKeyword(), block);
            block = catchTree.block();
        }
        SyntaxToken finallyKeyword = tryStatementTree.finallyKeyword();
        if (finallyKeyword != null) {
            checkTokenPosition(finallyKeyword, block);
        }
    }
}
Also used : TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree)

Example 20 with BlockTree

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

the class RedundantThrowsDeclarationCheck method hasTryWithResourceInBody.

private static boolean hasTryWithResourceInBody(MethodTree methodTree) {
    BlockTree block = methodTree.block();
    if (block == null) {
        return false;
    }
    TryWithResourcesVisitor visitor = new TryWithResourcesVisitor();
    block.accept(visitor);
    return visitor.hasTryWithResource;
}
Also used : BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

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