Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not remove try block if there are resources #161

Merged
merged 5 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,16 +18,14 @@
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;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.format.ShiftFormat;
import org.openrewrite.java.style.EmptyBlockStyle;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Space;
import org.openrewrite.java.tree.Statement;
import org.openrewrite.java.tree.*;
import org.openrewrite.marker.Markers;

import java.util.ArrayList;
Expand Down Expand Up @@ -99,7 +97,7 @@ public J.Try visitTry(J.Try tryable, P p) {

if (Boolean.TRUE.equals(emptyBlockStyle.getLiteralTry()) &&
isEmptyBlock(t.getBody()) &&
(t.getResources() == null || t.getResources().isEmpty())) {
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 @@ -223,6 +221,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;
joanvr marked this conversation as resolved.
Show resolved Hide resolved
}

private static class ExtractSideEffectsOfIfCondition<P> extends JavaVisitor<P> {
private final J.Block enclosingBlock;
private final J.If toExtract;
Expand Down
51 changes: 50 additions & 1 deletion src/test/java/org/openrewrite/staticanalysis/EmptyBlockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public class A {
}

@Test
void emptyTryWithResources() {
void emptyTryWithResourcesWithExternalResources() {
rewriteRun(
//language=java
java(
Expand All @@ -437,4 +437,53 @@ public void destroy() {
)
);
}

@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);
}
}
}
"""
)
);
}

}