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() + "\".");
}
}
}
}
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);
}
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));
}
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());
}
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);
}
}
}
Aggregations