Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import lombok.EqualsAndHashCode;
import lombok.Value;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.DeleteStatement;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaTemplate;
Expand Down Expand Up @@ -97,7 +98,9 @@ public J.Block visitBlock(J.Block block, P p) {
public J.Try visitTry(J.Try tryable, P p) {
J.Try t = super.visitTry(tryable, p);

if (Boolean.TRUE.equals(emptyBlockStyle.getLiteralTry()) && isEmptyBlock(t.getBody())) {
if (Boolean.TRUE.equals(emptyBlockStyle.getLiteralTry()) &&
isEmptyBlock(t.getBody()) &&
isEmptyResources(t.getResources())) {
doAfterVisit(new DeleteStatement<>(tryable));
} else if (Boolean.TRUE.equals(emptyBlockStyle.getLiteralFinally()) && t.getFinally() != null
&& !t.getCatches().isEmpty() && isEmptyBlock(t.getFinally())) {
Expand Down Expand Up @@ -211,7 +214,7 @@ public J.Switch visitSwitch(J.Switch switch_, P p) {

private boolean isEmptyBlock(Statement blockNode) {
if (blockNode instanceof J.Block) {
J.Block block = (J.Block)blockNode;
J.Block block = (J.Block) blockNode;
if (EmptyBlockStyle.BlockPolicy.STATEMENT.equals(emptyBlockStyle.getBlockPolicy())) {
return block.getStatements().isEmpty();
} else if (EmptyBlockStyle.BlockPolicy.TEXT.equals(emptyBlockStyle.getBlockPolicy())) {
Expand All @@ -221,6 +224,29 @@ private boolean isEmptyBlock(Statement blockNode) {
return false;
}

private boolean isEmptyResources(@Nullable List<J.Try.Resource> resources) {
if (resources == null || resources.isEmpty()) {
return true;
}
// Searching for access to instances from outside the scope to detect potential side effects.
// If that's the case, we cannot remove this resources block.
for (J.Try.Resource resource : resources) {
// Any reference to an identifier used here comes from outside the scope.
if (resource.getVariableDeclarations() instanceof J.Identifier) {
return false;
} else if (resource.getVariableDeclarations() instanceof J.VariableDeclarations) {
J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) resource.getVariableDeclarations();
for (J.VariableDeclarations.NamedVariable variable : variableDeclarations.getVariables()) {
// If the variable is not initialized with a new instance, it means it can come from outside the scope.
if (!(variable.getInitializer() instanceof J.NewClass)) {
return false;
}
}
}
}
return true;
}

private static class ExtractSideEffectsOfIfCondition<P> extends JavaVisitor<P> {
private final J.Block enclosingBlock;
private final J.If toExtract;
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/org/openrewrite/staticanalysis/EmptyBlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,76 @@ public class A {
)
);
}

@Test
void emptyTryWithResourcesWithExternalResources() {
rewriteRun(
//language=java
java(
"""
import java.io.FileInputStream;import java.io.IOException;import java.io.UncheckedIOException;public class A {
private final InputStream stdin = new FileInputStream("test.in");
private final InputStream stdout = new FileInputStream("test.out");

public void destroy() {
// close all streams in a try-with-resources
try (stdin; stdout) {
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
"""
)
);
}

@Test
void emptyTryWithResourcesWithVariableInitializationOfExternalResource() {
rewriteRun(
//language=java
java(
"""
import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.UncheckedIOException;public class A {
private final InputStream stdin = new FileInputStream("test.in");

public void destroy() {
// close stream in a try-with-resources
try (InputStream s = stdin) {
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
"""
)
);
}

@Test
void emptyTryWithResourcesWithVariableInitializationMethodInvocation() {
rewriteRun(
//language=java
java(
"""
import java.io.FileInputStream;import java.io.IOException;import java.io.InputStream;import java.io.UncheckedIOException;public class A {
private final InputStream stdin = new FileInputStream("test.in");

private InputStream getStdin() {
return stdin;
}

public void destroy() {
// close stream in a try-with-resources
try (InputStream s = getStdin()) {
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
"""
)
);
}

}