Search in sources :

Example 1 with CatchTree

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

the class InterruptedExceptionCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    TryStatementTree tryStatementTree = (TryStatementTree) tree;
    withinInterruptingFinally.addFirst(isFinallyInterrupting(tryStatementTree.finallyBlock()));
    for (CatchTree catchTree : tryStatementTree.catches()) {
        Type catchType = catchTree.parameter().symbol().type();
        if (catchType.is("java.lang.InterruptedException") || catchType.is("java.lang.ThreadDeath")) {
            BlockVisitor blockVisitor = new BlockVisitor(catchTree.parameter().symbol());
            catchTree.block().accept(blockVisitor);
            if (!blockVisitor.threadInterrupted && !isWithinInterruptingFinally()) {
                reportIssue(catchTree.parameter(), "Either re-interrupt this method or rethrow the \"" + catchType.name() + "\".");
            }
        }
    }
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree)

Example 2 with CatchTree

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

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

the class JavaTreeModelTest method try_statement.

/**
 * 14.20. The try statement
 */
@Test
public void try_statement() {
    TryStatementTree tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try { } finally { } } }");
    assertThat(tree.is(Tree.Kind.TRY_STATEMENT)).isTrue();
    assertThat(tree.resources()).isEmpty();
    assertThat(tree.block()).isNotNull();
    assertThat(tree.catches()).isEmpty();
    assertThat(tree.finallyKeyword().text()).isEqualTo("finally");
    assertThat(tree.finallyBlock()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 4);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try { } catch (RuntimeException e1) { } catch (Exception e2) { } } }");
    assertThat(tree.is(Tree.Kind.TRY_STATEMENT)).isTrue();
    assertThat(tree.tryKeyword().text()).isEqualTo("try");
    assertThat(tree.openParenToken()).isNull();
    assertThat(tree.resources()).isEmpty();
    assertThat(tree.closeParenToken()).isNull();
    assertThat(tree.block()).isNotNull();
    assertThat(tree.finallyKeyword()).isNull();
    assertThat(tree.finallyBlock()).isNull();
    assertThat(tree.catches()).hasSize(2);
    assertThatChildrenIteratorHasSize(tree, 4);
    CatchTree catchTree = tree.catches().get(0);
    assertThat(catchTree.catchKeyword().text()).isEqualTo("catch");
    assertThat(catchTree.block()).isNotNull();
    assertThat(catchTree.openParenToken().text()).isEqualTo("(");
    assertThat(catchTree.closeParenToken().text()).isEqualTo(")");
    assertThatChildrenIteratorHasSize(catchTree, 5);
    VariableTree parameterTree = catchTree.parameter();
    assertThat(parameterTree.type()).isNotNull();
    assertThat(parameterTree.simpleName().name()).isEqualTo("e1");
    assertThat(parameterTree.initializer()).isNull();
    assertThatChildrenIteratorHasSize(parameterTree, 3);
    catchTree = tree.catches().get(1);
    assertThatChildrenIteratorHasSize(catchTree, 5);
    parameterTree = catchTree.parameter();
    assertThat(parameterTree.type()).isNotNull();
    assertThat(parameterTree.simpleName().name()).isEqualTo("e2");
    assertThat(parameterTree.initializer()).isNull();
    assertThatChildrenIteratorHasSize(parameterTree, 3);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try { } catch (Exception e) { } finally { } } }");
    assertThat(tree.is(Tree.Kind.TRY_STATEMENT)).isTrue();
    assertThat(tree.resources()).isEmpty();
    assertThat(tree.block()).isNotNull();
    assertThat(tree.catches()).hasSize(1);
    assertThat(tree.finallyKeyword().text()).isEqualTo("finally");
    assertThatChildrenIteratorHasSize(tree.catches().get(0), 5);
    assertThat(tree.finallyBlock()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 5);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try { } catch (final @Foo Exception e) { } } }");
    assertThat(tree.is(Tree.Kind.TRY_STATEMENT)).isTrue();
    assertThat(tree.resources()).isEmpty();
    assertThat(tree.block()).isNotNull();
    assertThat(tree.catches()).hasSize(1);
    assertThat(tree.finallyKeyword()).isNull();
    assertThat(tree.finallyBlock()).isNull();
    assertThatChildrenIteratorHasSize(tree, 3);
    catchTree = tree.catches().get(0);
    assertThatChildrenIteratorHasSize(catchTree, 5);
    parameterTree = catchTree.parameter();
    assertThat(parameterTree.modifiers()).hasSize(2);
    assertThat(parameterTree.simpleName().identifierToken().text()).isEqualTo("e");
    assertThat(parameterTree.type().is(Tree.Kind.IDENTIFIER)).isTrue();
    assertThat(parameterTree.endToken()).isNull();
    assertThat(parameterTree.initializer()).isNull();
    assertThatChildrenIteratorHasSize(parameterTree, 3);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try (final @Foo Resource r = open()) { } } }");
    assertThat(tree.is(Tree.Kind.TRY_STATEMENT)).isTrue();
    assertThat(tree.block()).isNotNull();
    assertThat(tree.catches()).isEmpty();
    assertThat(tree.finallyKeyword()).isNull();
    assertThat(tree.finallyBlock()).isNull();
    assertThat(tree.openParenToken().text()).isEqualTo("(");
    assertThat(tree.resources()).hasSize(1);
    assertThat(tree.closeParenToken().text()).isEqualTo(")");
    assertThatChildrenIteratorHasSize(tree, 5);
    VariableTree resource = tree.resources().get(0);
    assertThat(resource.simpleName().name()).isEqualTo("r");
    assertThat(resource.initializer()).isNotNull();
    assertThat(resource.modifiers()).hasSize(2);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try (Resource r1 = open(); Resource r2 = open()) { } catch (Exception e) { } finally { } } }");
    assertThat(tree.is(Tree.Kind.TRY_STATEMENT)).isTrue();
    assertThat(tree.block()).isNotNull();
    assertThat(tree.catches()).hasSize(1);
    assertThat(tree.finallyKeyword().text()).isEqualTo("finally");
    assertThatChildrenIteratorHasSize(tree.catches().get(0), 5);
    assertThat(tree.finallyBlock()).isNotNull();
    assertThat(tree.openParenToken().text()).isEqualTo("(");
    assertThat(tree.resources()).hasSize(2);
    assertThat(tree.resources().separators()).hasSize(1);
    assertThat(tree.closeParenToken().text()).isEqualTo(")");
    resource = tree.resources().get(0);
    assertThat(resource.simpleName().name()).isEqualTo("r1");
    assertThat(resource.initializer()).isNotNull();
    resource = tree.resources().get(1);
    assertThat(resource.simpleName().name()).isEqualTo("r2");
    assertThat(resource.initializer()).isNotNull();
    assertThatChildrenIteratorHasSize(tree, 8);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try { } catch (Exception1 | Exception2 e) { } } }");
    parameterTree = tree.catches().get(0).parameter();
    assertThatChildrenIteratorHasSize(parameterTree, 3);
    UnionTypeTree type = (UnionTypeTree) parameterTree.type();
    assertThatChildrenIteratorHasSize(type, 1);
    assertThat(type.typeAlternatives()).hasSize(2);
    assertThat(type.typeAlternatives().separators()).hasSize(1);
    assertThatChildrenIteratorHasSize(tree, 3);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try (r1) { } } }");
    assertThat(tree.resources()).isEmpty();
    assertThat(tree.resources().separators()).isEmpty();
    assertThat(tree.resourceList()).hasSize(1);
    assertThat(tree.resourceList().separators()).isEmpty();
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try (r1; super.field; new A().f) { } } }");
    assertThat(tree.resources()).isEmpty();
    assertThat(tree.resources().separators()).isEmpty();
    assertThat(tree.resourceList()).hasSize(3);
    assertThat(tree.resourceList().separators()).hasSize(2);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try (r1; Resource r2 = open();) { } } }");
    assertThat(tree.resources()).hasSize(1);
    assertThat(tree.resources().separators()).hasSize(1);
    assertThat(tree.resources().separators().get(0).column()).isEqualTo(50);
    assertThat(tree.resourceList()).hasSize(2);
    assertThat(tree.resourceList().separators()).hasSize(2);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try (r1; Resource r2 = open()) { } } }");
    assertThat(tree.resources()).hasSize(1);
    assertThat(tree.resources().separators()).isEmpty();
    assertThat(tree.resourceList()).hasSize(2);
    assertThat(tree.resourceList().separators()).hasSize(1);
    tree = (TryStatementTree) firstMethodFirstStatement("class T { void m() { try (Resource r2 = open(); r1;) { } } }");
    assertThat(tree.resources()).hasSize(1);
    assertThat(tree.resources().separators()).hasSize(1);
    assertThat(tree.resources().separators().get(0).column()).isEqualTo(46);
    assertThat(tree.resourceList()).hasSize(2);
    assertThat(tree.resourceList().separators()).hasSize(2);
}
Also used : TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) CatchTree(org.sonar.plugins.java.api.tree.CatchTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) UnionTypeTree(org.sonar.plugins.java.api.tree.UnionTypeTree) Test(org.junit.Test)

Example 4 with CatchTree

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

the class DeadStoreCheck method handleTryStatement.

private static void handleTryStatement(Set<Symbol> out, Symbol.MethodSymbol methodSymbol, TryStatementTree element) {
    AssignedLocalVarVisitor visitor = new AssignedLocalVarVisitor();
    element.block().accept(visitor);
    out.addAll(visitor.assignedLocalVars);
    for (CatchTree catchTree : element.catches()) {
        out.addAll(getUsedLocalVarInSubTree(catchTree, methodSymbol));
    }
}
Also used : CatchTree(org.sonar.plugins.java.api.tree.CatchTree)

Example 5 with CatchTree

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

Aggregations

CatchTree (org.sonar.plugins.java.api.tree.CatchTree)9 TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)3 UnionTypeTree (org.sonar.plugins.java.api.tree.UnionTypeTree)3 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2 Test (org.junit.Test)1 Type (org.sonar.plugins.java.api.semantic.Type)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)1 InstanceOfTree (org.sonar.plugins.java.api.tree.InstanceOfTree)1 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)1 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)1 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1