Skip to content

Commit 72a9dff

Browse files
Java ConvertToARM fix: Fix case where existing variable (without declaration) is guarded and other variables are used outside the new block
Example: ============== Source ============== 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(); } } ============== Result ============== import java.io.ByteArrayOutputStream; import java.io.InputStream; public class Test { public static byte[] test(InputStream input) throws Exception { 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(); } } ==================================== Problem is here, that the result variable is used outside the new try block, but the declaration is not written. Closes: #9046
1 parent 190f8f3 commit 72a9dff

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

java/java.hints/src/org/netbeans/modules/java/hints/jdk/ConvertToARM.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,16 @@ private static BlockTree rewriteOwnerBlock(
612612
final List<StatementTree> statements = new ArrayList<StatementTree>(originalStatements.size());
613613
int state = var != null ? 0 : 1; //0 - ordinary,1 - replace by try, 2 - remove
614614
final Set<Tree> toRemove = new HashSet<Tree>(oldStms);
615+
boolean preVarDeclsWritten = false;
615616
for (StatementTree statement : originalStatements) {
616617
if (removeStms.contains(statement)) {
617618
continue;
618619
}
619620
if (var == statement) {
620-
statements.addAll(preVarDecls);
621+
if (!preVarDeclsWritten) {
622+
preVarDeclsWritten = true;
623+
statements.addAll(preVarDecls);
624+
}
621625
if (var.getKind() == Kind.TRY) {
622626
gen.copyComments(statement, newTry, true);
623627
gen.copyComments(statement, newTry, false);
@@ -658,6 +662,10 @@ private static BlockTree rewriteOwnerBlock(
658662
gen.copyComments(tt.getFinallyBlock(), nt, false);
659663
}
660664
}
665+
if(! preVarDeclsWritten) {
666+
preVarDeclsWritten = true;
667+
statements.addAll(preVarDecls);
668+
}
661669
statement = newTry;
662670
} else if (state == 2) {
663671
if (!toRemove.contains(statement)) {
@@ -667,6 +675,7 @@ private static BlockTree rewriteOwnerBlock(
667675
}
668676
statements.add(statement);
669677
}
678+
670679
return tm.Block(statements, false);
671680
}
672681

java/java.hints/test/unit/src/org/netbeans/modules/java/hints/jdk/ConvertToARMTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,4 +1554,54 @@ public void testPreserveComments228141() throws Exception {
15541554
}
15551555
}
15561556
}
1557+
1558+
// Case:
1559+
public void testGH9046() throws Exception {
1560+
HintTest
1561+
.create()
1562+
.input("package test;\n"
1563+
+ "import java.io.ByteArrayOutputStream;\n"
1564+
+ "import java.io.InputStream;\n"
1565+
+ "\n"
1566+
+ "public class Test {\n"
1567+
+ "\n"
1568+
+ " public static byte[] test(InputStream input) throws Exception {\n"
1569+
+ " ByteArrayOutputStream outStream = new ByteArrayOutputStream();\n"
1570+
+ " int len;\n"
1571+
+ "\n"
1572+
+ " byte[] buffer = new byte[1024];\n"
1573+
+ "\n"
1574+
+ " while ((len = input.read(buffer)) != -1) {\n"
1575+
+ " outStream.write(buffer, 0, len);\n"
1576+
+ " }\n"
1577+
+ " input.close();\n"
1578+
+ "\n"
1579+
+ " return outStream.toByteArray();\n"
1580+
+ " }\n"
1581+
+ "}\n")
1582+
.sourceLevel("17")
1583+
.run(ConvertToARM.class)
1584+
.findWarning("12:22-12:27:verifier:TXT_ConvertToARM")
1585+
.applyFix("TXT_ConvertToARM")
1586+
.assertOutput("package test;\n"
1587+
+ "import java.io.ByteArrayOutputStream;\n"
1588+
+ "import java.io.InputStream;\n"
1589+
+ "\n"
1590+
+ "public class Test {\n"
1591+
+ "\n"
1592+
+ " public static byte[] test(InputStream input) throws Exception {\n"
1593+
+ " ByteArrayOutputStream outStream;\n"
1594+
+ " try (input) {\n"
1595+
+ " outStream = new ByteArrayOutputStream();\n"
1596+
+ " int len;\n"
1597+
+ " byte[] buffer = new byte[1024];\n"
1598+
+ " while ((len = input.read(buffer)) != -1) {\n"
1599+
+ " outStream.write(buffer, 0, len);\n"
1600+
+ " }\n"
1601+
+ " }\n"
1602+
+ "\n"
1603+
+ " return outStream.toByteArray();\n"
1604+
+ " }\n"
1605+
+ "}\n");
1606+
}
15571607
}

0 commit comments

Comments
 (0)