Skip to content

Commit

Permalink
InterruptedExceptionSwallowed: handle try with resources better.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=288292908
  • Loading branch information
graememorgan authored and netdpb committed Jan 6, 2020
1 parent 08c4793 commit b7f2a6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
import com.sun.source.tree.NewClassTree;
import com.sun.source.tree.StatementTree;
import com.sun.source.tree.ThrowTree;
import com.sun.source.tree.Tree;
import com.sun.source.tree.TryTree;
import com.sun.source.tree.VariableTree;
import com.sun.source.util.TreeScanner;
Expand Down Expand Up @@ -155,7 +154,7 @@ public Description matchTry(TryTree tree, VisitorState state) {
return NO_MATCH;
}
if (caughtTypes.stream().anyMatch(t -> isSubtype(interrupted, t, state))) {
ImmutableSet<Type> thrownExceptions = getThrownExceptions(tree.getBlock(), state);
ImmutableSet<Type> thrownExceptions = getThrownExceptions(tree, state);
if (thrownExceptions.stream().anyMatch(t -> isSubtype(t, interrupted, state))
&& !blockChecksForInterruptedException(catchTree.getBlock(), state)
&& !isSuppressed(catchTree.getParameter())) {
Expand Down Expand Up @@ -196,9 +195,21 @@ public Boolean visitInstanceOf(InstanceOfTree instanceOfTree, Void aVoid) {
}.scan(block, null));
}

private static ImmutableSet<Type> getThrownExceptions(Tree tree, VisitorState state) {
/** Returns the exceptions thrown by {@code blockTree}. */
private static ImmutableSet<Type> getThrownExceptions(BlockTree blockTree, VisitorState state) {
ScanThrownTypes scanner = new ScanThrownTypes(state);
scanner.scan(tree, null);
scanner.scan(blockTree, null);
return ImmutableSet.copyOf(scanner.getThrownTypes());
}

/**
* Returns the exceptions that need to be handled by {@code tryTree}'s catch blocks, or be
* propagated out.
*/
private static ImmutableSet<Type> getThrownExceptions(TryTree tryTree, VisitorState state) {
ScanThrownTypes scanner = new ScanThrownTypes(state);
scanner.scanResources(tryTree);
scanner.scan(tryTree.getBlock(), null);
return ImmutableSet.copyOf(scanner.getThrownTypes());
}

Expand Down Expand Up @@ -231,9 +242,7 @@ public Void visitMethodInvocation(MethodInvocationTree invocation, Void unused)
@Override
public Void visitTry(TryTree tree, Void unused) {
thrownTypes.push(new HashSet<>());
inResources = true;
scan(tree.getResources(), null);
inResources = false;
scanResources(tree);
scan(tree.getBlock(), null);
// Make two passes over the `catch` blocks: once to remove caught exceptions, and once to
// add thrown ones. We can't do this in one step as an exception could be caught but later
Expand All @@ -253,6 +262,12 @@ public Void visitTry(TryTree tree, Void unused) {
return null;
}

public void scanResources(TryTree tree) {
inResources = true;
scan(tree.getResources(), null);
inResources = false;
}

@Override
public Void visitThrow(ThrowTree tree, Void unused) {
getThrownTypes().addAll(extractTypes(getType(tree.getExpression())));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -152,7 +151,6 @@ public void negative_fieldNamedClose() {
.doTest();
}

@Ignore("Not implemented yet") // TODO(b/147102301)
@Test
public void thrownByClose_swallowedSilently() {
compilationHelper
Expand Down

0 comments on commit b7f2a6e

Please sign in to comment.