Search in sources :

Example 1 with TryStatementTree

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

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

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

the class RedundantCloseCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    TryStatementTree tryStatementTree = (TryStatementTree) tree;
    Set<Symbol> resourceSymbols = tryStatementTree.resourceList().stream().map(RedundantCloseCheck::resourceSymbol).filter(s -> !s.isUnknown()).collect(Collectors.toSet());
    if (resourceSymbols.isEmpty()) {
        return;
    }
    tryStatementTree.block().accept(new CloseVisitor(resourceSymbols));
}
Also used : TypeCriteria(org.sonar.java.matcher.TypeCriteria) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodMatcher(org.sonar.java.matcher.MethodMatcher) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 4 with TryStatementTree

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

the class NestedTryCatchCheck method visitTryStatement.

@Override
public void visitTryStatement(TryStatementTree tree) {
    scan(tree.resourceList());
    Deque<Tree> currentNestingLevel = nestingLevel.peek();
    if (!tree.catches().isEmpty()) {
        int size = currentNestingLevel.size();
        if (size > 0) {
            List<JavaFileScannerContext.Location> secondary = new ArrayList<>(size);
            for (Tree element : currentNestingLevel) {
                secondary.add(new JavaFileScannerContext.Location("Nesting + 1", element));
            }
            context.reportIssue(this, tree.tryKeyword(), "Extract this nested try block into a separate method.", secondary, null);
        }
        currentNestingLevel.push(tree.tryKeyword());
    }
    scan(tree.block());
    if (!tree.catches().isEmpty()) {
        currentNestingLevel.pop();
    }
    scan(tree.catches());
    scan(tree.finallyBlock());
}
Also used : JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ArrayList(java.util.ArrayList) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree)

Example 5 with TryStatementTree

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

TryStatementTree (org.sonar.plugins.java.api.tree.TryStatementTree)6 CatchTree (org.sonar.plugins.java.api.tree.CatchTree)3 Tree (org.sonar.plugins.java.api.tree.Tree)3 ArrayList (java.util.ArrayList)2 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Collections (java.util.Collections)1 List (java.util.List)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Test (org.junit.Test)1 Rule (org.sonar.check.Rule)1 MethodMatcher (org.sonar.java.matcher.MethodMatcher)1 TypeCriteria (org.sonar.java.matcher.TypeCriteria)1 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)1 Symbol (org.sonar.plugins.java.api.semantic.Symbol)1 Type (org.sonar.plugins.java.api.semantic.Type)1 BaseTreeVisitor (org.sonar.plugins.java.api.tree.BaseTreeVisitor)1 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1