Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStephan Herrmann2019-12-26 12:18:14 -0500
committerStephan Herrmann2019-12-26 12:18:14 -0500
commit3f16176dca63531a876ad5417969b1b88503c387 (patch)
tree018a662c9e8f8ea0f3e8ba3823d3fd86ee3fceec
parent290d202454cc7d9e26a9e0edec925d6c26879a87 (diff)
downloadeclipse.jdt.core-3f16176dca63531a876ad5417969b1b88503c387.tar.gz
eclipse.jdt.core-3f16176dca63531a876ad5417969b1b88503c387.tar.xz
eclipse.jdt.core-3f16176dca63531a876ad5417969b1b88503c387.zip
Bug 552441 - "Potential resource leak" erroneously reported withinI20191226-1800
for-loop - test case Change-Id: I31f142ba437d885a616ab810aeab46b0e2d6d667
-rw-r--r--org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java65
1 files changed, 65 insertions, 0 deletions
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java
index 6811316c3c..7a51008e99 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/ResourceLeakTests.java
@@ -6026,4 +6026,69 @@ public void testBug519740() {
},
options);
}
+public void testBug552441() {
+ if (this.complianceLevel < ClassFileConstants.JDK1_7) return; // uses try-with-resources
+
+ Map options = getCompilerOptions();
+ options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
+ options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
+
+ runConformTest(
+ new String[] {
+ "Test.java",
+ "import java.io.BufferedOutputStream;\n" +
+ "import java.io.FileOutputStream;\n" +
+ "import java.io.IOException;\n" +
+ "import java.io.OutputStream;\n" +
+ "import java.util.concurrent.atomic.AtomicLong;\n" +
+ "\n" +
+ "public class Test {\n" +
+ " public static class CountingBufferedOutputStream extends BufferedOutputStream {\n" +
+ " private final AtomicLong bytesWritten;\n" +
+ "\n" +
+ " public CountingBufferedOutputStream(OutputStream out, AtomicLong bytesWritten) throws IOException {\n" +
+ " super(out);\n" +
+ " this.bytesWritten = bytesWritten;\n" +
+ " }\n" +
+ "\n" +
+ " @Override\n" +
+ " public void write(byte[] b) throws IOException {\n" +
+ " super.write(b);\n" +
+ " bytesWritten.addAndGet(b.length);\n" +
+ " }\n" +
+ "\n" +
+ " @Override\n" +
+ " public void write(byte[] b, int off, int len) throws IOException {\n" +
+ " super.write(b, off, len);\n" +
+ " bytesWritten.addAndGet(len);\n" +
+ " }\n" +
+ "\n" +
+ " @Override\n" +
+ " public synchronized void write(int b) throws IOException {\n" +
+ " super.write(b);\n" +
+ " bytesWritten.incrementAndGet();\n" +
+ " }\n" +
+ " }\n" +
+ "\n" +
+ " public static void test(String[] args) throws IOException {\n" +
+ " AtomicLong uncompressedBytesOut = new AtomicLong();\n" +
+ " int val = 0;\n" +
+ " try (CountingBufferedOutputStream out = new CountingBufferedOutputStream(\n" +
+ " new FileOutputStream(\"outputfile\"), uncompressedBytesOut)) {\n" +
+ "\n" +
+ " for (int i = 0; i < 1; i++) {\n" +
+ " if (val > 2) {\n" +
+ " throw new RuntimeException(\"X\");\n" +
+ " }\n" +
+ " }\n" +
+ " if (val > 2) {\n" +
+ " throw new RuntimeException(\"Y\");\n" +
+ " }\n" +
+ " throw new RuntimeException(\"Z\");\n" +
+ " }\n" +
+ " }\n" +
+ "}\n"
+ },
+ options);
+}
}

Back to the top