Skip to content
Open
Show file tree
Hide file tree
Changes from all 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 @@ -612,12 +612,16 @@ private static BlockTree rewriteOwnerBlock(
final List<StatementTree> statements = new ArrayList<StatementTree>(originalStatements.size());
int state = var != null ? 0 : 1; //0 - ordinary,1 - replace by try, 2 - remove
final Set<Tree> toRemove = new HashSet<Tree>(oldStms);
boolean preVarDeclsWritten = false;
for (StatementTree statement : originalStatements) {
if (removeStms.contains(statement)) {
continue;
}
if (var == statement) {
statements.addAll(preVarDecls);
if (!preVarDeclsWritten) {
preVarDeclsWritten = true;
statements.addAll(preVarDecls);
}
if (var.getKind() == Kind.TRY) {
gen.copyComments(statement, newTry, true);
gen.copyComments(statement, newTry, false);
Expand Down Expand Up @@ -658,6 +662,10 @@ private static BlockTree rewriteOwnerBlock(
gen.copyComments(tt.getFinallyBlock(), nt, false);
}
}
if (!preVarDeclsWritten) {
preVarDeclsWritten = true;
statements.addAll(preVarDecls);
}
statement = newTry;
} else if (state == 2) {
if (!toRemove.contains(statement)) {
Expand All @@ -667,6 +675,7 @@ private static BlockTree rewriteOwnerBlock(
}
statements.add(statement);
}

return tm.Block(statements, false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1554,4 +1554,58 @@ public void testPreserveComments228141() throws Exception {
}
}
}

// Case:
public void testGH9046() throws Exception {
HintTest
.create()
.input("""
package test;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Test {

public static byte[] test(InputStream input) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
int len;

byte[] buffer = new byte[1024];

while ((len = input.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
input.close();

return outStream.toByteArray();
}
}
""")
.sourceLevel("17")
.run(ConvertToARM.class)
.findWarning("12:22-12:27:verifier:TXT_ConvertToARM")
.applyFix("TXT_ConvertToARM")
.assertOutput("""
package test;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class Test {

public static byte[] test(InputStream input) throws Exception {
ByteArrayOutputStream outStream;
try (input) {
outStream = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[1024];
while ((len = input.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
}

return outStream.toByteArray();
}
}
""");
}
}
Loading