Skip to content

Commit 93b080e

Browse files
Merge every closed resource of a try in one pass in UseTryWithResources (#1105)
A try whose finally closes both an adjacent and a non-adjacent declaration had only the adjacent one merged per cycle, so the recipe needed a second cycle to add the other. Apply the non-adjacent merge after the adjacent one, and repeat it for each remaining close in the finally.
1 parent 80ecf94 commit 93b080e

2 files changed

Lines changed: 18 additions & 15 deletions

File tree

‎src/main/java/org/openrewrite/staticanalysis/UseTryWithResources.java‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,30 @@ public J.Block visitBlock(J.Block block, ExecutionContext ctx) {
8282
J.VariableDeclarations prevDecl = (J.VariableDeclarations) stmts.get(i - 1);
8383
if (canTransform(prevDecl, tryStmt)) {
8484
boolean usedAfter = isUsedAfter(prevDecl.getVariables().get(0).getSimpleName(), stmts, i);
85-
if (usedAfter) {
86-
return transformJava9(prevDecl, tryStmt);
87-
}
88-
return transform(prevDecl, tryStmt, unnamedAvailable);
85+
tryStmt = usedAfter ?
86+
transformJava9(prevDecl, tryStmt) :
87+
transform(prevDecl, tryStmt, unnamedAvailable);
8988
}
9089
}
9190

92-
// Case 2: Non-consecutive — varDecl separated by intervening statements
93-
if (tryStmt.getFinally() != null) {
91+
// Case 2: Non-consecutive — varDecl separated by intervening statements.
92+
// Each merge strips a close from the finally, so the loop ends.
93+
while (tryStmt.getFinally() != null) {
9494
String closedVar = extractClosedVarName(tryStmt.getFinally());
95-
if (closedVar != null) {
96-
int declIdx = findMatchingVarDecl(stmts, i, closedVar);
97-
if (declIdx >= 0 && declIdx < i - 1) {
98-
J.VariableDeclarations varDecl = (J.VariableDeclarations) stmts.get(declIdx);
99-
if (canTransform(varDecl, tryStmt) && !isReassignedBetween(closedVar, stmts, declIdx, i)) {
100-
return transformJava9(varDecl, tryStmt);
101-
}
102-
}
95+
if (closedVar == null) {
96+
break;
10397
}
98+
int declIdx = findMatchingVarDecl(stmts, i, closedVar);
99+
if (declIdx < 0 || declIdx >= i - 1) {
100+
break;
101+
}
102+
J.VariableDeclarations varDecl = (J.VariableDeclarations) stmts.get(declIdx);
103+
if (!canTransform(varDecl, tryStmt) || isReassignedBetween(closedVar, stmts, declIdx, i)) {
104+
break;
105+
}
106+
tryStmt = transformJava9(varDecl, tryStmt);
104107
}
108+
return tryStmt;
105109
}
106110
// Remove varDecl that was merged into the following try-with-resources
107111
if (stmt instanceof J.VariableDeclarations && i + 1 < stmts.size() &&

‎src/test/java/org/openrewrite/staticanalysis/UseTryWithResourcesTest.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ void method(String path) throws IOException {
612612
@Test
613613
void multipleClosesInFinallyPicksMatchingVariable() {
614614
rewriteRun(
615-
spec -> spec.expectedCyclesThatMakeChanges(2),
616615
//language=java
617616
java(
618617
"""

0 commit comments

Comments
 (0)