Skip to content

TryWithResources recipe#593

Closed
knutwannheden wants to merge 1 commit intomainfrom
try-with-resources
Closed

TryWithResources recipe#593
knutwannheden wants to merge 1 commit intomainfrom
try-with-resources

Conversation

@knutwannheden
Copy link
Contributor

@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Jun 13, 2025
Copy link
Contributor

@Pankraz76 Pankraz76 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amazing thanks, already a solid solution/foundation to increment.

seems to achieve MVP, so would ship to gain feedback.

feel free to join in @iddeepak.

private J.Block processBlock(J.Block body) {
// Find all try blocks in the method body
List<J.Try> tryBlocks = new ArrayList<>();
findTryBlocks(body, tryBlocks);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
findTryBlocks(body, tryBlocks);
List<J.Try> tryBlocks = collectTryBlocks(body);


for (Statement statement : finallyBlock.getStatements()) {
boolean shouldKeep = true;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (res != null) { } block also removes any other side-effects inside it such as logging or metrics.
@Pankraz76 clarify here!

Comment on lines +69 to +74
List<J.VariableDeclarations> variableDeclarations = new ArrayList<>();
for (Statement statement : t.getBody().getStatements()) {
if (statement instanceof J.VariableDeclarations) {
variableDeclarations.add((J.VariableDeclarations) statement);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
List<J.VariableDeclarations> variableDeclarations = new ArrayList<>();
for (Statement statement : t.getBody().getStatements()) {
if (statement instanceof J.VariableDeclarations) {
variableDeclarations.add((J.VariableDeclarations) statement);
}
}
List<J.VariableDeclarations> variableDeclarations = collectVariableDeclarations(t.getBody());

J.Try t = super.visitTry(tryable, ctx);

// Only process try blocks with a finally block
if (t.getFinally() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (t.getFinally() == null) {
if (Objects.isNull(t.getFinally())) {

Map<String, Expression> resourceInitializers = findResourceInitializers(t, resourcesThatAreClosed.keySet());

// Transform the try block to use try-with-resources
return transformToTryWithResources(t, resourcesThatAreClosed, resourceInitializers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return transformToTryWithResources(t, resourcesThatAreClosed, resourceInitializers);
return transformToTryWithResources(t, resourcesThatAreClosed, findResourceInitializers(t, resourcesThatAreClosed.keySet()));

just a suggestion to keep it inline

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pankraz76 clarify here! Is inline better ?

J.Block newBody = body;
for (J.Try tryBlock : tryBlocks) {
// Only process try blocks with a finally block
if (tryBlock.getFinally() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (tryBlock.getFinally() == null) {
if (Objects.isNull(t.getFinally())) {

Map<String, Expression> resourceInitializers = findResourceInitializers(tryBlock, resourcesThatAreClosed.keySet());

// Transform the try block to use try-with-resources
J.Try newTryBlock = transformToTryWithResources(tryBlock, resourcesThatAreClosed, resourceInitializers);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
J.Try newTryBlock = transformToTryWithResources(tryBlock, resourcesThatAreClosed, resourceInitializers);
J.Try newTryBlock = transformToTryWithResources(tryBlock, resourcesThatAreClosed, findResourceInitializers(tryBlock, resourcesThatAreClosed.keySet()));

@Pankraz76 clarify here! Is inline better ?

return false;
}

private J.Try transformToTryWithResources(J.Try tryable, Map<String, J.VariableDeclarations> resourcesThatAreClosed, Map<String, Expression> resourceInitializers) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one method looks big can we break it down ?
@Pankraz76

);
}

@Test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@Test
@Test
void renameIgnoredIfUnused() {
rewriteRun(
java(
"""
import java.io.*;
public void testConnection() throws MessagingException {
Transport transport = null;
try {
transport = connectTransport();
}
finally {
if (transport != null) {
transport.close();
}
}
}
""",
"""
import java.io.*;
class Test {
public void testConnection() throws MessagingException {
try (Transport ignored = connectTransport()) {
}
}
}
}
"""
)
);
}
@Test

imports missing but this is kind of an edge case missing:

@iddeepak
Copy link
Contributor

@knutwannheden @Pankraz76
You guys are already a gem.....Thank you for giving me a chance to contribute! Just few small suggestions from my side.

@knutwannheden
Copy link
Contributor Author

I am not planning on making any changes to this PR nor merging it as-is. Feel free to use it as a starting point for a PR.

If you spot problems, ai suggest you create a test that show cases the bug and then you can fix it.

@iddeepak iddeepak mentioned this pull request Jun 20, 2025
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Jun 20, 2025
@knutwannheden knutwannheden deleted the try-with-resources branch June 20, 2025 03:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

3 participants

Comments