Skip to content

Commit

Permalink
Continue the Control Flow bug squash (openrewrite#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLLeitschuh authored Jul 3, 2022
1 parent 50483e2 commit e8298f3
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ public J.Block visitBlock(J.Block block, P p) {
continueFlow.addAll(analysis.continueFlow);
breakFlow.addAll(analysis.breakFlow);
exitFlow.addAll(analysis.exitFlow);
if (current.isEmpty() && statement instanceof J.Try) {
// TODO: Support try-catch blocks properly.
// This case occurs when a try block has exhaustive exits,
// and catch blocks handle errors gracefully
break;
}
}
if (methodEntryPoint) {
ControlFlowNode end = ControlFlowNode.End.create();
Expand Down Expand Up @@ -388,6 +394,13 @@ public J.Binary visitBinary(J.Binary binary, P p) {
return binary;
}

@Override
public J.InstanceOf visitInstanceOf(J.InstanceOf instanceOf, P p) {
visit(instanceOf.getExpression(), p); // First the expression is invoked
addCursorToBasicBlock(); // Then the instanceof node
return instanceOf;
}

@Override
public J.DoWhileLoop visitDoWhileLoop(J.DoWhileLoop doWhileLoop, P p) {
addCursorToBasicBlock(); // Add the while node first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ private static Optional<JavaType> getTypeSafe(Expression e) {
default:
return Optional.empty();
}
} else if (e instanceof J.InstanceOf) {
return Optional.of(JavaType.Primitive.Boolean);
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,44 @@ interface ControlFlowTest : RewriteTest {
)
)

/**
* TODO: This is wrong, but we don't have control flow through try-catch modeled currently.
* This test is just to make sure that we don't blow up when we hit this case.
*/
@Test
fun `control flow for try with resources with catch and additional return`() = rewriteRun(
java(
"""
import java.io.InputStream;
class Test {
InputStream source() { return null; }
int test() {
try (InputStream source = source()) {
return source.read();
} catch (RuntimeException ignored) {
}
return 0;
}
}
""",
"""
import java.io.InputStream;
class Test {
InputStream source() { return null; }
int test() /*~~(BB: 1 CN: 0 EX: 1 | L)~~>*/{
try (InputStream source = source()) {
return source.read();
} catch (RuntimeException ignored) {
}
return 0;
}
}
"""
)
)

@Test
fun `control flow for try`() = rewriteRun(
java(
Expand Down

0 comments on commit e8298f3

Please sign in to comment.