|
Added
Link Here
|
| 1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2011, 2012 GK Software AG and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* Stephan Herrmann - initial API and implementation |
| 10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.jdt.core.tests.compiler.regression; |
| 12 |
|
| 13 |
import java.io.IOException; |
| 14 |
import java.net.URL; |
| 15 |
import java.util.Map; |
| 16 |
|
| 17 |
import junit.framework.Test; |
| 18 |
|
| 19 |
import org.eclipse.core.runtime.FileLocator; |
| 20 |
import org.eclipse.core.runtime.Path; |
| 21 |
import org.eclipse.core.runtime.Platform; |
| 22 |
import org.eclipse.jdt.core.JavaCore; |
| 23 |
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; |
| 24 |
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; |
| 25 |
|
| 26 |
public class ResourceLeakTests extends AbstractRegressionTest { |
| 27 |
|
| 28 |
static { |
| 29 |
// TESTS_NAMES = new String[] { "test066"}; |
| 30 |
// TESTS_NUMBERS = new int[] { 50 }; |
| 31 |
// TESTS_RANGE = new int[] { 11, -1 }; |
| 32 |
} |
| 33 |
public ResourceLeakTests(String name) { |
| 34 |
super(name); |
| 35 |
} |
| 36 |
public static Test suite() { |
| 37 |
return buildAllCompliancesTestSuite(ResourceLeakTests.class); |
| 38 |
} |
| 39 |
|
| 40 |
void runTestsExpectingErrorsOnlyIn17(String[] testFiles, String errorsIn17, Map options) { |
| 41 |
if (this.complianceLevel >= ClassFileConstants.JDK1_7) |
| 42 |
runNegativeTest(testFiles, errorsIn17, null, true, options); |
| 43 |
else |
| 44 |
runConformTest(testFiles, "", null, true, null, options, null); |
| 45 |
} |
| 46 |
|
| 47 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 48 |
// a method uses an AutoCloseable without ever closing it. |
| 49 |
public void test056() { |
| 50 |
Map options = getCompilerOptions(); |
| 51 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 52 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
| 53 |
this.runNegativeTest( |
| 54 |
new String[] { |
| 55 |
"X.java", |
| 56 |
"import java.io.File;\n" + |
| 57 |
"import java.io.FileReader;\n" + |
| 58 |
"import java.io.IOException;\n" + |
| 59 |
"public class X {\n" + |
| 60 |
" void foo() throws IOException {\n" + |
| 61 |
" File file = new File(\"somefile\");\n" + |
| 62 |
" FileReader fileReader = new FileReader(file);\n" + |
| 63 |
// not invoking any methods on FileReader, try to avoid necessary call to superclass() in the compiler |
| 64 |
// " char[] in = new char[50];\n" + |
| 65 |
// " fileReader.read(in);\n" + |
| 66 |
" }\n" + |
| 67 |
" public static void main(String[] args) throws IOException {\n" + |
| 68 |
" new X().foo();\n" + |
| 69 |
" }\n" + |
| 70 |
"}\n" |
| 71 |
}, |
| 72 |
"----------\n" + |
| 73 |
"1. ERROR in X.java (at line 7)\n" + |
| 74 |
" FileReader fileReader = new FileReader(file);\n" + |
| 75 |
" ^^^^^^^^^^\n" + |
| 76 |
"Resource leak: 'fileReader' is never closed\n" + |
| 77 |
"----------\n", |
| 78 |
null, |
| 79 |
true, |
| 80 |
options); |
| 81 |
} |
| 82 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 83 |
// a method uses an AutoCloseable and closes it but not protected by t-w-r nor regular try-finally |
| 84 |
public void test056a() { |
| 85 |
Map options = getCompilerOptions(); |
| 86 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 87 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 88 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
| 89 |
runTestsExpectingErrorsOnlyIn17( |
| 90 |
new String[] { |
| 91 |
"X.java", |
| 92 |
"import java.io.File;\n" + |
| 93 |
"import java.io.FileReader;\n" + |
| 94 |
"import java.io.IOException;\n" + |
| 95 |
"public class X {\n" + |
| 96 |
" void foo() throws IOException {\n" + |
| 97 |
" File file = new File(\"somefile\");\n" + |
| 98 |
" FileReader fileReader = new FileReader(file);\n" + |
| 99 |
" char[] in = new char[50];\n" + |
| 100 |
" fileReader.read(in);\n" + |
| 101 |
" fileReader.close();\n" + |
| 102 |
" }\n" + |
| 103 |
"}\n" |
| 104 |
}, |
| 105 |
"----------\n" + |
| 106 |
"1. ERROR in X.java (at line 7)\n" + |
| 107 |
" FileReader fileReader = new FileReader(file);\n" + |
| 108 |
" ^^^^^^^^^^\n" + |
| 109 |
"Resource 'fileReader' should be managed by try-with-resource\n" + |
| 110 |
"----------\n", |
| 111 |
options); |
| 112 |
} |
| 113 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 114 |
// a method uses an AutoCloseable and closes it properly in a finally block |
| 115 |
public void test056b() { |
| 116 |
Map options = getCompilerOptions(); |
| 117 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 118 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 119 |
this.runConformTest( |
| 120 |
new String[] { |
| 121 |
"X.java", |
| 122 |
"import java.io.File;\n" + |
| 123 |
"import java.io.FileReader;\n" + |
| 124 |
"import java.io.IOException;\n" + |
| 125 |
"public class X {\n" + |
| 126 |
" void foo() throws IOException {\n" + |
| 127 |
" File file = new File(\"somefile\");\n" + |
| 128 |
" FileReader fileReader = new FileReader(file);\n" + |
| 129 |
" try {\n" + |
| 130 |
" char[] in = new char[50];\n" + |
| 131 |
" fileReader.read(in);\n" + |
| 132 |
" } finally {\n" + |
| 133 |
" fileReader.close();\n" + |
| 134 |
" }\n" + |
| 135 |
" }\n" + |
| 136 |
" public static void main(String[] args) {\n" + |
| 137 |
" try {\n" + |
| 138 |
" new X().foo();\n" + |
| 139 |
" } catch (IOException ioex) {\n" + |
| 140 |
" System.out.println(\"caught\");\n" + |
| 141 |
" }\n" + |
| 142 |
" }\n" + |
| 143 |
"}\n" |
| 144 |
}, |
| 145 |
"caught", /*output*/ |
| 146 |
null/*classLibs*/, |
| 147 |
true/*shouldFlush*/, |
| 148 |
null/*vmargs*/, |
| 149 |
options, |
| 150 |
null/*requestor*/); |
| 151 |
} |
| 152 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 153 |
// a method uses an AutoCloseable properly within try-with-resources. |
| 154 |
public void test056c() { |
| 155 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; // t-w-r used |
| 156 |
Map options = getCompilerOptions(); |
| 157 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 158 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
| 159 |
this.runConformTest( |
| 160 |
new String[] { |
| 161 |
"X.java", |
| 162 |
"import java.io.File;\n" + |
| 163 |
"import java.io.FileReader;\n" + |
| 164 |
"import java.io.IOException;\n" + |
| 165 |
"public class X {\n" + |
| 166 |
" void foo() throws IOException {\n" + |
| 167 |
" File file = new File(\"somefile\");\n" + |
| 168 |
" try (FileReader fileReader = new FileReader(file)) {\n" + |
| 169 |
" char[] in = new char[50];\n" + |
| 170 |
" fileReader.read(in);\n" + |
| 171 |
" }\n" + |
| 172 |
" }\n" + |
| 173 |
" public static void main(String[] args) {\n" + |
| 174 |
" try {\n" + |
| 175 |
" new X().foo();\n" + |
| 176 |
" } catch (IOException ioex) {\n" + |
| 177 |
" System.out.println(\"caught\");\n" + |
| 178 |
" }\n" + |
| 179 |
" }\n" + |
| 180 |
"}\n" |
| 181 |
}, |
| 182 |
"caught", /*output*/ |
| 183 |
null/*classLibs*/, |
| 184 |
true/*shouldFlush*/, |
| 185 |
null/*vmargs*/, |
| 186 |
options, |
| 187 |
null/*requestor*/); |
| 188 |
} |
| 189 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 190 |
// a method uses two AutoCloseables (testing independent analysis) |
| 191 |
//- one closeable may be unclosed at a conditional return |
| 192 |
//- the other is only conditionally closed |
| 193 |
public void test056d() { |
| 194 |
Map options = getCompilerOptions(); |
| 195 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 196 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
| 197 |
this.runNegativeTest( |
| 198 |
new String[] { |
| 199 |
"X.java", |
| 200 |
"import java.io.File;\n" + |
| 201 |
"import java.io.FileReader;\n" + |
| 202 |
"import java.io.IOException;\n" + |
| 203 |
"public class X {\n" + |
| 204 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
| 205 |
" File file = new File(\"somefile\");\n" + |
| 206 |
" char[] in = new char[50];\n" + |
| 207 |
" FileReader fileReader1 = new FileReader(file);\n" + |
| 208 |
" fileReader1.read(in);\n" + |
| 209 |
" FileReader fileReader2 = new FileReader(file);\n" + |
| 210 |
" fileReader2.read(in);\n" + |
| 211 |
" if (flag1) {\n" + |
| 212 |
" fileReader2.close();\n" + |
| 213 |
" return;\n" + |
| 214 |
" } else if (flag2) {\n" + |
| 215 |
" fileReader2.close();\n" + |
| 216 |
" }\n" + |
| 217 |
" fileReader1.close();\n" + |
| 218 |
" }\n" + |
| 219 |
" public static void main(String[] args) throws IOException {\n" + |
| 220 |
" new X().foo(false, true);\n" + |
| 221 |
" }\n" + |
| 222 |
"}\n" |
| 223 |
}, |
| 224 |
"----------\n" + |
| 225 |
"1. WARNING in X.java (at line 10)\n" + |
| 226 |
" FileReader fileReader2 = new FileReader(file);\n" + |
| 227 |
" ^^^^^^^^^^^\n" + |
| 228 |
"Potential resource leak: 'fileReader2' may not be closed\n" + |
| 229 |
"----------\n" + |
| 230 |
"2. ERROR in X.java (at line 14)\n" + |
| 231 |
" return;\n" + |
| 232 |
" ^^^^^^^\n" + |
| 233 |
"Resource leak: 'fileReader1' is not closed at this location\n" + |
| 234 |
"----------\n", |
| 235 |
null, |
| 236 |
true, |
| 237 |
options); |
| 238 |
} |
| 239 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 240 |
// a method uses two AutoCloseables (testing independent analysis) |
| 241 |
//- one closeable may be unclosed at a conditional return |
| 242 |
//- the other is only conditionally closed |
| 243 |
public void test056d_suppress() { |
| 244 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // annotations used |
| 245 |
Map options = getCompilerOptions(); |
| 246 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 247 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
| 248 |
options.put(CompilerOptions.OPTION_SuppressOptionalErrors, CompilerOptions.ENABLED); |
| 249 |
this.runNegativeTest( |
| 250 |
new String[] { |
| 251 |
"X.java", |
| 252 |
"import java.io.File;\n" + |
| 253 |
"import java.io.FileReader;\n" + |
| 254 |
"import java.io.IOException;\n" + |
| 255 |
"public class X {\n" + |
| 256 |
" void foo(boolean flag1, boolean flag2) throws IOException {\n" + |
| 257 |
" @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + |
| 258 |
" char[] in = new char[50];\n" + |
| 259 |
" FileReader fileReader1 = new FileReader(file);\n" + |
| 260 |
" fileReader1.read(in);\n" + |
| 261 |
" @SuppressWarnings(\"resource\") FileReader fileReader2 = new FileReader(file); // useful suppress\n" + |
| 262 |
" fileReader2.read(in);\n" + |
| 263 |
" if (flag1) {\n" + |
| 264 |
" fileReader2.close();\n" + |
| 265 |
" return; // not suppressed\n" + |
| 266 |
" } else if (flag2) {\n" + |
| 267 |
" fileReader2.close();\n" + |
| 268 |
" }\n" + |
| 269 |
" fileReader1.close();\n" + |
| 270 |
" }\n" + |
| 271 |
" @SuppressWarnings(\"resource\") // useful suppress\n" + |
| 272 |
" void bar() throws IOException {\n" + |
| 273 |
" File file = new File(\"somefile\");\n" + |
| 274 |
" FileReader fileReader = new FileReader(file);\n" + |
| 275 |
" char[] in = new char[50];\n" + |
| 276 |
" fileReader.read(in);\n" + |
| 277 |
" }\n" + |
| 278 |
" public static void main(String[] args) throws IOException {\n" + |
| 279 |
" new X().foo(false, true);\n" + |
| 280 |
" }\n" + |
| 281 |
"}\n" |
| 282 |
}, |
| 283 |
"----------\n" + |
| 284 |
"1. WARNING in X.java (at line 6)\n" + |
| 285 |
" @SuppressWarnings(\"resource\") File file = new File(\"somefile\"); // unnecessary suppress\n" + |
| 286 |
" ^^^^^^^^^^\n" + |
| 287 |
"Unnecessary @SuppressWarnings(\"resource\")\n" + |
| 288 |
"----------\n" + |
| 289 |
"2. ERROR in X.java (at line 14)\n" + |
| 290 |
" return; // not suppressed\n" + |
| 291 |
" ^^^^^^^\n" + |
| 292 |
"Resource leak: 'fileReader1' is not closed at this location\n" + |
| 293 |
"----------\n", |
| 294 |
null, |
| 295 |
true, |
| 296 |
options); |
| 297 |
} |
| 298 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 299 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
| 300 |
// one method returns an AutoCleasble, a second method uses this object without ever closing it. |
| 301 |
public void test056e() { |
| 302 |
Map options = getCompilerOptions(); |
| 303 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 304 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 305 |
this.runNegativeTest( |
| 306 |
new String[] { |
| 307 |
"X.java", |
| 308 |
"import java.io.File;\n" + |
| 309 |
"import java.io.FileReader;\n" + |
| 310 |
"import java.io.IOException;\n" + |
| 311 |
"public class X {\n" + |
| 312 |
" FileReader getReader(String filename) throws IOException {\n" + |
| 313 |
" File file = new File(\"somefile\");\n" + |
| 314 |
" FileReader fileReader = new FileReader(file);\n" + |
| 315 |
" return fileReader;\n" + // don't complain here, pass responsibility to caller |
| 316 |
" }\n" + |
| 317 |
" void foo() throws IOException {\n" + |
| 318 |
" FileReader reader = getReader(\"somefile\");\n" + |
| 319 |
" char[] in = new char[50];\n" + |
| 320 |
" reader.read(in);\n" + |
| 321 |
" }\n" + |
| 322 |
" public static void main(String[] args) throws IOException {\n" + |
| 323 |
" new X().foo();\n" + |
| 324 |
" }\n" + |
| 325 |
"}\n" |
| 326 |
}, |
| 327 |
"----------\n" + |
| 328 |
"1. ERROR in X.java (at line 11)\n" + |
| 329 |
" FileReader reader = getReader(\"somefile\");\n" + |
| 330 |
" ^^^^^^\n" + |
| 331 |
"Potential resource leak: \'reader\' may not be closed\n" + |
| 332 |
"----------\n", |
| 333 |
null, |
| 334 |
true, |
| 335 |
options); |
| 336 |
} |
| 337 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 338 |
// a method explicitly closes its AutoCloseable rather than using t-w-r |
| 339 |
public void test056f() { |
| 340 |
Map options = getCompilerOptions(); |
| 341 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 342 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
| 343 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
| 344 |
runTestsExpectingErrorsOnlyIn17( |
| 345 |
new String[] { |
| 346 |
"X.java", |
| 347 |
"import java.io.File;\n" + |
| 348 |
"import java.io.FileReader;\n" + |
| 349 |
"import java.io.IOException;\n" + |
| 350 |
"public class X {\n" + |
| 351 |
" void foo() throws IOException {\n" + |
| 352 |
" File file = new File(\"somefile\");\n" + |
| 353 |
" FileReader fileReader = null;\n" + |
| 354 |
" try {\n" + |
| 355 |
" fileReader = new FileReader(file);\n" + |
| 356 |
" char[] in = new char[50];\n" + |
| 357 |
" fileReader.read(in);\n" + |
| 358 |
" } finally {\n" + |
| 359 |
" fileReader.close();\n" + |
| 360 |
" }\n" + |
| 361 |
" }\n" + |
| 362 |
"}\n" |
| 363 |
}, |
| 364 |
"----------\n" + |
| 365 |
"1. ERROR in X.java (at line 7)\n" + |
| 366 |
" FileReader fileReader = null;\n" + |
| 367 |
" ^^^^^^^^^^\n" + |
| 368 |
"Resource 'fileReader' should be managed by try-with-resource\n" + |
| 369 |
"----------\n", |
| 370 |
options); |
| 371 |
} |
| 372 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 373 |
// an AutoCloseable local is re-assigned |
| 374 |
public void test056g() { |
| 375 |
Map options = getCompilerOptions(); |
| 376 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 377 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 378 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 379 |
this.runNegativeTest( |
| 380 |
new String[] { |
| 381 |
"X.java", |
| 382 |
"import java.io.File;\n" + |
| 383 |
"import java.io.FileReader;\n" + |
| 384 |
"import java.io.IOException;\n" + |
| 385 |
"public class X {\n" + |
| 386 |
" void foo() throws IOException {\n" + |
| 387 |
" File file = new File(\"somefile\");\n" + |
| 388 |
" FileReader fileReader = new FileReader(file);\n" + |
| 389 |
" char[] in = new char[50];\n" + |
| 390 |
" fileReader.read(in);\n" + |
| 391 |
" fileReader = new FileReader(file);\n" + |
| 392 |
" fileReader.read(in);\n" + |
| 393 |
" fileReader.close();\n" + |
| 394 |
" fileReader = null;\n" + |
| 395 |
" }\n" + |
| 396 |
" public static void main(String[] args) throws IOException {\n" + |
| 397 |
" new X().foo();\n" + |
| 398 |
" }\n" + |
| 399 |
"}\n" |
| 400 |
}, |
| 401 |
"----------\n" + |
| 402 |
"1. ERROR in X.java (at line 10)\n" + |
| 403 |
" fileReader = new FileReader(file);\n" + |
| 404 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 405 |
"Resource leak: 'fileReader' is not closed at this location\n" + |
| 406 |
"----------\n", |
| 407 |
null, |
| 408 |
true, |
| 409 |
options); |
| 410 |
} |
| 411 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 412 |
// an AutoCloseable local is re-assigned after null-assigned |
| 413 |
public void test056g2() { |
| 414 |
Map options = getCompilerOptions(); |
| 415 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 416 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 417 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 418 |
this.runNegativeTest( |
| 419 |
new String[] { |
| 420 |
"X.java", |
| 421 |
"import java.io.File;\n" + |
| 422 |
"import java.io.FileReader;\n" + |
| 423 |
"import java.io.IOException;\n" + |
| 424 |
"public class X {\n" + |
| 425 |
" void foo() throws IOException {\n" + |
| 426 |
" File file = new File(\"somefile\");\n" + |
| 427 |
" FileReader fileReader = new FileReader(file);\n" + |
| 428 |
" char[] in = new char[50];\n" + |
| 429 |
" fileReader.read(in);\n" + |
| 430 |
" fileReader = null;\n" + |
| 431 |
" fileReader = new FileReader(file);\n" + // don't complain again, fileReader is null, so nothing can leak here |
| 432 |
" fileReader.read(in);\n" + |
| 433 |
" fileReader.close();\n" + |
| 434 |
" }\n" + |
| 435 |
" public static void main(String[] args) throws IOException {\n" + |
| 436 |
" new X().foo();\n" + |
| 437 |
" }\n" + |
| 438 |
"}\n" |
| 439 |
}, |
| 440 |
"----------\n" + |
| 441 |
"1. ERROR in X.java (at line 10)\n" + |
| 442 |
" fileReader = null;\n" + |
| 443 |
" ^^^^^^^^^^^^^^^^^\n" + |
| 444 |
"Resource leak: 'fileReader' is not closed at this location\n" + |
| 445 |
"----------\n", |
| 446 |
null, |
| 447 |
true, |
| 448 |
options); |
| 449 |
} |
| 450 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 451 |
// two AutoCloseables at different nesting levels (anonymous local type) |
| 452 |
public void test056h() { |
| 453 |
Map options = getCompilerOptions(); |
| 454 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 455 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 456 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 457 |
this.runNegativeTest( |
| 458 |
new String[] { |
| 459 |
"X.java", |
| 460 |
"import java.io.File;\n" + |
| 461 |
"import java.io.FileReader;\n" + |
| 462 |
"import java.io.IOException;\n" + |
| 463 |
"public class X {\n" + |
| 464 |
" void foo() throws IOException {\n" + |
| 465 |
" final File file = new File(\"somefile\");\n" + |
| 466 |
" final FileReader fileReader = new FileReader(file);\n" + |
| 467 |
" char[] in = new char[50];\n" + |
| 468 |
" fileReader.read(in);\n" + |
| 469 |
" new Runnable() {\n public void run() {\n" + |
| 470 |
" try {\n" + |
| 471 |
" fileReader.close();\n" + |
| 472 |
" FileReader localReader = new FileReader(file);\n" + |
| 473 |
" } catch (IOException ex) { /* nop */ }\n" + |
| 474 |
" }}.run();\n" + |
| 475 |
" }\n" + |
| 476 |
" public static void main(String[] args) throws IOException {\n" + |
| 477 |
" new X().foo();\n" + |
| 478 |
" }\n" + |
| 479 |
"}\n" |
| 480 |
}, |
| 481 |
"----------\n" + |
| 482 |
"1. WARNING in X.java (at line 7)\n" + |
| 483 |
" final FileReader fileReader = new FileReader(file);\n" + |
| 484 |
" ^^^^^^^^^^\n" + |
| 485 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
| 486 |
"----------\n" + |
| 487 |
"2. ERROR in X.java (at line 14)\n" + |
| 488 |
" FileReader localReader = new FileReader(file);\n" + |
| 489 |
" ^^^^^^^^^^^\n" + |
| 490 |
"Resource leak: 'localReader' is never closed\n" + |
| 491 |
"----------\n", |
| 492 |
null, |
| 493 |
true, |
| 494 |
options); |
| 495 |
} |
| 496 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 497 |
// three AutoCloseables in different blocks of the same method |
| 498 |
public void test056i() { |
| 499 |
Map options = getCompilerOptions(); |
| 500 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 501 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 502 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 503 |
this.runNegativeTest( |
| 504 |
new String[] { |
| 505 |
"X.java", |
| 506 |
"import java.io.File;\n" + |
| 507 |
"import java.io.FileReader;\n" + |
| 508 |
"import java.io.IOException;\n" + |
| 509 |
"public class X {\n" + |
| 510 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
| 511 |
" File file = new File(\"somefile\");\n" + |
| 512 |
" if (f1) {\n" + |
| 513 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
| 514 |
" char[] in = new char[50];\n" + |
| 515 |
" fileReader.read(in);\n" + |
| 516 |
" while (true) {\n" + |
| 517 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
| 518 |
" loopReader.close();" + |
| 519 |
" break;\n" + |
| 520 |
" }\n" + |
| 521 |
" } else {\n" + |
| 522 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
| 523 |
" if (f2)\n" + |
| 524 |
" fileReader.close();\n" + |
| 525 |
" }\n" + |
| 526 |
" }\n" + |
| 527 |
" public static void main(String[] args) throws IOException {\n" + |
| 528 |
" new X().foo(true, true);\n" + |
| 529 |
" }\n" + |
| 530 |
"}\n" |
| 531 |
}, |
| 532 |
"----------\n" + |
| 533 |
"1. ERROR in X.java (at line 8)\n" + |
| 534 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
| 535 |
" ^^^^^^^^^^\n" + |
| 536 |
"Resource leak: 'fileReader' is never closed\n" + |
| 537 |
"----------\n" + |
| 538 |
"2. WARNING in X.java (at line 16)\n" + |
| 539 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
| 540 |
" ^^^^^^^^^^\n" + |
| 541 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
| 542 |
"----------\n", |
| 543 |
null, |
| 544 |
true, |
| 545 |
options); |
| 546 |
} |
| 547 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 548 |
// three AutoCloseables in different blocks of the same method - problems ignored |
| 549 |
public void test056i_ignore() { |
| 550 |
Map options = getCompilerOptions(); |
| 551 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.IGNORE); |
| 552 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.IGNORE); |
| 553 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 554 |
this.runConformTest( |
| 555 |
new String[] { |
| 556 |
"X.java", |
| 557 |
"import java.io.File;\n" + |
| 558 |
"import java.io.FileReader;\n" + |
| 559 |
"import java.io.IOException;\n" + |
| 560 |
"public class X {\n" + |
| 561 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
| 562 |
" File file = new File(\"somefile\");\n" + |
| 563 |
" if (f1) {\n" + |
| 564 |
" FileReader fileReader = new FileReader(file); // err: not closed\n" + |
| 565 |
" char[] in = new char[50];\n" + |
| 566 |
" fileReader.read(in);\n" + |
| 567 |
" while (true) {\n" + |
| 568 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
| 569 |
" loopReader.close();" + |
| 570 |
" break;\n" + |
| 571 |
" }\n" + |
| 572 |
" } else {\n" + |
| 573 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
| 574 |
" if (f2)\n" + |
| 575 |
" fileReader.close();\n" + |
| 576 |
" }\n" + |
| 577 |
" }\n" + |
| 578 |
"}\n" |
| 579 |
}, |
| 580 |
"", |
| 581 |
null, |
| 582 |
true, |
| 583 |
null, |
| 584 |
options, |
| 585 |
null); |
| 586 |
} |
| 587 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 588 |
// three AutoCloseables in different blocks of the same method |
| 589 |
public void test056i2() { |
| 590 |
Map options = getCompilerOptions(); |
| 591 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 592 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 593 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 594 |
this.runNegativeTest( |
| 595 |
new String[] { |
| 596 |
"X.java", |
| 597 |
"import java.io.File;\n" + |
| 598 |
"import java.io.FileReader;\n" + |
| 599 |
"import java.io.IOException;\n" + |
| 600 |
"public class X {\n" + |
| 601 |
" void foo(boolean f1, boolean f2) throws IOException {\n" + |
| 602 |
" File file = new File(\"somefile\");\n" + |
| 603 |
" if (f1) {\n" + |
| 604 |
" FileReader fileReader = new FileReader(file); // properly closed\n" + |
| 605 |
" char[] in = new char[50];\n" + |
| 606 |
" fileReader.read(in);\n" + |
| 607 |
" while (true) {\n" + |
| 608 |
" fileReader.close();\n" + |
| 609 |
" FileReader loopReader = new FileReader(file); // don't warn, properly closed\n" + |
| 610 |
" loopReader.close();\n" + |
| 611 |
" break;\n" + |
| 612 |
" }\n" + |
| 613 |
" } else {\n" + |
| 614 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
| 615 |
" if (f2)\n" + |
| 616 |
" fileReader.close();\n" + |
| 617 |
" }\n" + |
| 618 |
" }\n" + |
| 619 |
" public static void main(String[] args) throws IOException {\n" + |
| 620 |
" new X().foo(true, true);\n" + |
| 621 |
" }\n" + |
| 622 |
"}\n" |
| 623 |
}, |
| 624 |
"----------\n" + |
| 625 |
"1. ERROR in X.java (at line 18)\n" + |
| 626 |
" FileReader fileReader = new FileReader(file); // warn: not closed on all paths\n" + |
| 627 |
" ^^^^^^^^^^\n" + |
| 628 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
| 629 |
"----------\n", |
| 630 |
null, |
| 631 |
true, |
| 632 |
options); |
| 633 |
} |
| 634 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 635 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
| 636 |
public void test056j() { |
| 637 |
Map options = getCompilerOptions(); |
| 638 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 639 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 640 |
this.runNegativeTest( |
| 641 |
new String[] { |
| 642 |
"X.java", |
| 643 |
"import java.io.File;\n" + |
| 644 |
"import java.io.FileReader;\n" + |
| 645 |
"import java.io.IOException;\n" + |
| 646 |
"public class X {\n" + |
| 647 |
" void foo() throws IOException {\n" + |
| 648 |
" File file = new File(\"somefile\");\n" + |
| 649 |
" FileReader fileReader = new FileReader(file);\n" + |
| 650 |
" read(fileReader);\n" + |
| 651 |
" }\n" + |
| 652 |
" void read(FileReader reader) { }\n" + |
| 653 |
" public static void main(String[] args) throws IOException {\n" + |
| 654 |
" new X().foo();\n" + |
| 655 |
" }\n" + |
| 656 |
"}\n" |
| 657 |
}, |
| 658 |
"----------\n" + |
| 659 |
"1. ERROR in X.java (at line 7)\n" + |
| 660 |
" FileReader fileReader = new FileReader(file);\n" + |
| 661 |
" ^^^^^^^^^^\n" + |
| 662 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
| 663 |
"----------\n", |
| 664 |
null, |
| 665 |
true, |
| 666 |
options); |
| 667 |
} |
| 668 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 669 |
// a method uses an AutoCloseable without closing it locally but passing as arg to another method |
| 670 |
public void test056jconditional() { |
| 671 |
Map options = getCompilerOptions(); |
| 672 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 673 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 674 |
this.runNegativeTest( |
| 675 |
new String[] { |
| 676 |
"X.java", |
| 677 |
"import java.io.File;\n" + |
| 678 |
"import java.io.FileReader;\n" + |
| 679 |
"import java.io.IOException;\n" + |
| 680 |
"public class X {\n" + |
| 681 |
" void foo(boolean b) throws IOException {\n" + |
| 682 |
" File file = new File(\"somefile\");\n" + |
| 683 |
" FileReader fileReader = new FileReader(file);\n" + |
| 684 |
" synchronized (b ? this : new X()) {\n" + |
| 685 |
" new ReadDelegator(fileReader);\n" + |
| 686 |
" }\n" + |
| 687 |
" }\n" + |
| 688 |
" class ReadDelegator { ReadDelegator(FileReader reader) { } }\n" + |
| 689 |
" public static void main(String[] args) throws IOException {\n" + |
| 690 |
" new X().foo(true);\n" + |
| 691 |
" }\n" + |
| 692 |
"}\n" |
| 693 |
}, |
| 694 |
"----------\n" + |
| 695 |
"1. ERROR in X.java (at line 7)\n" + |
| 696 |
" FileReader fileReader = new FileReader(file);\n" + |
| 697 |
" ^^^^^^^^^^\n" + |
| 698 |
"Potential resource leak: 'fileReader' may not be closed\n" + |
| 699 |
"----------\n", |
| 700 |
null, |
| 701 |
true, |
| 702 |
options); |
| 703 |
} |
| 704 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 705 |
// many locals, some are AutoCloseable. |
| 706 |
// Unfortunately analysis cannot respect how exception exits may affect ra3 and rb3, |
| 707 |
// doing so would create false positives. |
| 708 |
public void test056k() { |
| 709 |
Map options = getCompilerOptions(); |
| 710 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 711 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
| 712 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
| 713 |
String expectedProblems = this.complianceLevel < ClassFileConstants.JDK1_7 ? |
| 714 |
"----------\n" + |
| 715 |
"1. ERROR in X.java (at line 15)\n" + |
| 716 |
" ra2 = new FileReader(file);\n" + |
| 717 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 718 |
"Resource leak: \'ra2\' is never closed\n" + |
| 719 |
"----------\n" + |
| 720 |
"2. ERROR in X.java (at line 28)\n" + |
| 721 |
" rb2 = new FileReader(file);\n" + |
| 722 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 723 |
"Resource leak: \'rb2\' is never closed\n" + |
| 724 |
"----------\n" |
| 725 |
: |
| 726 |
"----------\n" + |
| 727 |
"1. ERROR in X.java (at line 12)\n" + |
| 728 |
" FileReader ra1 = null, ra2 = null;\n" + |
| 729 |
" ^^^\n" + |
| 730 |
"Resource 'ra1' should be managed by try-with-resource\n" + |
| 731 |
"----------\n" + |
| 732 |
"2. ERROR in X.java (at line 15)\n" + |
| 733 |
" ra2 = new FileReader(file);\n" + |
| 734 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 735 |
"Resource leak: 'ra2' is never closed\n" + |
| 736 |
"----------\n" + |
| 737 |
"3. ERROR in X.java (at line 16)\n" + |
| 738 |
" FileReader ra3 = new FileReader(file);\n" + |
| 739 |
" ^^^\n" + |
| 740 |
"Resource 'ra3' should be managed by try-with-resource\n" + |
| 741 |
"----------\n" + |
| 742 |
"4. ERROR in X.java (at line 25)\n" + |
| 743 |
" FileReader rb1 = null, rb2 = null;\n" + |
| 744 |
" ^^^\n" + |
| 745 |
"Resource 'rb1' should be managed by try-with-resource\n" + |
| 746 |
"----------\n" + |
| 747 |
"5. ERROR in X.java (at line 28)\n" + |
| 748 |
" rb2 = new FileReader(file);\n" + |
| 749 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 750 |
"Resource leak: 'rb2' is never closed\n" + |
| 751 |
"----------\n" + |
| 752 |
"6. ERROR in X.java (at line 29)\n" + |
| 753 |
" FileReader rb3 = new FileReader(file);\n" + |
| 754 |
" ^^^\n" + |
| 755 |
"Resource 'rb3' should be managed by try-with-resource\n" + |
| 756 |
"----------\n"; |
| 757 |
this.runNegativeTest( |
| 758 |
new String[] { |
| 759 |
"X.java", |
| 760 |
"import java.io.File;\n" + |
| 761 |
"import java.io.FileReader;\n" + |
| 762 |
"import java.io.IOException;\n" + |
| 763 |
"public class X {\n" + |
| 764 |
" void foo() throws IOException {\n" + |
| 765 |
" int i01, i02, i03, i04, i05, i06, i07, i08, i09,\n" + |
| 766 |
" i11, i12, i13, i14, i15, i16, i17, i18, i19,\n" + |
| 767 |
" i21, i22, i23, i24, i25, i26, i27, i28, i29,\n" + |
| 768 |
" i31, i32, i33, i34, i35, i36, i37, i38, i39,\n" + |
| 769 |
" i41, i42, i43, i44, i45, i46, i47, i48, i49;\n" + |
| 770 |
" File file = new File(\"somefile\");\n" + |
| 771 |
" FileReader ra1 = null, ra2 = null;\n" + |
| 772 |
" try {\n" + |
| 773 |
" ra1 = new FileReader(file);\n" + |
| 774 |
" ra2 = new FileReader(file);\n" + |
| 775 |
" FileReader ra3 = new FileReader(file);\n" + |
| 776 |
" char[] in = new char[50];\n" + |
| 777 |
" ra1.read(in);\n" + |
| 778 |
" ra2.read(in);\n" + |
| 779 |
" ra3.close();\n" + |
| 780 |
" } finally {\n" + |
| 781 |
" ra1.close();\n" + |
| 782 |
" }\n" + |
| 783 |
" int i51, i52, i53, i54, i55, i56, i57, i58, i59, i60;\n" + // beyond this point locals are analyzed using extraBits |
| 784 |
" FileReader rb1 = null, rb2 = null;\n" + |
| 785 |
" try {\n" + |
| 786 |
" rb1 = new FileReader(file);\n" + |
| 787 |
" rb2 = new FileReader(file);\n" + |
| 788 |
" FileReader rb3 = new FileReader(file);\n" + |
| 789 |
" char[] in = new char[50];\n" + |
| 790 |
" rb1.read(in);\n" + |
| 791 |
" rb2.read(in);\n" + |
| 792 |
" rb3.close();\n" + |
| 793 |
" } finally {\n" + |
| 794 |
" rb1.close();\n" + |
| 795 |
" }\n" + |
| 796 |
" }\n" + |
| 797 |
" public static void main(String[] args) throws IOException {\n" + |
| 798 |
" new X().foo();\n" + |
| 799 |
" }\n" + |
| 800 |
"}\n" |
| 801 |
}, |
| 802 |
expectedProblems, |
| 803 |
null, |
| 804 |
true, |
| 805 |
options); |
| 806 |
} |
| 807 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 808 |
// various non-problems |
| 809 |
public void test056l() { |
| 810 |
Map options = getCompilerOptions(); |
| 811 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 812 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 813 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
| 814 |
String expectedProblems = this.complianceLevel >= ClassFileConstants.JDK1_7 ? |
| 815 |
"----------\n" + |
| 816 |
"1. ERROR in X.java (at line 8)\n" + |
| 817 |
" FileReader fileReader = getReader();\n" + |
| 818 |
" ^^^^^^^^^^\n" + |
| 819 |
"Resource 'fileReader' should be managed by try-with-resource\n" + |
| 820 |
"----------\n" + |
| 821 |
"2. ERROR in X.java (at line 11)\n" + |
| 822 |
" FileReader r3 = getReader();\n" + |
| 823 |
" ^^\n" + |
| 824 |
"Resource 'r3' should be managed by try-with-resource\n" + |
| 825 |
"----------\n" + |
| 826 |
"3. ERROR in X.java (at line 24)\n" + |
| 827 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
| 828 |
" ^^\n" + |
| 829 |
"Potential resource leak: 'r2' may not be closed\n" + |
| 830 |
"----------\n" + |
| 831 |
"4. ERROR in X.java (at line 25)\n" + |
| 832 |
" new X(r2).foo(new FileReader(new File(\"notthere\"))); // potential problem: foo may/may not close the new FileReader\n" + |
| 833 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 834 |
"Potential resource leak: \'<unassigned Closeable value>\' may not be closed\n" + |
| 835 |
"----------\n" |
| 836 |
: |
| 837 |
"----------\n" + |
| 838 |
"1. ERROR in X.java (at line 24)\n" + |
| 839 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
| 840 |
" ^^\n" + |
| 841 |
"Potential resource leak: 'r2' may not be closed\n" + |
| 842 |
"----------\n" + |
| 843 |
"2. ERROR in X.java (at line 25)\n" + |
| 844 |
" new X(r2).foo(new FileReader(new File(\"notthere\"))); // potential problem: foo may/may not close the new FileReader\n" + |
| 845 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 846 |
"Potential resource leak: \'<unassigned Closeable value>\' may not be closed\n" + |
| 847 |
"----------\n"; |
| 848 |
this.runNegativeTest( |
| 849 |
new String[] { |
| 850 |
"X.java", |
| 851 |
"import java.io.File;\n" + |
| 852 |
"import java.io.FileReader;\n" + |
| 853 |
"import java.io.IOException;\n" + |
| 854 |
"public class X {\n" + |
| 855 |
" X(FileReader r0) {}\n" + // don't complain against argument |
| 856 |
" FileReader getReader() { return null; }\n" + |
| 857 |
" void foo(FileReader r1) throws IOException {\n" + |
| 858 |
" FileReader fileReader = getReader();\n" + |
| 859 |
" if (fileReader == null)\n" + |
| 860 |
" return;\n" + // don't complain, resource is actually null |
| 861 |
" FileReader r3 = getReader();\n" + |
| 862 |
" if (r3 == null)\n" + |
| 863 |
" r3 = new FileReader(new File(\"absent\"));\n" + // don't complain, previous resource is actually null |
| 864 |
" try {\n" + |
| 865 |
" char[] in = new char[50];\n" + |
| 866 |
" fileReader.read(in);\n" + |
| 867 |
" r1.read(in);\n" + |
| 868 |
" } finally {\n" + |
| 869 |
" fileReader.close();\n" + |
| 870 |
" r3.close();\n" + // the effect of this close() call might be spoiled by exception in fileReader.close() above, but we ignore exception exits in the analysis |
| 871 |
" }\n" + |
| 872 |
" }\n" + |
| 873 |
" public static void main(String[] args) throws IOException {\n" + |
| 874 |
" FileReader r2 = new FileReader(new File(\"inexist\")); // only potential problem: ctor X below might close r2\n" + |
| 875 |
" new X(r2).foo(new FileReader(new File(\"notthere\"))); // potential problem: foo may/may not close the new FileReader\n" + |
| 876 |
" }\n" + |
| 877 |
"}\n" |
| 878 |
}, |
| 879 |
expectedProblems, |
| 880 |
null, |
| 881 |
true, |
| 882 |
options); |
| 883 |
} |
| 884 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 885 |
// nested try with early exit |
| 886 |
public void test056m() { |
| 887 |
Map options = getCompilerOptions(); |
| 888 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 889 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 890 |
this.runConformTest( |
| 891 |
new String[] { |
| 892 |
"X.java", |
| 893 |
"import java.io.File;\n" + |
| 894 |
"import java.io.FileReader;\n" + |
| 895 |
"import java.io.IOException;\n" + |
| 896 |
"public class X {\n" + |
| 897 |
" void foo() {\n" + |
| 898 |
" File file = new File(\"somefile\");" + |
| 899 |
" try {\n" + |
| 900 |
" FileReader fileReader = new FileReader(file);\n" + |
| 901 |
" try {\n" + |
| 902 |
" char[] in = new char[50];\n" + |
| 903 |
" if (fileReader.read(in)==0)\n" + |
| 904 |
" return;\n" + |
| 905 |
" } finally {\n" + |
| 906 |
" fileReader.close();\n" + |
| 907 |
" }\n" + |
| 908 |
" } catch (IOException e) {\n" + |
| 909 |
" System.out.println(\"caught\");\n" + |
| 910 |
" }\n" + |
| 911 |
" }\n" + |
| 912 |
" public static void main(String[] args) {\n" + |
| 913 |
" new X().foo();\n" + |
| 914 |
" }\n" + |
| 915 |
"}\n" |
| 916 |
}, |
| 917 |
"caught", /*output*/ |
| 918 |
null/*classLibs*/, |
| 919 |
true/*shouldFlush*/, |
| 920 |
null/*vmargs*/, |
| 921 |
options, |
| 922 |
null/*requestor*/); |
| 923 |
} |
| 924 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 925 |
// nested try should not interfere with earlier analysis. |
| 926 |
public void test056n() { |
| 927 |
Map options = getCompilerOptions(); |
| 928 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 929 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 930 |
this.runConformTest( |
| 931 |
new String[] { |
| 932 |
"X.java", |
| 933 |
"import java.io.File;\n" + |
| 934 |
"import java.io.FileReader;\n" + |
| 935 |
"import java.io.IOException;\n" + |
| 936 |
"import java.io.FileNotFoundException;\n" + |
| 937 |
"public class X {\n" + |
| 938 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
| 939 |
" FileReader fr1 = new FileReader(someFile);\n" + |
| 940 |
" try {\n" + |
| 941 |
" fr1.read(buf);\n" + |
| 942 |
" } finally {\n" + |
| 943 |
" fr1.close();\n" + |
| 944 |
" }\n" + |
| 945 |
" try {\n" + |
| 946 |
" FileReader fr3 = new FileReader(someFile);\n" + |
| 947 |
" try {\n" + |
| 948 |
" } finally {\n" + |
| 949 |
" fr3.close();\n" + |
| 950 |
" }\n" + |
| 951 |
" } catch (IOException e) {\n" + |
| 952 |
" }\n" + |
| 953 |
" }\n" + |
| 954 |
" public static void main(String[] args) throws IOException {\n" + |
| 955 |
" try {\n" + |
| 956 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
| 957 |
" } catch (FileNotFoundException e) {\n" + |
| 958 |
" System.out.println(\"caught\");\n" + |
| 959 |
" }\n" + |
| 960 |
" }\n" + |
| 961 |
"}\n" |
| 962 |
}, |
| 963 |
"caught", /*output*/ |
| 964 |
null/*classLibs*/, |
| 965 |
true/*shouldFlush*/, |
| 966 |
null/*vmargs*/, |
| 967 |
options, |
| 968 |
null/*requestor*/); |
| 969 |
} |
| 970 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 971 |
// if close is guarded by null check this should still be recognized as definitely closed |
| 972 |
public void test056o() { |
| 973 |
Map options = getCompilerOptions(); |
| 974 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 975 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 976 |
this.runConformTest( |
| 977 |
new String[] { |
| 978 |
"X.java", |
| 979 |
"import java.io.File;\n" + |
| 980 |
"import java.io.FileReader;\n" + |
| 981 |
"import java.io.IOException;\n" + |
| 982 |
"import java.io.FileNotFoundException;\n" + |
| 983 |
"public class X {\n" + |
| 984 |
" void foo(File someFile, char[] buf) throws IOException {\n" + |
| 985 |
" FileReader fr1 = null;\n" + |
| 986 |
" try {\n" + |
| 987 |
" fr1 = new FileReader(someFile);" + |
| 988 |
" fr1.read(buf);\n" + |
| 989 |
" } finally {\n" + |
| 990 |
" if (fr1 != null)\n" + |
| 991 |
" try {\n" + |
| 992 |
" fr1.close();\n" + |
| 993 |
" } catch (IOException e) { /*do nothing*/ }\n" + |
| 994 |
" }\n" + |
| 995 |
" }\n" + |
| 996 |
" public static void main(String[] args) throws IOException {\n" + |
| 997 |
" try {\n" + |
| 998 |
" new X().foo(new File(\"missing\"), new char[100]);\n" + |
| 999 |
" } catch (FileNotFoundException e) {\n" + |
| 1000 |
" System.out.println(\"caught\");\n" + |
| 1001 |
" }\n" + |
| 1002 |
" }\n" + |
| 1003 |
"}\n" |
| 1004 |
}, |
| 1005 |
"caught", /*output*/ |
| 1006 |
null/*classLibs*/, |
| 1007 |
true/*shouldFlush*/, |
| 1008 |
null/*vmargs*/, |
| 1009 |
options, |
| 1010 |
null/*requestor*/); |
| 1011 |
} |
| 1012 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1013 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
| 1014 |
// a method uses an AutoCloseable without ever closing it, type from a type variable |
| 1015 |
public void test056p() { |
| 1016 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // generics used |
| 1017 |
Map options = getCompilerOptions(); |
| 1018 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 1019 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 1020 |
this.runNegativeTest( |
| 1021 |
new String[] { |
| 1022 |
"X.java", |
| 1023 |
"import java.io.File;\n" + |
| 1024 |
"import java.io.FileReader;\n" + |
| 1025 |
"import java.io.Reader;\n" + |
| 1026 |
"import java.io.IOException;\n" + |
| 1027 |
"public abstract class X <T extends Reader> {\n" + |
| 1028 |
" void foo() throws IOException {\n" + |
| 1029 |
" File file = new File(\"somefile\");\n" + |
| 1030 |
" T fileReader = newReader(file);\n" + |
| 1031 |
" char[] in = new char[50];\n" + |
| 1032 |
" fileReader.read(in);\n" + |
| 1033 |
" }\n" + |
| 1034 |
" abstract T newReader(File file) throws IOException;\n" + |
| 1035 |
" public static void main(String[] args) throws IOException {\n" + |
| 1036 |
" new X<FileReader>() {\n" + |
| 1037 |
" FileReader newReader(File f) throws IOException { return new FileReader(f); }\n" + |
| 1038 |
" }.foo();\n" + |
| 1039 |
" }\n" + |
| 1040 |
"}\n" |
| 1041 |
}, |
| 1042 |
"----------\n" + |
| 1043 |
"1. ERROR in X.java (at line 8)\n" + |
| 1044 |
" T fileReader = newReader(file);\n" + |
| 1045 |
" ^^^^^^^^^^\n" + |
| 1046 |
"Potential resource leak: \'fileReader\' may not be closed\n" + |
| 1047 |
"----------\n", |
| 1048 |
null, |
| 1049 |
true, |
| 1050 |
options); |
| 1051 |
} |
| 1052 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1053 |
// closed in dead code |
| 1054 |
public void test056q() { |
| 1055 |
Map options = getCompilerOptions(); |
| 1056 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1057 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1058 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 1059 |
this.runNegativeTest( |
| 1060 |
new String[] { |
| 1061 |
"X.java", |
| 1062 |
"import java.io.File;\n" + |
| 1063 |
"import java.io.FileReader;\n" + |
| 1064 |
"import java.io.IOException;\n" + |
| 1065 |
"public class X {\n" + |
| 1066 |
" void foo() throws IOException {\n" + |
| 1067 |
" File file = new File(\"somefile\");\n" + |
| 1068 |
" FileReader fileReader = new FileReader(file);\n" + |
| 1069 |
" char[] in = new char[50];\n" + |
| 1070 |
" fileReader.read(in);\n" + |
| 1071 |
" if (2*2 == 4)\n" + |
| 1072 |
" return;\n" + |
| 1073 |
" fileReader.close();\n" + |
| 1074 |
" }\n" + |
| 1075 |
" public static void main(String[] args) throws IOException {\n" + |
| 1076 |
" new X().foo();\n" + |
| 1077 |
" }\n" + |
| 1078 |
"}\n" |
| 1079 |
}, |
| 1080 |
"----------\n" + |
| 1081 |
"1. ERROR in X.java (at line 7)\n" + |
| 1082 |
" FileReader fileReader = new FileReader(file);\n" + |
| 1083 |
" ^^^^^^^^^^\n" + |
| 1084 |
"Resource leak: 'fileReader' is never closed\n" + |
| 1085 |
"----------\n" + |
| 1086 |
"2. WARNING in X.java (at line 10)\n" + |
| 1087 |
" if (2*2 == 4)\n" + |
| 1088 |
" ^^^^^^^^\n" + |
| 1089 |
"Comparing identical expressions\n" + |
| 1090 |
"----------\n" + |
| 1091 |
"3. WARNING in X.java (at line 12)\n" + |
| 1092 |
" fileReader.close();\n" + |
| 1093 |
" ^^^^^^^^^^^^^^^^^^\n" + |
| 1094 |
"Dead code\n" + |
| 1095 |
"----------\n", |
| 1096 |
null, |
| 1097 |
true, |
| 1098 |
options); |
| 1099 |
} |
| 1100 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1101 |
// properly closed, dead code in between |
| 1102 |
public void test056r() { |
| 1103 |
Map options = getCompilerOptions(); |
| 1104 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1105 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1106 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 1107 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1108 |
this.runNegativeTest( |
| 1109 |
new String[] { |
| 1110 |
"X.java", |
| 1111 |
"import java.io.File;\n" + |
| 1112 |
"import java.io.FileReader;\n" + |
| 1113 |
"import java.io.IOException;\n" + |
| 1114 |
"public class X {\n" + |
| 1115 |
" void foo() throws IOException {\n" + |
| 1116 |
" File file = new File(\"somefile\");\n" + |
| 1117 |
" FileReader fr = new FileReader(file);\n" + |
| 1118 |
" Object b = null;\n" + |
| 1119 |
" fr.close();\n" + |
| 1120 |
" if (b != null) {\n" + |
| 1121 |
" fr = new FileReader(file);\n" + |
| 1122 |
" return;\n" + |
| 1123 |
" } else {\n" + |
| 1124 |
" System.out.print(42);\n" + |
| 1125 |
" }\n" + |
| 1126 |
" return; // Should not complain about fr\n" + |
| 1127 |
" }\n" + |
| 1128 |
" public static void main(String[] args) throws IOException {\n" + |
| 1129 |
" new X().foo();\n" + |
| 1130 |
" }\n" + |
| 1131 |
"}\n" |
| 1132 |
}, |
| 1133 |
"----------\n" + |
| 1134 |
"1. ERROR in X.java (at line 10)\n" + |
| 1135 |
" if (b != null) {\n" + |
| 1136 |
" fr = new FileReader(file);\n" + |
| 1137 |
" return;\n" + |
| 1138 |
" } else {\n" + |
| 1139 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1140 |
"Dead code\n" + |
| 1141 |
"----------\n" + |
| 1142 |
"2. WARNING in X.java (at line 13)\n" + |
| 1143 |
" } else {\n" + |
| 1144 |
" System.out.print(42);\n" + |
| 1145 |
" }\n" + |
| 1146 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1147 |
"Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally\n" + |
| 1148 |
"----------\n", |
| 1149 |
null, |
| 1150 |
true, |
| 1151 |
options); |
| 1152 |
} |
| 1153 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1154 |
// resource inside t-w-r is re-assigned, shouldn't even record an errorLocation |
| 1155 |
public void test056s() { |
| 1156 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; // t-w-r used |
| 1157 |
Map options = getCompilerOptions(); |
| 1158 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1159 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1160 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.IGNORE); |
| 1161 |
this.runNegativeTest( |
| 1162 |
new String[] { |
| 1163 |
"X.java", |
| 1164 |
"import java.io.File;\n" + |
| 1165 |
"import java.io.FileReader;\n" + |
| 1166 |
"import java.io.IOException;\n" + |
| 1167 |
"public class X {\n" + |
| 1168 |
" void foo() throws IOException {\n" + |
| 1169 |
" File file = new File(\"somefile\");\n" + |
| 1170 |
" try (FileReader fileReader = new FileReader(file);) {\n" + |
| 1171 |
" char[] in = new char[50];\n" + |
| 1172 |
" fileReader.read(in);\n" + |
| 1173 |
" fileReader = new FileReader(file); // debug here\n" + |
| 1174 |
" fileReader.read(in);\n" + |
| 1175 |
" }\n" + |
| 1176 |
" }\n" + |
| 1177 |
" public static void main(String[] args) throws IOException {\n" + |
| 1178 |
" new X().foo();\n" + |
| 1179 |
" }\n" + |
| 1180 |
"}\n" |
| 1181 |
}, |
| 1182 |
"----------\n" + |
| 1183 |
"1. ERROR in X.java (at line 10)\n" + |
| 1184 |
" fileReader = new FileReader(file); // debug here\n" + |
| 1185 |
" ^^^^^^^^^^\n" + |
| 1186 |
"The resource fileReader of a try-with-resources statement cannot be assigned\n" + |
| 1187 |
"----------\n", |
| 1188 |
null, |
| 1189 |
true, |
| 1190 |
options); |
| 1191 |
} |
| 1192 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1193 |
// resource is closed, dead code follows |
| 1194 |
public void test056t() { |
| 1195 |
Map options = getCompilerOptions(); |
| 1196 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1197 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1198 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1199 |
this.runNegativeTest( |
| 1200 |
new String[] { |
| 1201 |
"X.java", |
| 1202 |
"import java.io.FileReader;\n" + |
| 1203 |
"import java.io.IOException;\n" + |
| 1204 |
"public class X {\n" + |
| 1205 |
" void foo31() throws IOException {\n" + |
| 1206 |
" FileReader reader = new FileReader(\"file\"); //warning\n" + |
| 1207 |
" if (reader != null) {\n" + |
| 1208 |
" reader.close();\n" + |
| 1209 |
" } else {\n" + |
| 1210 |
" // nop\n" + |
| 1211 |
" }\n" + |
| 1212 |
" }\n" + |
| 1213 |
" public static void main(String[] args) throws IOException {\n" + |
| 1214 |
" new X().foo31();\n" + |
| 1215 |
" }\n" + |
| 1216 |
"}\n" |
| 1217 |
}, |
| 1218 |
"----------\n" + |
| 1219 |
"1. ERROR in X.java (at line 8)\n" + |
| 1220 |
" } else {\n" + |
| 1221 |
" // nop\n" + |
| 1222 |
" }\n" + |
| 1223 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1224 |
"Dead code\n" + |
| 1225 |
"----------\n", |
| 1226 |
null, |
| 1227 |
true, |
| 1228 |
options); |
| 1229 |
} |
| 1230 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1231 |
// resource is reassigned within t-w-r with different resource |
| 1232 |
// was initially broken due to https://bugs.eclipse.org/358827 |
| 1233 |
public void test056u() { |
| 1234 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; // t-w-r used |
| 1235 |
Map options = getCompilerOptions(); |
| 1236 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1237 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1238 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1239 |
this.runNegativeTest( |
| 1240 |
new String[] { |
| 1241 |
"X.java", |
| 1242 |
"import java.io.FileReader;\n" + |
| 1243 |
"public class X {\n" + |
| 1244 |
" void foo() throws Exception {\n" + |
| 1245 |
" FileReader reader1 = new FileReader(\"file1\");\n" + |
| 1246 |
" FileReader reader2 = new FileReader(\"file2\");\n" + |
| 1247 |
" reader2 = reader1;// this disconnects reader 2\n" + |
| 1248 |
" try (FileReader reader3 = new FileReader(\"file3\")) {\n" + |
| 1249 |
" int ch;\n" + |
| 1250 |
" while ((ch = reader2.read()) != -1) {\n" + |
| 1251 |
" System.out.println(ch);\n" + |
| 1252 |
" reader1.read();\n" + |
| 1253 |
" }\n" + |
| 1254 |
" reader2 = reader1; // warning 1 regarding original reader1\n" + // this warning was missing |
| 1255 |
" reader2 = reader1; // warning 2 regarding original reader1\n" + |
| 1256 |
" } finally {\n" + |
| 1257 |
" if (reader2 != null) {\n" + |
| 1258 |
" reader2.close();\n" + |
| 1259 |
" } else {\n" + |
| 1260 |
" System.out.println();\n" + |
| 1261 |
" }\n" + |
| 1262 |
" }\n" + |
| 1263 |
" }\n" + |
| 1264 |
"}\n" |
| 1265 |
}, |
| 1266 |
"----------\n" + |
| 1267 |
"1. ERROR in X.java (at line 5)\n" + |
| 1268 |
" FileReader reader2 = new FileReader(\"file2\");\n" + |
| 1269 |
" ^^^^^^^\n" + |
| 1270 |
"Resource leak: 'reader2' is never closed\n" + |
| 1271 |
"----------\n" + |
| 1272 |
"2. ERROR in X.java (at line 13)\n" + |
| 1273 |
" reader2 = reader1; // warning 1 regarding original reader1\n" + |
| 1274 |
" ^^^^^^^^^^^^^^^^^\n" + |
| 1275 |
"Resource leak: 'reader1' is not closed at this location\n" + |
| 1276 |
"----------\n" + |
| 1277 |
"3. ERROR in X.java (at line 14)\n" + |
| 1278 |
" reader2 = reader1; // warning 2 regarding original reader1\n" + |
| 1279 |
" ^^^^^^^^^^^^^^^^^\n" + |
| 1280 |
"Resource leak: 'reader1' is not closed at this location\n" + |
| 1281 |
"----------\n", |
| 1282 |
null, |
| 1283 |
true, |
| 1284 |
options); |
| 1285 |
} |
| 1286 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1287 |
// scope-related pbs reported in https://bugs.eclipse.org/349326#c70 and https://bugs.eclipse.org/349326#c82 |
| 1288 |
public void test056v() { |
| 1289 |
Map options = getCompilerOptions(); |
| 1290 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1291 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1292 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.WARNING); |
| 1293 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1294 |
String expectedProblems = this.complianceLevel >= ClassFileConstants.JDK1_7 ? |
| 1295 |
"----------\n" + |
| 1296 |
"1. ERROR in X.java (at line 4)\n" + |
| 1297 |
" FileReader reader = new FileReader(\"file\");\n" + |
| 1298 |
" ^^^^^^\n" + |
| 1299 |
"Resource leak: 'reader' is never closed\n" + |
| 1300 |
"----------\n" + |
| 1301 |
"2. WARNING in X.java (at line 19)\n" + |
| 1302 |
" FileReader reader111 = new FileReader(\"file2\");\n" + |
| 1303 |
" ^^^^^^^^^\n" + |
| 1304 |
"Resource 'reader111' should be managed by try-with-resource\n" + |
| 1305 |
"----------\n" + |
| 1306 |
"3. ERROR in X.java (at line 42)\n" + |
| 1307 |
" return;\n" + |
| 1308 |
" ^^^^^^^\n" + |
| 1309 |
"Resource leak: 'reader2' is not closed at this location\n" + |
| 1310 |
"----------\n" |
| 1311 |
: |
| 1312 |
"----------\n" + |
| 1313 |
"1. ERROR in X.java (at line 4)\n" + |
| 1314 |
" FileReader reader = new FileReader(\"file\");\n" + |
| 1315 |
" ^^^^^^\n" + |
| 1316 |
"Resource leak: 'reader' is never closed\n" + |
| 1317 |
"----------\n" + |
| 1318 |
"2. ERROR in X.java (at line 42)\n" + |
| 1319 |
" return;\n" + |
| 1320 |
" ^^^^^^^\n" + |
| 1321 |
"Resource leak: 'reader2' is not closed at this location\n" + |
| 1322 |
"----------\n"; |
| 1323 |
this.runNegativeTest( |
| 1324 |
new String[] { |
| 1325 |
"X.java", |
| 1326 |
"import java.io.FileReader;\n" + |
| 1327 |
"public class X {\n" + |
| 1328 |
" boolean foo1() throws Exception {\n" + |
| 1329 |
" FileReader reader = new FileReader(\"file\");\n" + |
| 1330 |
" try {\n" + |
| 1331 |
" int ch;\n" + |
| 1332 |
" while ((ch = reader.read()) != -1) {\n" + |
| 1333 |
" System.out.println(ch);\n" + |
| 1334 |
" reader.read();\n" + |
| 1335 |
" }\n" + |
| 1336 |
" if (ch > 10) {\n" + |
| 1337 |
" return true;\n" + |
| 1338 |
" }\n" + |
| 1339 |
" return false;\n" + // return while resource from enclosing scope remains unclosed |
| 1340 |
" } finally {\n" + |
| 1341 |
" }\n" + |
| 1342 |
" }\n" + |
| 1343 |
" void foo111() throws Exception {\n" + |
| 1344 |
" FileReader reader111 = new FileReader(\"file2\");\n" + |
| 1345 |
" try {\n" + |
| 1346 |
" int ch;\n" + |
| 1347 |
" while ((ch = reader111.read()) != -1) {\n" + |
| 1348 |
" System.out.println(ch);\n" + |
| 1349 |
" reader111.read();\n" + |
| 1350 |
" }\n" + |
| 1351 |
" return;\n" + // this shouldn't spoil the warning "should be managed with t-w-r" |
| 1352 |
" } finally {\n" + |
| 1353 |
" if (reader111 != null) {\n" + |
| 1354 |
" reader111.close();\n" + |
| 1355 |
" }\n" + |
| 1356 |
" }\n" + |
| 1357 |
" }\n" + |
| 1358 |
" void foo2() throws Exception {\n" + |
| 1359 |
" FileReader reader2 = new FileReader(\"file\");\n" + |
| 1360 |
" try {\n" + |
| 1361 |
" int ch;\n" + |
| 1362 |
" while ((ch = reader2.read()) != -1) {\n" + |
| 1363 |
" System.out.println(ch);\n" + |
| 1364 |
" reader2.read();\n" + |
| 1365 |
" }\n" + |
| 1366 |
" if (ch > 10) {\n" + |
| 1367 |
" return;\n" + // potential leak |
| 1368 |
" }\n" + |
| 1369 |
" } finally {\n" + |
| 1370 |
" }\n" + |
| 1371 |
" reader2.close();\n" + // due to this close we don't say "never closed" |
| 1372 |
" }\n" + |
| 1373 |
"}\n" |
| 1374 |
}, |
| 1375 |
expectedProblems, |
| 1376 |
null, |
| 1377 |
true, |
| 1378 |
options); |
| 1379 |
} |
| 1380 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1381 |
// end of method is dead end, but before we have both a close() and an early return |
| 1382 |
public void test056w() { |
| 1383 |
Map options = getCompilerOptions(); |
| 1384 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1385 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1386 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1387 |
this.runNegativeTest( |
| 1388 |
new String[] { |
| 1389 |
"X.java", |
| 1390 |
"import java.io.FileReader;\n" + |
| 1391 |
"public class X {\n" + |
| 1392 |
" boolean foo1() throws Exception {\n" + |
| 1393 |
" FileReader reader = new FileReader(\"file\");\n" + |
| 1394 |
" try {\n" + |
| 1395 |
" int ch;\n" + |
| 1396 |
" while ((ch = reader.read()) != -1) {\n" + |
| 1397 |
" System.out.println(ch);\n" + |
| 1398 |
" reader.read();\n" + |
| 1399 |
" }\n" + |
| 1400 |
" if (ch > 10) {\n" + |
| 1401 |
" reader.close();\n" + |
| 1402 |
" return true;\n" + |
| 1403 |
" }\n" + |
| 1404 |
" return false;\n" + |
| 1405 |
" } finally {\n" + |
| 1406 |
" }\n" + |
| 1407 |
" }\n" + |
| 1408 |
"}\n" |
| 1409 |
}, |
| 1410 |
"----------\n" + |
| 1411 |
"1. ERROR in X.java (at line 15)\n" + |
| 1412 |
" return false;\n" + |
| 1413 |
" ^^^^^^^^^^^^^\n" + |
| 1414 |
"Resource leak: 'reader' is not closed at this location\n" + |
| 1415 |
"----------\n", |
| 1416 |
null, |
| 1417 |
true, |
| 1418 |
options); |
| 1419 |
} |
| 1420 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1421 |
// different early exits, if no close seen report as definitely unclosed |
| 1422 |
public void test056x() { |
| 1423 |
Map options = getCompilerOptions(); |
| 1424 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1425 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1426 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1427 |
this.runNegativeTest( |
| 1428 |
new String[] { |
| 1429 |
"X.java", |
| 1430 |
"import java.io.FileReader;\n" + |
| 1431 |
"public class X {\n" + |
| 1432 |
" void foo31(boolean b) throws Exception {\n" + |
| 1433 |
" FileReader reader = new FileReader(\"file\");\n" + |
| 1434 |
" if (b) {\n" + |
| 1435 |
" reader.close();\n" + |
| 1436 |
" } else {\n" + |
| 1437 |
" return; // warning\n" + |
| 1438 |
" }\n" + |
| 1439 |
" }\n" + |
| 1440 |
" void foo32(boolean b) throws Exception {\n" + |
| 1441 |
" FileReader reader = new FileReader(\"file\"); // warn here\n" + |
| 1442 |
" return;\n" + |
| 1443 |
" }\n" + |
| 1444 |
"}\n" |
| 1445 |
}, |
| 1446 |
"----------\n" + |
| 1447 |
"1. ERROR in X.java (at line 8)\n" + |
| 1448 |
" return; // warning\n" + |
| 1449 |
" ^^^^^^^\n" + |
| 1450 |
"Resource leak: 'reader' is not closed at this location\n" + |
| 1451 |
"----------\n" + |
| 1452 |
"2. ERROR in X.java (at line 12)\n" + |
| 1453 |
" FileReader reader = new FileReader(\"file\"); // warn here\n" + |
| 1454 |
" ^^^^^^\n" + |
| 1455 |
"Resource leak: 'reader' is never closed\n" + |
| 1456 |
"----------\n", |
| 1457 |
null, |
| 1458 |
true, |
| 1459 |
options); |
| 1460 |
} |
| 1461 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1462 |
// nested method passes the resource to outside code |
| 1463 |
public void test056y() { |
| 1464 |
Map options = getCompilerOptions(); |
| 1465 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1466 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.WARNING); |
| 1467 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1468 |
this.runNegativeTest( |
| 1469 |
new String[] { |
| 1470 |
"X.java", |
| 1471 |
"import java.io.FileReader;\n" + |
| 1472 |
"public class X {\n" + |
| 1473 |
" void foo31(boolean b) throws Exception {\n" + |
| 1474 |
" final FileReader reader31 = new FileReader(\"file\");\n" + |
| 1475 |
" new Runnable() {\n" + |
| 1476 |
" public void run() {\n" + |
| 1477 |
" foo18(reader31);\n" + |
| 1478 |
" }\n" + |
| 1479 |
" }.run();\n" + |
| 1480 |
" }\n" + |
| 1481 |
" void foo18(FileReader r18) {\n" + |
| 1482 |
" // could theoretically close r18;\n" + |
| 1483 |
" }\n" + |
| 1484 |
" abstract class ResourceProvider {\n" + |
| 1485 |
" abstract FileReader provide();" + |
| 1486 |
" }\n" + |
| 1487 |
" ResourceProvider provider;" + |
| 1488 |
" void foo23() throws Exception {\n" + |
| 1489 |
" final FileReader reader23 = new FileReader(\"file\");\n" + |
| 1490 |
" provider = new ResourceProvider() {\n" + |
| 1491 |
" public FileReader provide() {\n" + |
| 1492 |
" return reader23;\n" + // responsibility now lies at the caller of this method |
| 1493 |
" }\n" + |
| 1494 |
" };\n" + |
| 1495 |
" }\n" + |
| 1496 |
"}\n" |
| 1497 |
}, |
| 1498 |
"----------\n" + |
| 1499 |
"1. WARNING in X.java (at line 4)\n" + |
| 1500 |
" final FileReader reader31 = new FileReader(\"file\");\n" + |
| 1501 |
" ^^^^^^^^\n" + |
| 1502 |
"Potential resource leak: 'reader31' may not be closed\n" + |
| 1503 |
"----------\n", |
| 1504 |
null, |
| 1505 |
true, |
| 1506 |
options); |
| 1507 |
} |
| 1508 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1509 |
// resource assigned to second local and is (potentially) closed on the latter |
| 1510 |
public void test056z() { |
| 1511 |
Map options = getCompilerOptions(); |
| 1512 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1513 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1514 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1515 |
this.runNegativeTest( |
| 1516 |
new String[] { |
| 1517 |
"X.java", |
| 1518 |
"import java.io.FileReader;\n" + |
| 1519 |
"public class X {\n" + |
| 1520 |
" void foo17() throws Exception {\n" + |
| 1521 |
" FileReader reader17 = new FileReader(\"file\");\n" + |
| 1522 |
" final FileReader readerCopy = reader17;\n" + |
| 1523 |
" readerCopy.close();\n" + |
| 1524 |
" }\n" + |
| 1525 |
" void foo17a() throws Exception {\n" + |
| 1526 |
" FileReader reader17a = new FileReader(\"file\");\n" + |
| 1527 |
" FileReader readerCopya;" + |
| 1528 |
" readerCopya = reader17a;\n" + |
| 1529 |
" bar(readerCopya);\n" + // potentially closes |
| 1530 |
" }\n" + |
| 1531 |
" void bar(FileReader r) {}\n" + |
| 1532 |
"}\n" |
| 1533 |
}, |
| 1534 |
"----------\n" + |
| 1535 |
"1. ERROR in X.java (at line 9)\n" + |
| 1536 |
" FileReader reader17a = new FileReader(\"file\");\n" + |
| 1537 |
" ^^^^^^^^^\n" + |
| 1538 |
"Potential resource leak: 'reader17a' may not be closed\n" + |
| 1539 |
"----------\n", |
| 1540 |
null, |
| 1541 |
true, |
| 1542 |
options); |
| 1543 |
} |
| 1544 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1545 |
// multiple early exists from nested scopes (always closed) |
| 1546 |
public void test056zz() { |
| 1547 |
Map options = getCompilerOptions(); |
| 1548 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1549 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1550 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
| 1551 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1552 |
runTestsExpectingErrorsOnlyIn17( |
| 1553 |
new String[] { |
| 1554 |
"X.java", |
| 1555 |
"import java.io.FileReader;\n" + |
| 1556 |
"public class X {\n" + |
| 1557 |
" void foo16() throws Exception {\n" + |
| 1558 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
| 1559 |
" try {\n" + |
| 1560 |
" reader16.close();\n " + |
| 1561 |
" return;\n" + |
| 1562 |
" } catch (RuntimeException re) {\n" + |
| 1563 |
" return;\n" + |
| 1564 |
" } catch (Error e) {\n" + |
| 1565 |
" return;\n" + |
| 1566 |
" } finally {\n" + |
| 1567 |
" reader16.close();\n " + |
| 1568 |
" }\n" + |
| 1569 |
" }\n" + |
| 1570 |
"}\n" |
| 1571 |
}, |
| 1572 |
"----------\n" + |
| 1573 |
"1. ERROR in X.java (at line 4)\n" + |
| 1574 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
| 1575 |
" ^^^^^^^^\n" + |
| 1576 |
"Resource 'reader16' should be managed by try-with-resource\n" + |
| 1577 |
"----------\n", |
| 1578 |
options); |
| 1579 |
} |
| 1580 |
// Bug 349326 - [1.7] new warning for missing try-with-resources |
| 1581 |
// multiple early exists from nested scopes (never closed) |
| 1582 |
public void test056zzz() { |
| 1583 |
Map options = getCompilerOptions(); |
| 1584 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1585 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1586 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
| 1587 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1588 |
this.runNegativeTest( |
| 1589 |
new String[] { |
| 1590 |
"X.java", |
| 1591 |
"import java.io.FileReader;\n" + |
| 1592 |
"public class X {\n" + |
| 1593 |
" void foo16() throws Exception {\n" + |
| 1594 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
| 1595 |
" try {\n" + |
| 1596 |
" return;\n" + |
| 1597 |
" } catch (RuntimeException re) {\n" + |
| 1598 |
" return;\n" + |
| 1599 |
" } catch (Error e) {\n" + |
| 1600 |
" return;\n" + |
| 1601 |
" } finally {\n" + |
| 1602 |
" System.out.println();\n " + |
| 1603 |
" }\n" + |
| 1604 |
" }\n" + |
| 1605 |
"}\n" |
| 1606 |
}, |
| 1607 |
"----------\n" + |
| 1608 |
"1. ERROR in X.java (at line 4)\n" + |
| 1609 |
" FileReader reader16 = new FileReader(\"file\");\n" + |
| 1610 |
" ^^^^^^^^\n" + |
| 1611 |
"Resource leak: 'reader16' is never closed\n" + |
| 1612 |
"----------\n", |
| 1613 |
null, |
| 1614 |
true, |
| 1615 |
options); |
| 1616 |
} |
| 1617 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
| 1618 |
// explicit throw is a true method exit here |
| 1619 |
public void test056throw1() { |
| 1620 |
Map options = getCompilerOptions(); |
| 1621 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1622 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1623 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
| 1624 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1625 |
this.runNegativeTest( |
| 1626 |
new String[] { |
| 1627 |
"X.java", |
| 1628 |
"import java.io.FileReader;\n" + |
| 1629 |
"public class X {\n" + |
| 1630 |
" void foo2(boolean a, boolean b, boolean c) throws Exception {\n" + |
| 1631 |
" FileReader reader = new FileReader(\"file\");\n" + |
| 1632 |
" if(a)\n" + |
| 1633 |
" throw new Exception(); //warning 1\n" + |
| 1634 |
" else if (b)\n" + |
| 1635 |
" reader.close();\n" + |
| 1636 |
" else if(c)\n" + |
| 1637 |
" throw new Exception(); //warning 2\n" + |
| 1638 |
" reader.close();\n" + |
| 1639 |
" }\n" + |
| 1640 |
"}\n" |
| 1641 |
}, |
| 1642 |
"----------\n" + |
| 1643 |
"1. ERROR in X.java (at line 6)\n" + |
| 1644 |
" throw new Exception(); //warning 1\n" + |
| 1645 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1646 |
"Resource leak: 'reader' is not closed at this location\n" + |
| 1647 |
"----------\n" + |
| 1648 |
"2. ERROR in X.java (at line 10)\n" + |
| 1649 |
" throw new Exception(); //warning 2\n" + |
| 1650 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1651 |
"Resource leak: 'reader' is not closed at this location\n" + |
| 1652 |
"----------\n", |
| 1653 |
null, |
| 1654 |
true, |
| 1655 |
options); |
| 1656 |
} |
| 1657 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
| 1658 |
// close() within finally provides protection for throw |
| 1659 |
public void test056throw2() { |
| 1660 |
Map options = getCompilerOptions(); |
| 1661 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1662 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1663 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
| 1664 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1665 |
runTestsExpectingErrorsOnlyIn17( |
| 1666 |
new String[] { |
| 1667 |
"X.java", |
| 1668 |
"import java.io.FileReader;\n" + |
| 1669 |
"public class X {\n" + |
| 1670 |
" void foo1() throws Exception {\n" + |
| 1671 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
| 1672 |
" try {\n" + |
| 1673 |
" reader.read();\n" + |
| 1674 |
" return;\n" + |
| 1675 |
" } catch (Exception e) {\n" + |
| 1676 |
" throw new Exception();\n" + |
| 1677 |
" } finally {\n" + |
| 1678 |
" reader.close();\n" + |
| 1679 |
" }\n" + |
| 1680 |
" }\n" + |
| 1681 |
"\n" + |
| 1682 |
" void foo2() throws Exception {\n" + |
| 1683 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
| 1684 |
" try {\n" + |
| 1685 |
" reader.read();\n" + |
| 1686 |
" throw new Exception(); // should not warn here\n" + |
| 1687 |
" } catch (Exception e) {\n" + |
| 1688 |
" throw new Exception();\n" + |
| 1689 |
" } finally {\n" + |
| 1690 |
" reader.close();\n" + |
| 1691 |
" }\n" + |
| 1692 |
" }\n" + |
| 1693 |
"\n" + |
| 1694 |
" void foo3() throws Exception {\n" + |
| 1695 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
| 1696 |
" try {\n" + |
| 1697 |
" reader.read();\n" + |
| 1698 |
" throw new Exception();\n" + |
| 1699 |
" } finally {\n" + |
| 1700 |
" reader.close();\n" + |
| 1701 |
" }\n" + |
| 1702 |
" }\n" + |
| 1703 |
"}\n" |
| 1704 |
}, |
| 1705 |
"----------\n" + |
| 1706 |
"1. ERROR in X.java (at line 4)\n" + |
| 1707 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
| 1708 |
" ^^^^^^\n" + |
| 1709 |
"Resource 'reader' should be managed by try-with-resource\n" + |
| 1710 |
"----------\n" + |
| 1711 |
"2. ERROR in X.java (at line 16)\n" + |
| 1712 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
| 1713 |
" ^^^^^^\n" + |
| 1714 |
"Resource 'reader' should be managed by try-with-resource\n" + |
| 1715 |
"----------\n" + |
| 1716 |
"3. ERROR in X.java (at line 28)\n" + |
| 1717 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
| 1718 |
" ^^^^^^\n" + |
| 1719 |
"Resource 'reader' should be managed by try-with-resource\n" + |
| 1720 |
"----------\n", |
| 1721 |
options); |
| 1722 |
} |
| 1723 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
| 1724 |
// close() nested within finally provides protection for throw |
| 1725 |
public void test056throw3() { |
| 1726 |
Map options = getCompilerOptions(); |
| 1727 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1728 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1729 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
| 1730 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1731 |
runTestsExpectingErrorsOnlyIn17( |
| 1732 |
new String[] { |
| 1733 |
"X.java", |
| 1734 |
"import java.io.FileReader;\n" + |
| 1735 |
"public class X {\n" + |
| 1736 |
" void foo2x() throws Exception {\n" + |
| 1737 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
| 1738 |
" try {\n" + |
| 1739 |
" reader.read();\n" + |
| 1740 |
" throw new Exception(); // should not warn here\n" + |
| 1741 |
" } catch (Exception e) {\n" + |
| 1742 |
" throw new Exception();\n" + |
| 1743 |
" } finally {\n" + |
| 1744 |
" if (reader != null)\n" + |
| 1745 |
" try {\n" + |
| 1746 |
" reader.close();\n" + |
| 1747 |
" } catch (java.io.IOException io) {}\n" + |
| 1748 |
" }\n" + |
| 1749 |
" }\n" + |
| 1750 |
"}\n" |
| 1751 |
}, |
| 1752 |
"----------\n" + |
| 1753 |
"1. ERROR in X.java (at line 4)\n" + |
| 1754 |
" FileReader reader = new FileReader(\"file\"); // propose t-w-r\n" + |
| 1755 |
" ^^^^^^\n" + |
| 1756 |
"Resource 'reader' should be managed by try-with-resource\n" + |
| 1757 |
"----------\n", |
| 1758 |
options); |
| 1759 |
} |
| 1760 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
| 1761 |
// additional boolean should shed doubt on whether we reach the close() call |
| 1762 |
public void test056throw4() { |
| 1763 |
Map options = getCompilerOptions(); |
| 1764 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1765 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1766 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
| 1767 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1768 |
this.runNegativeTest( |
| 1769 |
new String[] { |
| 1770 |
"X.java", |
| 1771 |
"import java.io.FileReader;\n" + |
| 1772 |
"public class X {\n" + |
| 1773 |
" void foo2x(boolean b) throws Exception {\n" + |
| 1774 |
" FileReader reader = new FileReader(\"file\");\n" + |
| 1775 |
" try {\n" + |
| 1776 |
" reader.read();\n" + |
| 1777 |
" throw new Exception(); // should warn here\n" + |
| 1778 |
" } catch (Exception e) {\n" + |
| 1779 |
" throw new Exception(); // should warn here\n" + |
| 1780 |
" } finally {\n" + |
| 1781 |
" if (reader != null && b)\n" + // this condition is too strong to protect reader |
| 1782 |
" try {\n" + |
| 1783 |
" reader.close();\n" + |
| 1784 |
" } catch (java.io.IOException io) {}\n" + |
| 1785 |
" }\n" + |
| 1786 |
" }\n" + |
| 1787 |
"}\n" |
| 1788 |
}, |
| 1789 |
"----------\n" + |
| 1790 |
"1. ERROR in X.java (at line 7)\n" + |
| 1791 |
" throw new Exception(); // should warn here\n" + |
| 1792 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1793 |
"Potential resource leak: 'reader' may not be closed at this location\n" + |
| 1794 |
"----------\n" + |
| 1795 |
"2. ERROR in X.java (at line 9)\n" + |
| 1796 |
" throw new Exception(); // should warn here\n" + |
| 1797 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1798 |
"Potential resource leak: 'reader' may not be closed at this location\n" + |
| 1799 |
"----------\n", |
| 1800 |
null, |
| 1801 |
true, |
| 1802 |
options); |
| 1803 |
} |
| 1804 |
// Bug 359334 - Analysis for resource leak warnings does not consider exceptions as method exit points |
| 1805 |
// similar to test056throw3() but indirectly calling close(), so doubts remain. |
| 1806 |
public void test056throw5() { |
| 1807 |
Map options = getCompilerOptions(); |
| 1808 |
options.put(JavaCore.COMPILER_PB_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1809 |
options.put(JavaCore.COMPILER_PB_POTENTIALLY_UNCLOSED_CLOSEABLE, CompilerOptions.ERROR); |
| 1810 |
options.put(JavaCore.COMPILER_PB_EXPLICITLY_CLOSED_AUTOCLOSEABLE, CompilerOptions.ERROR); |
| 1811 |
options.put(JavaCore.COMPILER_PB_DEAD_CODE, CompilerOptions.ERROR); |
| 1812 |
this.runNegativeTest( |
| 1813 |
new String[] { |
| 1814 |
"X.java", |
| 1815 |
"import java.io.FileReader;\n" + |
| 1816 |
"public class X {\n" + |
| 1817 |
" void foo2x() throws Exception {\n" + |
| 1818 |
" FileReader reader = new FileReader(\"file\");\n" + |
| 1819 |
" try {\n" + |
| 1820 |
" reader.read();\n" + |
| 1821 |
" throw new Exception(); // should warn 'may not' here\n" + |
| 1822 |
" } catch (Exception e) {\n" + |
| 1823 |
" throw new Exception(); // should warn 'may not' here\n" + |
| 1824 |
" } finally {\n" + |
| 1825 |
" doClose(reader);\n" + |
| 1826 |
" }\n" + |
| 1827 |
" }\n" + |
| 1828 |
" void doClose(FileReader r) { try { r.close(); } catch (java.io.IOException ex) {}}\n" + |
| 1829 |
"}\n" |
| 1830 |
}, |
| 1831 |
"----------\n" + |
| 1832 |
"1. ERROR in X.java (at line 7)\n" + |
| 1833 |
" throw new Exception(); // should warn \'may not\' here\n" + |
| 1834 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1835 |
"Potential resource leak: 'reader' may not be closed at this location\n" + |
| 1836 |
"----------\n" + |
| 1837 |
"2. ERROR in X.java (at line 9)\n" + |
| 1838 |
" throw new Exception(); // should warn \'may not\' here\n" + |
| 1839 |
" ^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 1840 |
"Potential resource leak: 'reader' may not be closed at this location\n" + |
| 1841 |
"----------\n", |
| 1842 |
null, |
| 1843 |
true, |
| 1844 |
options); |
| 1845 |
} |
| 1846 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 1847 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
| 1848 |
// a resource wrapper is not closed but the underlying resource is |
| 1849 |
public void test061a() { |
| 1850 |
Map options = getCompilerOptions(); |
| 1851 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 1852 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 1853 |
this.runConformTest( |
| 1854 |
new String[] { |
| 1855 |
"X.java", |
| 1856 |
"import java.io.File;\n" + |
| 1857 |
"import java.io.BufferedInputStream;\n" + |
| 1858 |
"import java.io.FileInputStream;\n" + |
| 1859 |
"import java.io.IOException;\n" + |
| 1860 |
"public class X {\n" + |
| 1861 |
" void foo() throws IOException {\n" + |
| 1862 |
" File file = new File(\"somefile\");\n" + |
| 1863 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
| 1864 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
| 1865 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
| 1866 |
" System.out.println(bis.available());\n" + |
| 1867 |
" fileStream.close();\n" + |
| 1868 |
" }\n" + |
| 1869 |
" void inline() throws IOException {\n" + |
| 1870 |
" File file = new File(\"somefile\");\n" + |
| 1871 |
" FileInputStream fileStream;\n" + |
| 1872 |
" BufferedInputStream bis = new BufferedInputStream(fileStream = new FileInputStream(file));\n" + |
| 1873 |
" System.out.println(bis.available());\n" + |
| 1874 |
" fileStream.close();\n" + |
| 1875 |
" }\n" + |
| 1876 |
" public static void main(String[] args) throws IOException {\n" + |
| 1877 |
" try {\n" + |
| 1878 |
" new X().foo();\n" + |
| 1879 |
" } catch (IOException ex) {" + |
| 1880 |
" System.out.println(\"Got IO Exception\");\n" + |
| 1881 |
" }\n" + |
| 1882 |
" }\n" + |
| 1883 |
"}\n" |
| 1884 |
}, |
| 1885 |
"Got IO Exception", |
| 1886 |
null, |
| 1887 |
true, |
| 1888 |
null, |
| 1889 |
options, |
| 1890 |
null); |
| 1891 |
} |
| 1892 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 1893 |
// a closeable without OS resource is not closed |
| 1894 |
public void test061b() { |
| 1895 |
Map options = getCompilerOptions(); |
| 1896 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 1897 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 1898 |
this.runConformTest( |
| 1899 |
new String[] { |
| 1900 |
"X.java", |
| 1901 |
"import java.io.StringReader;\n" + |
| 1902 |
"import java.io.IOException;\n" + |
| 1903 |
"public class X {\n" + |
| 1904 |
" void foo() throws IOException {\n" + |
| 1905 |
" StringReader string = new StringReader(\"content\");\n" + |
| 1906 |
" System.out.println(string.read());\n" + |
| 1907 |
" }\n" + |
| 1908 |
" public static void main(String[] args) throws IOException {\n" + |
| 1909 |
" new X().foo();\n" + |
| 1910 |
" }\n" + |
| 1911 |
"}\n" |
| 1912 |
}, |
| 1913 |
"99", // character 'c' |
| 1914 |
null, |
| 1915 |
true, |
| 1916 |
null, |
| 1917 |
options, |
| 1918 |
null); |
| 1919 |
} |
| 1920 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 1921 |
// a resource wrapper is not closed but the underlying closeable is resource-free |
| 1922 |
public void test061c() { |
| 1923 |
Map options = getCompilerOptions(); |
| 1924 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 1925 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 1926 |
this.runConformTest( |
| 1927 |
new String[] { |
| 1928 |
"X.java", |
| 1929 |
"import java.io.BufferedReader;\n" + |
| 1930 |
"import java.io.StringReader;\n" + |
| 1931 |
"import java.io.IOException;\n" + |
| 1932 |
"public class X {\n" + |
| 1933 |
" void foo() throws IOException {\n" + |
| 1934 |
" StringReader input = new StringReader(\"content\");\n" + |
| 1935 |
" BufferedReader br = new BufferedReader(input);\n" + |
| 1936 |
" BufferedReader doubleWrap = new BufferedReader(br);\n" + |
| 1937 |
" System.out.println(br.read());\n" + |
| 1938 |
" }\n" + |
| 1939 |
" void inline() throws IOException {\n" + |
| 1940 |
" BufferedReader br = new BufferedReader(new StringReader(\"content\"));\n" + |
| 1941 |
" System.out.println(br.read());\n" + |
| 1942 |
" }\n" + |
| 1943 |
" public static void main(String[] args) throws IOException {\n" + |
| 1944 |
" new X().foo();\n" + |
| 1945 |
" }\n" + |
| 1946 |
"}\n" |
| 1947 |
}, |
| 1948 |
"99", |
| 1949 |
null, |
| 1950 |
true, |
| 1951 |
null, |
| 1952 |
options, |
| 1953 |
null); |
| 1954 |
} |
| 1955 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 1956 |
// a resource wrapper is not closed neither is the underlying resource |
| 1957 |
public void test061d() { |
| 1958 |
Map options = getCompilerOptions(); |
| 1959 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 1960 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.WARNING); |
| 1961 |
this.runNegativeTest( |
| 1962 |
new String[] { |
| 1963 |
"X.java", |
| 1964 |
"import java.io.File;\n" + |
| 1965 |
"import java.io.BufferedInputStream;\n" + |
| 1966 |
"import java.io.FileInputStream;\n" + |
| 1967 |
"import java.io.IOException;\n" + |
| 1968 |
"public class X {\n" + |
| 1969 |
" void foo() throws IOException {\n" + |
| 1970 |
" File file = new File(\"somefile\");\n" + |
| 1971 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
| 1972 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
| 1973 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
| 1974 |
" System.out.println(bis.available());\n" + |
| 1975 |
" }\n" + |
| 1976 |
" void inline() throws IOException {\n" + |
| 1977 |
" File file = new File(\"somefile\");\n" + |
| 1978 |
" BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file));\n" + |
| 1979 |
" System.out.println(bis2.available());\n" + |
| 1980 |
" }\n" + |
| 1981 |
" public static void main(String[] args) throws IOException {\n" + |
| 1982 |
" try {\n" + |
| 1983 |
" new X().foo();\n" + |
| 1984 |
" } catch (IOException ex) {" + |
| 1985 |
" System.out.println(\"Got IO Exception\");\n" + |
| 1986 |
" }\n" + |
| 1987 |
" }\n" + |
| 1988 |
"}\n" |
| 1989 |
}, |
| 1990 |
"----------\n" + |
| 1991 |
"1. ERROR in X.java (at line 10)\n" + |
| 1992 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
| 1993 |
" ^^^^^^^^^^\n" + |
| 1994 |
"Resource leak: \'doubleWrap\' is never closed\n" + |
| 1995 |
"----------\n" + |
| 1996 |
"2. ERROR in X.java (at line 15)\n" + |
| 1997 |
" BufferedInputStream bis2 = new BufferedInputStream(new FileInputStream(file));\n" + |
| 1998 |
" ^^^^\n" + |
| 1999 |
"Resource leak: \'bis2\' is never closed\n" + |
| 2000 |
"----------\n", |
| 2001 |
null, |
| 2002 |
true, |
| 2003 |
options); |
| 2004 |
} |
| 2005 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2006 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
| 2007 |
// a resource wrapper is closed closing also the underlying resource |
| 2008 |
public void test061e() { |
| 2009 |
Map options = getCompilerOptions(); |
| 2010 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2011 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2012 |
this.runConformTest( |
| 2013 |
new String[] { |
| 2014 |
"X.java", |
| 2015 |
"import java.io.File;\n" + |
| 2016 |
"import java.io.BufferedInputStream;\n" + |
| 2017 |
"import java.io.FileInputStream;\n" + |
| 2018 |
"import java.io.IOException;\n" + |
| 2019 |
"public class X {\n" + |
| 2020 |
" FileInputStream fis;" + |
| 2021 |
" void foo() throws IOException {\n" + |
| 2022 |
" File file = new File(\"somefile\");\n" + |
| 2023 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
| 2024 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
| 2025 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
| 2026 |
" System.out.println(bis.available());\n" + |
| 2027 |
" bis.close();\n" + |
| 2028 |
" }\n" + |
| 2029 |
" void inline() throws IOException {\n" + |
| 2030 |
" File file = new File(\"somefile\");\n" + |
| 2031 |
" BufferedInputStream bis2 = new BufferedInputStream(fis = new FileInputStream(file));\n" + // field assignment |
| 2032 |
" System.out.println(bis2.available());\n" + |
| 2033 |
" bis2.close();\n" + |
| 2034 |
" FileInputStream fileStream = null;\n" + |
| 2035 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream = new FileInputStream(file));\n" + |
| 2036 |
" System.out.println(bis3.available());\n" + |
| 2037 |
" bis3.close();\n" + |
| 2038 |
" }\n" + |
| 2039 |
" public static void main(String[] args) throws IOException {\n" + |
| 2040 |
" try {\n" + |
| 2041 |
" new X().foo();\n" + |
| 2042 |
" } catch (IOException ex) {" + |
| 2043 |
" System.out.println(\"Got IO Exception\");\n" + |
| 2044 |
" }\n" + |
| 2045 |
" }\n" + |
| 2046 |
"}\n" |
| 2047 |
}, |
| 2048 |
"Got IO Exception", |
| 2049 |
null, |
| 2050 |
true, |
| 2051 |
null, |
| 2052 |
options, |
| 2053 |
null); |
| 2054 |
} |
| 2055 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2056 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
| 2057 |
// a resource wrapper is closed closing also the underlying resource - original test case |
| 2058 |
public void test061f() throws IOException { |
| 2059 |
Map options = getCompilerOptions(); |
| 2060 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2061 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2062 |
URL url = FileLocator.toFileURL(FileLocator.find(Platform.getBundle("org.eclipse.jdt.core.tests.compiler"), new Path("META-INF/MANIFEST.MF"), null)); |
| 2063 |
this.runConformTest( |
| 2064 |
new String[] { |
| 2065 |
"X.java", |
| 2066 |
"import java.io.InputStream;\n" + |
| 2067 |
"import java.io.InputStreamReader;\n" + |
| 2068 |
"import java.io.BufferedReader;\n" + |
| 2069 |
"import java.io.IOException;\n" + |
| 2070 |
"import java.net.URL;\n" + |
| 2071 |
"public class X {\n" + |
| 2072 |
" boolean loadURL(final URL url) throws IOException {\n" + |
| 2073 |
" InputStream stream = null;\n" + |
| 2074 |
" BufferedReader reader = null;\n" + |
| 2075 |
" try {\n" + |
| 2076 |
" stream = url.openStream();\n" + |
| 2077 |
" reader = new BufferedReader(new InputStreamReader(stream));\n" + |
| 2078 |
" System.out.println(reader.readLine());\n" + |
| 2079 |
" } finally {\n" + |
| 2080 |
" try {\n" + |
| 2081 |
" if (reader != null)\n" + |
| 2082 |
" reader.close();\n" + |
| 2083 |
" } catch (IOException x) {\n" + |
| 2084 |
" }\n" + |
| 2085 |
" }\n" + |
| 2086 |
" return false; // 'stream' may not be closed at this location\n" + |
| 2087 |
" }\n" + |
| 2088 |
" public static void main(String[] args) throws IOException {\n" + |
| 2089 |
" try {\n" + |
| 2090 |
" new X().loadURL(new URL(\""+url.toString()+"\"));\n" + |
| 2091 |
" } catch (IOException ex) {\n" + |
| 2092 |
" System.out.println(\"Got IO Exception\"+ex);\n" + |
| 2093 |
" }\n" + |
| 2094 |
" }\n" + |
| 2095 |
"}\n" |
| 2096 |
}, |
| 2097 |
"Manifest-Version: 1.0", |
| 2098 |
null, |
| 2099 |
true, |
| 2100 |
null, |
| 2101 |
options, |
| 2102 |
null); |
| 2103 |
} |
| 2104 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2105 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
| 2106 |
// a resource wrapper is closed closing also the underlying resource - from a real-world example |
| 2107 |
public void test061f2() throws IOException { |
| 2108 |
Map options = getCompilerOptions(); |
| 2109 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2110 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2111 |
this.runConformTest( |
| 2112 |
new String[] { |
| 2113 |
"X.java", |
| 2114 |
"import java.io.OutputStream;\n" + |
| 2115 |
"import java.io.FileOutputStream;\n" + |
| 2116 |
"import java.io.BufferedOutputStream;\n" + |
| 2117 |
"import java.io.IOException;\n" + |
| 2118 |
"public class X {\n" + |
| 2119 |
" void zork() throws IOException {\n" + |
| 2120 |
" try {\n" + |
| 2121 |
" OutputStream os = null;\n" + |
| 2122 |
" try {\n" + |
| 2123 |
" os = new BufferedOutputStream(new FileOutputStream(\"somefile\"));\n" + |
| 2124 |
" String externalForm = \"externalPath\";\n" + |
| 2125 |
" } finally {\n" + |
| 2126 |
" if (os != null)\n" + |
| 2127 |
" os.close();\n" + |
| 2128 |
" }\n" + |
| 2129 |
" } catch (IOException e) {\n" + |
| 2130 |
" e.printStackTrace();\n" + |
| 2131 |
" }\n" + |
| 2132 |
" }\n" + |
| 2133 |
"}\n" |
| 2134 |
}, |
| 2135 |
"", |
| 2136 |
null, |
| 2137 |
true, |
| 2138 |
null, |
| 2139 |
options, |
| 2140 |
null); |
| 2141 |
} |
| 2142 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2143 |
// Bug 361073 - Avoid resource leak warning when the top level resource is closed explicitly |
| 2144 |
// a resource wrapper is sent to another method affecting also the underlying resource - from a real-world example |
| 2145 |
public void test061f3() throws IOException { |
| 2146 |
Map options = getCompilerOptions(); |
| 2147 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2148 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2149 |
this.runNegativeTest( |
| 2150 |
new String[] { |
| 2151 |
"X.java", |
| 2152 |
"import java.io.File;\n" + |
| 2153 |
"import java.io.FileInputStream;\n" + |
| 2154 |
"import java.io.FileNotFoundException;\n" + |
| 2155 |
"import java.io.InputStream;\n" + |
| 2156 |
"import java.io.BufferedInputStream;\n" + |
| 2157 |
"public class X {\n" + |
| 2158 |
" String loadProfile(File profileFile) {\n" + |
| 2159 |
" try {\n" + |
| 2160 |
" InputStream stream = new BufferedInputStream(new FileInputStream(profileFile));\n" + |
| 2161 |
" return loadProfile(stream);\n" + |
| 2162 |
" } catch (FileNotFoundException e) {\n" + |
| 2163 |
" //null\n" + |
| 2164 |
" }\n" + |
| 2165 |
" return null;\n" + |
| 2166 |
" }\n" + |
| 2167 |
" private String loadProfile(InputStream stream) {\n" + |
| 2168 |
" return null;\n" + |
| 2169 |
" }\n" + |
| 2170 |
"}\n" |
| 2171 |
}, |
| 2172 |
"----------\n" + |
| 2173 |
"1. ERROR in X.java (at line 10)\n" + |
| 2174 |
" return loadProfile(stream);\n" + |
| 2175 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2176 |
"Potential resource leak: \'stream\' may not be closed at this location\n" + |
| 2177 |
"----------\n", |
| 2178 |
null, |
| 2179 |
true, |
| 2180 |
options); |
| 2181 |
} |
| 2182 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2183 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
| 2184 |
// Different points in a resource chain are closed |
| 2185 |
public void test061g() { |
| 2186 |
Map options = getCompilerOptions(); |
| 2187 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2188 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2189 |
this.runNegativeTest( |
| 2190 |
new String[] { |
| 2191 |
"X.java", |
| 2192 |
"import java.io.File;\n" + |
| 2193 |
"import java.io.BufferedInputStream;\n" + |
| 2194 |
"import java.io.FileInputStream;\n" + |
| 2195 |
"import java.io.IOException;\n" + |
| 2196 |
"public class X {\n" + |
| 2197 |
" void closeMiddle() throws IOException {\n" + |
| 2198 |
" File file = new File(\"somefile\");\n" + |
| 2199 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
| 2200 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
| 2201 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
| 2202 |
" System.out.println(bis.available());\n" + |
| 2203 |
" bis.close();\n" + |
| 2204 |
" }\n" + |
| 2205 |
" void closeOuter() throws IOException {\n" + |
| 2206 |
" File file2 = new File(\"somefile\");\n" + |
| 2207 |
" FileInputStream fileStream2 = new FileInputStream(file2);\n" + |
| 2208 |
" BufferedInputStream bis2 = new BufferedInputStream(fileStream2);\n" + |
| 2209 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
| 2210 |
" System.out.println(bis2.available());\n" + |
| 2211 |
" doubleWrap2.close();\n" + |
| 2212 |
" }\n" + |
| 2213 |
" void neverClosed() throws IOException {\n" + |
| 2214 |
" File file3 = new File(\"somefile\");\n" + |
| 2215 |
" FileInputStream fileStream3 = new FileInputStream(file3);\n" + |
| 2216 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream3);\n" + |
| 2217 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
| 2218 |
" System.out.println(doubleWrap3.available());\n" + |
| 2219 |
" }\n" + |
| 2220 |
"}\n" |
| 2221 |
}, |
| 2222 |
"----------\n" + |
| 2223 |
"1. ERROR in X.java (at line 26)\n" + |
| 2224 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
| 2225 |
" ^^^^^^^^^^^\n" + |
| 2226 |
"Resource leak: \'doubleWrap3\' is never closed\n" + |
| 2227 |
"----------\n", |
| 2228 |
null, |
| 2229 |
true, |
| 2230 |
options); |
| 2231 |
} |
| 2232 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2233 |
// Bug 360908 - Avoid resource leak warning when the underlying/chained resource is closed explicitly |
| 2234 |
// Different points in a resource chain are potentially closed |
| 2235 |
public void test061h() { |
| 2236 |
Map options = getCompilerOptions(); |
| 2237 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2238 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2239 |
this.runNegativeTest( |
| 2240 |
new String[] { |
| 2241 |
"X.java", |
| 2242 |
"import java.io.File;\n" + |
| 2243 |
"import java.io.BufferedInputStream;\n" + |
| 2244 |
"import java.io.FileInputStream;\n" + |
| 2245 |
"import java.io.IOException;\n" + |
| 2246 |
"public class X {\n" + |
| 2247 |
" void closeMiddle(boolean b) throws IOException {\n" + |
| 2248 |
" File file = new File(\"somefile\");\n" + |
| 2249 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
| 2250 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
| 2251 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
| 2252 |
" System.out.println(bis.available());\n" + |
| 2253 |
" if (b)\n" + |
| 2254 |
" bis.close();\n" + |
| 2255 |
" }\n" + |
| 2256 |
" void closeOuter(boolean b) throws IOException {\n" + |
| 2257 |
" File file2 = new File(\"somefile\");\n" + |
| 2258 |
" FileInputStream fileStream2 = new FileInputStream(file2);\n" + |
| 2259 |
" BufferedInputStream dummy;\n" + |
| 2260 |
" BufferedInputStream bis2 = (dummy = new BufferedInputStream(fileStream2));\n" + |
| 2261 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
| 2262 |
" System.out.println(bis2.available());\n" + |
| 2263 |
" if (b)\n" + |
| 2264 |
" doubleWrap2.close();\n" + |
| 2265 |
" }\n" + |
| 2266 |
" void potAndDef(boolean b) throws IOException {\n" + |
| 2267 |
" File file3 = new File(\"somefile\");\n" + |
| 2268 |
" FileInputStream fileStream3 = new FileInputStream(file3);\n" + |
| 2269 |
" BufferedInputStream bis3 = new BufferedInputStream(fileStream3);\n" + |
| 2270 |
" BufferedInputStream doubleWrap3 = new BufferedInputStream(bis3);\n" + |
| 2271 |
" System.out.println(doubleWrap3.available());\n" + |
| 2272 |
" if (b) bis3.close();\n" + |
| 2273 |
" fileStream3.close();\n" + |
| 2274 |
" }\n" + |
| 2275 |
"}\n" |
| 2276 |
}, |
| 2277 |
"----------\n" + |
| 2278 |
"1. ERROR in X.java (at line 10)\n" + |
| 2279 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
| 2280 |
" ^^^^^^^^^^\n" + |
| 2281 |
"Potential resource leak: \'doubleWrap\' may not be closed\n" + |
| 2282 |
"----------\n" + |
| 2283 |
"2. ERROR in X.java (at line 20)\n" + |
| 2284 |
" BufferedInputStream doubleWrap2 = new BufferedInputStream(bis2);\n" + |
| 2285 |
" ^^^^^^^^^^^\n" + |
| 2286 |
"Potential resource leak: \'doubleWrap2\' may not be closed\n" + |
| 2287 |
"----------\n", |
| 2288 |
null, |
| 2289 |
true, |
| 2290 |
options); |
| 2291 |
} |
| 2292 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2293 |
// local var is re-used for two levels of wrappers |
| 2294 |
public void test061i() { |
| 2295 |
Map options = getCompilerOptions(); |
| 2296 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2297 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2298 |
this.runNegativeTest( |
| 2299 |
new String[] { |
| 2300 |
"X.java", |
| 2301 |
"import java.io.File;\n" + |
| 2302 |
"import java.io.InputStream;\n" + |
| 2303 |
"import java.io.BufferedInputStream;\n" + |
| 2304 |
"import java.io.FileInputStream;\n" + |
| 2305 |
"import java.io.IOException;\n" + |
| 2306 |
"public class X {\n" + |
| 2307 |
" void closeMiddle() throws IOException {\n" + |
| 2308 |
" File file = new File(\"somefile\");\n" + |
| 2309 |
" InputStream stream = new FileInputStream(file);\n" + |
| 2310 |
" stream = new BufferedInputStream(stream);\n" + |
| 2311 |
" InputStream middle;\n" + |
| 2312 |
" stream = new BufferedInputStream(middle = stream);\n" + |
| 2313 |
" System.out.println(stream.available());\n" + |
| 2314 |
" middle.close();\n" + |
| 2315 |
" }\n" + |
| 2316 |
" void closeOuter() throws IOException {\n" + |
| 2317 |
" File file = new File(\"somefile\");\n" + |
| 2318 |
" InputStream stream2 = new FileInputStream(file);\n" + |
| 2319 |
" stream2 = new BufferedInputStream(stream2);\n" + |
| 2320 |
" stream2 = new BufferedInputStream(stream2);\n" + |
| 2321 |
" System.out.println(stream2.available());\n" + |
| 2322 |
" stream2.close();\n" + |
| 2323 |
" }\n" + |
| 2324 |
" void neverClosed() throws IOException {\n" + |
| 2325 |
" File file = new File(\"somefile\");\n" + |
| 2326 |
" InputStream stream3 = new FileInputStream(file);\n" + |
| 2327 |
" stream3 = new BufferedInputStream(stream3);\n" + |
| 2328 |
" stream3 = new BufferedInputStream(stream3);\n" + |
| 2329 |
" System.out.println(stream3.available());\n" + |
| 2330 |
" }\n" + |
| 2331 |
"}\n" |
| 2332 |
}, |
| 2333 |
"----------\n" + |
| 2334 |
"1. ERROR in X.java (at line 26)\n" + |
| 2335 |
" InputStream stream3 = new FileInputStream(file);\n" + |
| 2336 |
" ^^^^^^^\n" + |
| 2337 |
"Resource leak: \'stream3\' is never closed\n" + |
| 2338 |
"----------\n", |
| 2339 |
null, |
| 2340 |
true, |
| 2341 |
options); |
| 2342 |
} |
| 2343 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2344 |
// self-wrapping a method argument (caused NPE UnconditionalFlowInfo.markAsDefinitelyNull(..)). |
| 2345 |
public void test061j() { |
| 2346 |
Map options = getCompilerOptions(); |
| 2347 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2348 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2349 |
this.runConformTest( |
| 2350 |
new String[] { |
| 2351 |
"X.java", |
| 2352 |
"import java.io.InputStream;\n" + |
| 2353 |
"import java.io.BufferedInputStream;\n" + |
| 2354 |
"import java.io.IOException;\n" + |
| 2355 |
"public class X {\n" + |
| 2356 |
" void foo(InputStream stream) throws IOException {\n" + |
| 2357 |
" stream = new BufferedInputStream(stream);\n" + |
| 2358 |
" System.out.println(stream.available());\n" + |
| 2359 |
" stream.close();\n" + |
| 2360 |
" }\n" + |
| 2361 |
" void boo(InputStream stream2) throws IOException {\n" + |
| 2362 |
" stream2 = new BufferedInputStream(stream2);\n" + |
| 2363 |
" System.out.println(stream2.available());\n" + |
| 2364 |
" }\n" + |
| 2365 |
"}\n" |
| 2366 |
}, |
| 2367 |
"", |
| 2368 |
null, |
| 2369 |
true, |
| 2370 |
null, |
| 2371 |
options, |
| 2372 |
null); |
| 2373 |
} |
| 2374 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2375 |
// a wrapper is created in a return statement |
| 2376 |
public void test061k() throws IOException { |
| 2377 |
Map options = getCompilerOptions(); |
| 2378 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2379 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2380 |
this.runConformTest( |
| 2381 |
new String[] { |
| 2382 |
"X.java", |
| 2383 |
"import java.io.File;\n" + |
| 2384 |
"import java.io.FileInputStream;\n" + |
| 2385 |
"import java.io.BufferedInputStream;\n" + |
| 2386 |
"import java.io.IOException;\n" + |
| 2387 |
"public class X {\n" + |
| 2388 |
" BufferedInputStream getReader(File file) throws IOException {\n" + |
| 2389 |
" FileInputStream stream = new FileInputStream(file);\n" + |
| 2390 |
" return new BufferedInputStream(stream);\n" + |
| 2391 |
" }\n" + |
| 2392 |
"}\n" |
| 2393 |
}, |
| 2394 |
"", |
| 2395 |
null, |
| 2396 |
true, |
| 2397 |
null, |
| 2398 |
options, |
| 2399 |
null); |
| 2400 |
} |
| 2401 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2402 |
// a closeable is assigned to a field |
| 2403 |
public void test061l() throws IOException { |
| 2404 |
Map options = getCompilerOptions(); |
| 2405 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2406 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2407 |
this.runConformTest( |
| 2408 |
new String[] { |
| 2409 |
"X.java", |
| 2410 |
"import java.io.File;\n" + |
| 2411 |
"import java.io.FileInputStream;\n" + |
| 2412 |
"import java.io.BufferedInputStream;\n" + |
| 2413 |
"import java.io.IOException;\n" + |
| 2414 |
"public class X {\n" + |
| 2415 |
" BufferedInputStream stream;\n" + |
| 2416 |
" void foo(File file) throws IOException {\n" + |
| 2417 |
" FileInputStream s = new FileInputStream(file);\n" + |
| 2418 |
" stream = new BufferedInputStream(s);\n" + |
| 2419 |
" }\n" + |
| 2420 |
"}\n" |
| 2421 |
}, |
| 2422 |
"", |
| 2423 |
null, |
| 2424 |
true, |
| 2425 |
null, |
| 2426 |
options, |
| 2427 |
null); |
| 2428 |
} |
| 2429 |
// Bug 361407 - Resource leak warning when resource is assigned to a field outside of constructor |
| 2430 |
// a closeable is assigned to a field - constructor vs. method |
| 2431 |
public void test061l2() throws IOException { |
| 2432 |
Map options = getCompilerOptions(); |
| 2433 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2434 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2435 |
this.runNegativeTest( |
| 2436 |
new String[] { |
| 2437 |
"xy/Leaks.java", |
| 2438 |
"package xy;\n" + |
| 2439 |
"\n" + |
| 2440 |
"import java.io.FileInputStream;\n" + |
| 2441 |
"import java.io.IOException;\n" + |
| 2442 |
"\n" + |
| 2443 |
"public class Leaks {\n" + |
| 2444 |
" private FileInputStream fInput;\n" + |
| 2445 |
"\n" + |
| 2446 |
" Leaks(String name) throws IOException {\n" + |
| 2447 |
" FileInputStream fileInputStream= new FileInputStream(name);\n" + |
| 2448 |
" fInput= fileInputStream;\n" + // warning silenced by field assignment |
| 2449 |
" Objects.hashCode(fInput);\n" + |
| 2450 |
" \n" + |
| 2451 |
" init(name);\n" + |
| 2452 |
" }\n" + |
| 2453 |
" \n" + |
| 2454 |
" Leaks() throws IOException {\n" + |
| 2455 |
" this(new FileInputStream(\"default\")); // potential problem\n" + |
| 2456 |
" }\n" + |
| 2457 |
" \n" + |
| 2458 |
" Leaks(FileInputStream fis) throws IOException {\n" + |
| 2459 |
" fInput= fis;\n" + |
| 2460 |
" }\n" + |
| 2461 |
" void init(String name) throws IOException {\n" + |
| 2462 |
" FileInputStream fileInputStream= new FileInputStream(name);\n" + |
| 2463 |
" fInput= fileInputStream;\n" + // warning silenced by field assignment |
| 2464 |
" Objects.hashCode(fInput);\n" + |
| 2465 |
" }\n" + |
| 2466 |
" \n" + |
| 2467 |
" public void dispose() throws IOException {\n" + |
| 2468 |
" fInput.close();\n" + |
| 2469 |
" }\n" + |
| 2470 |
"}\n" + |
| 2471 |
"class Objects {\n" + // mock java.util.Objects (@since 1.7). |
| 2472 |
" static int hashCode(Object o) { return 13; }\n" + |
| 2473 |
"}\n" |
| 2474 |
}, |
| 2475 |
"----------\n" + |
| 2476 |
"1. ERROR in xy\\Leaks.java (at line 18)\n" + |
| 2477 |
" this(new FileInputStream(\"default\")); // potential problem\n" + |
| 2478 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2479 |
"Potential resource leak: \'<unassigned Closeable value>\' may not be closed\n" + |
| 2480 |
"----------\n", |
| 2481 |
null, |
| 2482 |
true, |
| 2483 |
options); |
| 2484 |
} |
| 2485 |
// Bug 361407 - Resource leak warning when resource is assigned to a field outside of constructor |
| 2486 |
// a closeable is not assigned to a field - constructor vs. method |
| 2487 |
public void test061l3() throws IOException { |
| 2488 |
Map options = getCompilerOptions(); |
| 2489 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2490 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2491 |
this.runNegativeTest( |
| 2492 |
new String[] { |
| 2493 |
"xy/Leaks.java", |
| 2494 |
"package xy;\n" + |
| 2495 |
"\n" + |
| 2496 |
"import java.io.FileInputStream;\n" + |
| 2497 |
"import java.io.IOException;\n" + |
| 2498 |
"\n" + |
| 2499 |
"public class Leaks {\n" + |
| 2500 |
"\n" + |
| 2501 |
" Leaks(String name) throws IOException {\n" + |
| 2502 |
" FileInputStream fileInputStream= new FileInputStream(name);\n" + |
| 2503 |
" Objects.hashCode(fileInputStream);\n" + |
| 2504 |
" \n" + |
| 2505 |
" init(name);\n" + |
| 2506 |
" }\n" + |
| 2507 |
" void init(String name) throws IOException {\n" + |
| 2508 |
" FileInputStream fileInputStream= new FileInputStream(name);\n" + |
| 2509 |
" Objects.hashCode(fileInputStream);\n" + |
| 2510 |
" }\n" + |
| 2511 |
"}\n" + |
| 2512 |
"class Objects {\n" + // mock java.util.Objects (@since 1.7). |
| 2513 |
" static int hashCode(Object o) { return 13; }\n" + |
| 2514 |
"}\n" |
| 2515 |
}, |
| 2516 |
"----------\n" + |
| 2517 |
"1. ERROR in xy\\Leaks.java (at line 9)\n" + |
| 2518 |
" FileInputStream fileInputStream= new FileInputStream(name);\n" + |
| 2519 |
" ^^^^^^^^^^^^^^^\n" + |
| 2520 |
"Potential resource leak: \'fileInputStream\' may not be closed\n" + |
| 2521 |
"----------\n" + |
| 2522 |
"2. ERROR in xy\\Leaks.java (at line 15)\n" + |
| 2523 |
" FileInputStream fileInputStream= new FileInputStream(name);\n" + |
| 2524 |
" ^^^^^^^^^^^^^^^\n" + |
| 2525 |
"Potential resource leak: \'fileInputStream\' may not be closed\n" + |
| 2526 |
"----------\n", |
| 2527 |
null, |
| 2528 |
true, |
| 2529 |
options); |
| 2530 |
} |
| 2531 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2532 |
// a closeable is passed to another method in a return statement |
| 2533 |
// example constructed after org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactRepository#getArtifact(..) |
| 2534 |
public void test061m() throws IOException { |
| 2535 |
Map options = getCompilerOptions(); |
| 2536 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2537 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2538 |
this.runNegativeTest( |
| 2539 |
new String[] { |
| 2540 |
"X.java", |
| 2541 |
"import java.io.File;\n" + |
| 2542 |
"import java.io.FileInputStream;\n" + |
| 2543 |
"import java.io.BufferedInputStream;\n" + |
| 2544 |
"import java.io.InputStream;\n" + |
| 2545 |
"import java.io.IOException;\n" + |
| 2546 |
"public class X {\n" + |
| 2547 |
" BufferedInputStream stream;\n" + |
| 2548 |
" BufferedInputStream foo(File file) throws IOException {\n" + |
| 2549 |
" FileInputStream s = new FileInputStream(file);\n" + |
| 2550 |
" return check(new BufferedInputStream(s));\n" + |
| 2551 |
" }\n" + |
| 2552 |
" BufferedInputStream foo2(FileInputStream s, File file) throws IOException {\n" + |
| 2553 |
" s = new FileInputStream(file);\n" + |
| 2554 |
" return check(s);\n" + |
| 2555 |
" }\n" + |
| 2556 |
" BufferedInputStream foo3(InputStream s) throws IOException {\n" + |
| 2557 |
" s = check(s);\n" + |
| 2558 |
" return check(s);\n" + |
| 2559 |
" }\n" + |
| 2560 |
" BufferedInputStream check(InputStream s) { return null; }\n" + |
| 2561 |
"}\n" |
| 2562 |
}, |
| 2563 |
// TODO: also these warnings *might* be avoidable by detecting check(s) as a wrapper creation?? |
| 2564 |
"----------\n" + |
| 2565 |
"1. ERROR in X.java (at line 10)\n" + |
| 2566 |
" return check(new BufferedInputStream(s));\n" + |
| 2567 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2568 |
"Potential resource leak: \'<unassigned Closeable value>\' may not be closed at this location\n" + |
| 2569 |
"----------\n" + |
| 2570 |
"2. ERROR in X.java (at line 14)\n" + |
| 2571 |
" return check(s);\n" + |
| 2572 |
" ^^^^^^^^^^^^^^^^\n" + |
| 2573 |
"Potential resource leak: \'s\' may not be closed at this location\n" + |
| 2574 |
"----------\n" + |
| 2575 |
"3. ERROR in X.java (at line 18)\n" + |
| 2576 |
" return check(s);\n" + |
| 2577 |
" ^^^^^^^^^^^^^^^^\n" + |
| 2578 |
"Potential resource leak: \'s\' may not be closed at this location\n" + |
| 2579 |
"----------\n", |
| 2580 |
null, |
| 2581 |
true, |
| 2582 |
options); |
| 2583 |
} |
| 2584 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2585 |
// a resource wrapper does not wrap any provided resource |
| 2586 |
public void test061n() { |
| 2587 |
Map options = getCompilerOptions(); |
| 2588 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2589 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2590 |
this.runNegativeTest( |
| 2591 |
new String[] { |
| 2592 |
"X.java", |
| 2593 |
"import java.io.PrintWriter;\n" + |
| 2594 |
"import java.io.IOException;\n" + |
| 2595 |
"public class X {\n" + |
| 2596 |
" void foo() throws IOException {\n" + |
| 2597 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
| 2598 |
" writer.write(1);\n" + |
| 2599 |
" }\n" + |
| 2600 |
"}\n" |
| 2601 |
}, |
| 2602 |
"----------\n" + |
| 2603 |
"1. ERROR in X.java (at line 5)\n" + |
| 2604 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
| 2605 |
" ^^^^^^\n" + |
| 2606 |
"Resource leak: \'writer\' is never closed\n" + |
| 2607 |
"----------\n", |
| 2608 |
null, |
| 2609 |
true, |
| 2610 |
options); |
| 2611 |
} |
| 2612 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2613 |
// a resource wrapper is closed only in its local block, underlying resource may leak |
| 2614 |
public void test061o() { |
| 2615 |
Map options = getCompilerOptions(); |
| 2616 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2617 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2618 |
this.runNegativeTest( |
| 2619 |
new String[] { |
| 2620 |
"X.java", |
| 2621 |
"import java.io.File;\n" + |
| 2622 |
"import java.io.FileInputStream;\n" + |
| 2623 |
"import java.io.BufferedInputStream;\n" + |
| 2624 |
"import java.io.IOException;\n" + |
| 2625 |
"public class X {\n" + |
| 2626 |
" void foo(boolean bar) throws IOException {\n" + |
| 2627 |
" File file = new File(\"somefil\");\n" + |
| 2628 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
| 2629 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
| 2630 |
" if (bar) {\n" + |
| 2631 |
" BufferedInputStream doubleWrap = new BufferedInputStream(bis);\n" + |
| 2632 |
" doubleWrap.close();\n" + |
| 2633 |
" }\n" + |
| 2634 |
" }\n" + |
| 2635 |
"}\n" |
| 2636 |
}, |
| 2637 |
"----------\n" + |
| 2638 |
"1. ERROR in X.java (at line 9)\n" + |
| 2639 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
| 2640 |
" ^^^\n" + |
| 2641 |
"Potential resource leak: \'bis\' may not be closed\n" + |
| 2642 |
"----------\n", |
| 2643 |
null, |
| 2644 |
true, |
| 2645 |
options); |
| 2646 |
} |
| 2647 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2648 |
// a resource wrapper is conditionally allocated but not closed - from a real-world example |
| 2649 |
public void test061f4() throws IOException { |
| 2650 |
Map options = getCompilerOptions(); |
| 2651 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2652 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2653 |
this.runNegativeTest( |
| 2654 |
new String[] { |
| 2655 |
"X.java", |
| 2656 |
"import java.io.File;\n" + |
| 2657 |
"import java.io.FileInputStream;\n" + |
| 2658 |
"import java.io.FileNotFoundException;\n" + |
| 2659 |
"import java.io.InputStream;\n" + |
| 2660 |
"import java.io.BufferedInputStream;\n" + |
| 2661 |
"public class X {\n" + |
| 2662 |
" void foo(File location, String adviceFilePath) throws FileNotFoundException {\n" + |
| 2663 |
" InputStream stream = null;\n" + |
| 2664 |
" if (location.isDirectory()) {\n" + |
| 2665 |
" File adviceFile = new File(location, adviceFilePath);\n" + |
| 2666 |
" stream = new BufferedInputStream(new FileInputStream(adviceFile));\n" + |
| 2667 |
" }\n" + |
| 2668 |
" }\n" + |
| 2669 |
"}\n" |
| 2670 |
}, |
| 2671 |
"----------\n" + |
| 2672 |
"1. ERROR in X.java (at line 11)\n" + |
| 2673 |
" stream = new BufferedInputStream(new FileInputStream(adviceFile));\n" + |
| 2674 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2675 |
"Potential resource leak: \'stream\' may not be closed\n" + // message could be stronger, but the enclosing if blurs the picture |
| 2676 |
"----------\n", |
| 2677 |
null, |
| 2678 |
true, |
| 2679 |
options); |
| 2680 |
} |
| 2681 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2682 |
// a t-w-r wraps an existing resource |
| 2683 |
public void test061p() { |
| 2684 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; |
| 2685 |
Map options = getCompilerOptions(); |
| 2686 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2687 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2688 |
this.runConformTest( |
| 2689 |
new String[] { |
| 2690 |
"X.java", |
| 2691 |
"import java.io.PrintWriter;\n" + |
| 2692 |
"import java.io.BufferedWriter;\n" + |
| 2693 |
"import java.io.IOException;\n" + |
| 2694 |
"public class X {\n" + |
| 2695 |
" void foo() throws IOException {\n" + |
| 2696 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
| 2697 |
" try (BufferedWriter bw = new BufferedWriter(writer)) {\n" + |
| 2698 |
" bw.write(1);\n" + |
| 2699 |
" }\n" + |
| 2700 |
" }\n" + |
| 2701 |
"}\n" |
| 2702 |
}, |
| 2703 |
"", |
| 2704 |
null, |
| 2705 |
true, |
| 2706 |
null, |
| 2707 |
options, |
| 2708 |
null); |
| 2709 |
} |
| 2710 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2711 |
// a t-w-r potentially wraps an existing resource |
| 2712 |
// DISABLED, fails because we currently don't include t-w-r managed resources in the analysis |
| 2713 |
public void _test061q() { |
| 2714 |
if (this.complianceLevel < ClassFileConstants.JDK1_7) return; |
| 2715 |
Map options = getCompilerOptions(); |
| 2716 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2717 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2718 |
this.runNegativeTest( |
| 2719 |
new String[] { |
| 2720 |
"X.java", |
| 2721 |
"import java.io.PrintWriter;\n" + |
| 2722 |
"import java.io.BufferedWriter;\n" + |
| 2723 |
"import java.io.IOException;\n" + |
| 2724 |
"public class X {\n" + |
| 2725 |
" void foo(boolean b) throws IOException {\n" + |
| 2726 |
" PrintWriter writer = new PrintWriter(\"filename\");\n" + |
| 2727 |
" if (b)\n" + |
| 2728 |
" try (BufferedWriter bw = new BufferedWriter(writer)) {\n" + |
| 2729 |
" bw.write(1);\n" + |
| 2730 |
" }\n" + |
| 2731 |
" }\n" + |
| 2732 |
"}\n" |
| 2733 |
}, |
| 2734 |
"----------\n" + |
| 2735 |
"1. ERROR in X.java (at line 6)\n" + |
| 2736 |
" PrintWriter writer = new PrintWriter(\\\"filename\\\");\n" + |
| 2737 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2738 |
"Potential resource leak: \'writer\' may not be closed\n" + |
| 2739 |
"----------\n", |
| 2740 |
null, |
| 2741 |
true, |
| 2742 |
options); |
| 2743 |
} |
| 2744 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2745 |
// the inner from a wrapper is returned |
| 2746 |
public void test061r() { |
| 2747 |
Map options = getCompilerOptions(); |
| 2748 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2749 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2750 |
this.runConformTest( |
| 2751 |
new String[] { |
| 2752 |
"X.java", |
| 2753 |
"import java.io.FileInputStream;\n" + |
| 2754 |
"import java.io.File;\n" + |
| 2755 |
"import java.io.BufferedInputStream;\n" + |
| 2756 |
"import java.io.IOException;\n" + |
| 2757 |
"public class X {\n" + |
| 2758 |
" FileInputStream foo() throws IOException {\n" + |
| 2759 |
" File file = new File(\"somefil\");\n" + |
| 2760 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
| 2761 |
" BufferedInputStream bis = new BufferedInputStream(fileStream); \n" + |
| 2762 |
" return fileStream;\n" + |
| 2763 |
" }\n" + |
| 2764 |
"}\n" |
| 2765 |
}, |
| 2766 |
"", |
| 2767 |
null, |
| 2768 |
true, |
| 2769 |
null, |
| 2770 |
options, |
| 2771 |
null); |
| 2772 |
} |
| 2773 |
// Bug 358903 - Filter practically unimportant resource leak warnings |
| 2774 |
// a wrapper is forgotten, the inner is closed afterwards |
| 2775 |
public void test061s() { |
| 2776 |
Map options = getCompilerOptions(); |
| 2777 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2778 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2779 |
this.runConformTest( |
| 2780 |
new String[] { |
| 2781 |
"X.java", |
| 2782 |
"import java.io.FileInputStream;\n" + |
| 2783 |
"import java.io.File;\n" + |
| 2784 |
"import java.io.BufferedInputStream;\n" + |
| 2785 |
"import java.io.IOException;\n" + |
| 2786 |
"public class X {\n" + |
| 2787 |
" void foo() throws IOException {\n" + |
| 2788 |
" File file = new File(\"somefil\");\n" + |
| 2789 |
" FileInputStream fileStream = new FileInputStream(file);\n" + |
| 2790 |
" BufferedInputStream bis = new BufferedInputStream(fileStream);\n" + |
| 2791 |
" bis = null;\n" + |
| 2792 |
" fileStream.close();\n" + |
| 2793 |
" }\n" + |
| 2794 |
"}\n" |
| 2795 |
}, |
| 2796 |
"", |
| 2797 |
null, |
| 2798 |
true, |
| 2799 |
null, |
| 2800 |
options, |
| 2801 |
null); |
| 2802 |
} |
| 2803 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
| 2804 |
// a resource is never assigned |
| 2805 |
public void test062a() throws IOException { |
| 2806 |
Map options = getCompilerOptions(); |
| 2807 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2808 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2809 |
this.runNegativeTest( |
| 2810 |
new String[] { |
| 2811 |
"X.java", |
| 2812 |
"import java.io.File;\n" + |
| 2813 |
"import java.io.FileOutputStream;\n" + |
| 2814 |
"import java.io.IOException;\n" + |
| 2815 |
"public class X {\n" + |
| 2816 |
" void foo() throws IOException {\n" + |
| 2817 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).write(1);\n" + |
| 2818 |
" }\n" + |
| 2819 |
"}\n" |
| 2820 |
}, |
| 2821 |
"----------\n" + |
| 2822 |
"1. ERROR in X.java (at line 6)\n" + |
| 2823 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).write(1);\n" + |
| 2824 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2825 |
"Resource leak: \'<unassigned Closeable value>\' is never closed\n" + |
| 2826 |
"----------\n", |
| 2827 |
null, |
| 2828 |
true, |
| 2829 |
options); |
| 2830 |
} |
| 2831 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
| 2832 |
// a freshly allocated resource is immediately closed |
| 2833 |
public void test062b() throws IOException { |
| 2834 |
Map options = getCompilerOptions(); |
| 2835 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2836 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2837 |
this.runConformTest( |
| 2838 |
new String[] { |
| 2839 |
"X.java", |
| 2840 |
"import java.io.File;\n" + |
| 2841 |
"import java.io.FileOutputStream;\n" + |
| 2842 |
"import java.io.IOException;\n" + |
| 2843 |
"public class X {\n" + |
| 2844 |
" void foo() throws IOException {\n" + |
| 2845 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\")).close();\n" + |
| 2846 |
" }\n" + |
| 2847 |
"}\n" |
| 2848 |
}, |
| 2849 |
"", |
| 2850 |
null, |
| 2851 |
true, |
| 2852 |
null, |
| 2853 |
options, |
| 2854 |
null); |
| 2855 |
} |
| 2856 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
| 2857 |
// a resource is directly passed to another method |
| 2858 |
public void test062c() throws IOException { |
| 2859 |
Map options = getCompilerOptions(); |
| 2860 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2861 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2862 |
this.runNegativeTest( |
| 2863 |
new String[] { |
| 2864 |
"X.java", |
| 2865 |
"import java.io.File;\n" + |
| 2866 |
"import java.io.FileOutputStream;\n" + |
| 2867 |
"import java.io.IOException;\n" + |
| 2868 |
"public class X {\n" + |
| 2869 |
" void foo() throws IOException {\n" + |
| 2870 |
" writeIt(new FileOutputStream(new File(\"C:\\temp\\foo.txt\")));\n" + |
| 2871 |
" }\n" + |
| 2872 |
" void writeIt(FileOutputStream fos) throws IOException {\n" + |
| 2873 |
" fos.write(1);\n" + |
| 2874 |
" fos.close();\n" + |
| 2875 |
" }\n" + |
| 2876 |
"}\n" |
| 2877 |
}, |
| 2878 |
"----------\n" + |
| 2879 |
"1. ERROR in X.java (at line 6)\n" + |
| 2880 |
" writeIt(new FileOutputStream(new File(\"C:\\temp\\foo.txt\")));\n" + |
| 2881 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2882 |
"Potential resource leak: \'<unassigned Closeable value>\' may not be closed\n" + |
| 2883 |
"----------\n", |
| 2884 |
null, |
| 2885 |
true, |
| 2886 |
options); |
| 2887 |
} |
| 2888 |
// Bug 362331 - Resource leak not detected when closeable not assigned to variable |
| 2889 |
// a resource is not used |
| 2890 |
public void test062d() throws IOException { |
| 2891 |
Map options = getCompilerOptions(); |
| 2892 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2893 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2894 |
this.runNegativeTest( |
| 2895 |
new String[] { |
| 2896 |
"X.java", |
| 2897 |
"import java.io.File;\n" + |
| 2898 |
"import java.io.FileOutputStream;\n" + |
| 2899 |
"import java.io.IOException;\n" + |
| 2900 |
"public class X {\n" + |
| 2901 |
" void foo() throws IOException {\n" + |
| 2902 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\"));\n" + |
| 2903 |
" }\n" + |
| 2904 |
"}\n" |
| 2905 |
}, |
| 2906 |
"----------\n" + |
| 2907 |
"1. ERROR in X.java (at line 6)\n" + |
| 2908 |
" new FileOutputStream(new File(\"C:\\temp\\foo.txt\"));\n" + |
| 2909 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2910 |
"Resource leak: \'<unassigned Closeable value>\' is never closed\n" + |
| 2911 |
"----------\n", |
| 2912 |
null, |
| 2913 |
true, |
| 2914 |
options); |
| 2915 |
} |
| 2916 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
| 2917 |
// a wrapper is obtained from another method |
| 2918 |
public void test063a() throws IOException { |
| 2919 |
Map options = getCompilerOptions(); |
| 2920 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2921 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2922 |
this.runNegativeTest( |
| 2923 |
new String[] { |
| 2924 |
"X.java", |
| 2925 |
"import java.io.File;\n" + |
| 2926 |
"import java.io.FileInputStream;\n" + |
| 2927 |
"import java.io.BufferedInputStream;\n" + |
| 2928 |
"import java.io.IOException;\n" + |
| 2929 |
"public class X {\n" + |
| 2930 |
" void read(File file) throws IOException {\n" + |
| 2931 |
" FileInputStream stream = new FileInputStream(file);\n" + |
| 2932 |
" BufferedInputStream bis = new BufferedInputStream(stream); // never since reassigned\n" + |
| 2933 |
" FileInputStream stream2 = new FileInputStream(file); // unsure since passed to method\n" + |
| 2934 |
" bis = getReader(stream2); // unsure since obtained from method\n" + |
| 2935 |
" bis.available();\n" + |
| 2936 |
" }\n" + |
| 2937 |
" BufferedInputStream getReader(FileInputStream stream) throws IOException {\n" + |
| 2938 |
" return new BufferedInputStream(stream);\n" + |
| 2939 |
" }\n" + |
| 2940 |
"}\n" |
| 2941 |
}, |
| 2942 |
"----------\n" + |
| 2943 |
"1. ERROR in X.java (at line 7)\n" + |
| 2944 |
" FileInputStream stream = new FileInputStream(file);\n" + |
| 2945 |
" ^^^^^^\n" + |
| 2946 |
"Resource leak: \'stream\' is never closed\n" + |
| 2947 |
"----------\n" + |
| 2948 |
"2. ERROR in X.java (at line 9)\n" + |
| 2949 |
" FileInputStream stream2 = new FileInputStream(file); // unsure since passed to method\n" + |
| 2950 |
" ^^^^^^^\n" + |
| 2951 |
"Potential resource leak: \'stream2\' may not be closed\n" + |
| 2952 |
"----------\n" + |
| 2953 |
"3. ERROR in X.java (at line 10)\n" + |
| 2954 |
" bis = getReader(stream2); // unsure since obtained from method\n" + |
| 2955 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 2956 |
"Potential resource leak: \'bis\' may not be closed\n" + |
| 2957 |
"----------\n", |
| 2958 |
null, |
| 2959 |
true, |
| 2960 |
options); |
| 2961 |
} |
| 2962 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
| 2963 |
// a wrapper is obtained from a field read |
| 2964 |
public void test063b() throws IOException { |
| 2965 |
Map options = getCompilerOptions(); |
| 2966 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2967 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2968 |
this.runConformTest( |
| 2969 |
new String[] { |
| 2970 |
"X.java", |
| 2971 |
"import java.io.FileInputStream;\n" + |
| 2972 |
"import java.io.BufferedInputStream;\n" + |
| 2973 |
"import java.io.IOException;\n" + |
| 2974 |
"public class X {\n" + |
| 2975 |
" FileInputStream stream;\n" + |
| 2976 |
" void read() throws IOException {\n" + |
| 2977 |
" FileInputStream s = this.stream;\n" + |
| 2978 |
" BufferedInputStream bis = new BufferedInputStream(s); // don't complain since s is obtained from a field\n" + |
| 2979 |
" bis.available();\n" + |
| 2980 |
" }\n" + |
| 2981 |
"}\n" |
| 2982 |
}, |
| 2983 |
"", |
| 2984 |
null, |
| 2985 |
true, |
| 2986 |
null, |
| 2987 |
options, |
| 2988 |
null); |
| 2989 |
} |
| 2990 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
| 2991 |
// a wrapper is assigned to a field |
| 2992 |
public void test063c() throws IOException { |
| 2993 |
Map options = getCompilerOptions(); |
| 2994 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 2995 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 2996 |
this.runConformTest( |
| 2997 |
new String[] { |
| 2998 |
"X.java", |
| 2999 |
"import java.io.FileInputStream;\n" + |
| 3000 |
"import java.io.BufferedInputStream;\n" + |
| 3001 |
"import java.io.IOException;\n" + |
| 3002 |
"public class X {\n" + |
| 3003 |
" BufferedInputStream stream;\n" + |
| 3004 |
" void read() throws IOException {\n" + |
| 3005 |
" FileInputStream s = new FileInputStream(\"somefile\");\n" + |
| 3006 |
" BufferedInputStream bis = new BufferedInputStream(s);\n" + |
| 3007 |
" this.stream = bis;\n" + |
| 3008 |
" }\n" + |
| 3009 |
"}\n" |
| 3010 |
}, |
| 3011 |
"", |
| 3012 |
null, |
| 3013 |
true, |
| 3014 |
null, |
| 3015 |
options, |
| 3016 |
null); |
| 3017 |
} |
| 3018 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
| 3019 |
// a resource is obtained as a method argument and/or assigned with a cast |
| 3020 |
public void test063d() throws IOException { |
| 3021 |
Map options = getCompilerOptions(); |
| 3022 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3023 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3024 |
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.ERROR); |
| 3025 |
runTestsExpectingErrorsOnlyIn17( |
| 3026 |
new String[] { |
| 3027 |
"X.java", |
| 3028 |
"import java.io.FileInputStream;\n" + |
| 3029 |
"import java.io.BufferedInputStream;\n" + |
| 3030 |
"import java.io.InputStream;\n" + |
| 3031 |
"import java.io.IOException;\n" + |
| 3032 |
"public class X {\n" + |
| 3033 |
" void foo( InputStream input) throws IOException {\n" + |
| 3034 |
" FileInputStream input1 = (FileInputStream)input;\n" + |
| 3035 |
" System.out.println(input1.read());\n" + |
| 3036 |
" input.close();\n" + // don't propose t-w-r for argument |
| 3037 |
" }\n" + |
| 3038 |
" void foo() throws IOException {\n" + |
| 3039 |
" InputStream input = new FileInputStream(\"somefile\");\n" + |
| 3040 |
" FileInputStream input1 = (FileInputStream)input;\n" + |
| 3041 |
" System.out.println(input1.read());\n" + |
| 3042 |
" input.close();\n" + // do propose t-w-r, not from a method argument |
| 3043 |
" }\n" + |
| 3044 |
" void foo3( InputStream input, InputStream input2) throws IOException {\n" + |
| 3045 |
" FileInputStream input1 = (FileInputStream)input;\n" + // still don't claim because obtained from outside |
| 3046 |
" System.out.println(input1.read());\n" + |
| 3047 |
" BufferedInputStream bis = new BufferedInputStream(input2);\n" + |
| 3048 |
" System.out.println(bis.read());\n" + |
| 3049 |
" }\n" + |
| 3050 |
"}\n" |
| 3051 |
}, |
| 3052 |
"----------\n" + |
| 3053 |
"1. ERROR in X.java (at line 12)\n" + |
| 3054 |
" InputStream input = new FileInputStream(\"somefile\");\n" + |
| 3055 |
" ^^^^^\n" + |
| 3056 |
"Resource \'input\' should be managed by try-with-resource\n" + |
| 3057 |
"----------\n", |
| 3058 |
options); |
| 3059 |
} |
| 3060 |
// Bug 362332 - Only report potential leak when closeable not created in the local scope |
| 3061 |
// a resource is obtained from a field read, then re-assigned |
| 3062 |
public void test063e() { |
| 3063 |
Map options = getCompilerOptions(); |
| 3064 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3065 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3066 |
this.runConformTest( |
| 3067 |
new String[] { |
| 3068 |
"X.java", |
| 3069 |
"import java.io.FileInputStream;\n" + |
| 3070 |
"import java.io.IOException;\n" + |
| 3071 |
"public class X {\n" + |
| 3072 |
" FileInputStream input1;\n" + |
| 3073 |
" public void foo() throws IOException {\n" + |
| 3074 |
" FileInputStream input = input1;\n" + |
| 3075 |
" input = new FileInputStream(\"adfafd\");\n" + |
| 3076 |
" input.close();\n" + |
| 3077 |
" }\n" + |
| 3078 |
"}\n" |
| 3079 |
}, |
| 3080 |
"", |
| 3081 |
null, |
| 3082 |
true, |
| 3083 |
null, |
| 3084 |
options, |
| 3085 |
null); |
| 3086 |
} |
| 3087 |
// Bug 368709 - Endless loop in FakedTrackingVariable.markPassedToOutside |
| 3088 |
// original test case from jgit |
| 3089 |
public void testBug368709a() { |
| 3090 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) return; |
| 3091 |
Map options = getCompilerOptions(); |
| 3092 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3093 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3094 |
this.runNegativeTest( |
| 3095 |
new String[] { |
| 3096 |
"X.java", |
| 3097 |
"import java.io.*;\n" + |
| 3098 |
"import java.util.zip.*;\n" + |
| 3099 |
"public class X {\n" + |
| 3100 |
" Object db, pack;\n" + // mock |
| 3101 |
" int objectOffset, headerLength, type, size;\n" + |
| 3102 |
" public ObjectStream openStream() throws MissingObjectException, IOException {\n" + |
| 3103 |
" WindowCursor wc = new WindowCursor(db);\n" + |
| 3104 |
" InputStream in;\n" + |
| 3105 |
" try\n" + |
| 3106 |
" {\n" + |
| 3107 |
" in = new PackInputStream(pack, (objectOffset + headerLength), wc);\n" + |
| 3108 |
" }\n" + |
| 3109 |
" catch (IOException packGone)\n" + |
| 3110 |
" {\n" + |
| 3111 |
" return wc.open(getObjectId(), type).openStream();\n" + |
| 3112 |
" }\n" + |
| 3113 |
" in = new BufferedInputStream(new InflaterInputStream(in, wc.inflater(), 8192), 8192);\n" + |
| 3114 |
" return new ObjectStream.Filter(type, size, in);\n" + |
| 3115 |
" }\n" + |
| 3116 |
" String getObjectId() { return \"\"; }\n" + // mock |
| 3117 |
"}\n" + |
| 3118 |
// mock: |
| 3119 |
"class WindowCursor {\n" + |
| 3120 |
" WindowCursor(Object db) {}\n" + |
| 3121 |
" ObjectStream open(String id, int type) { return null; }\n" + |
| 3122 |
" Inflater inflater() { return null; }\n" + |
| 3123 |
"}\n" + |
| 3124 |
"class MissingObjectException extends Exception {\n" + |
| 3125 |
" public static final long serialVersionUID = 13L;\n" + |
| 3126 |
" MissingObjectException() { super();}\n" + |
| 3127 |
"}\n" + |
| 3128 |
"class PackInputStream extends InputStream {\n" + |
| 3129 |
" PackInputStream(Object pack, int offset, WindowCursor wc) throws IOException {}\n" + |
| 3130 |
" public int read() { return 0; }\n" + |
| 3131 |
"}\n" + |
| 3132 |
"class ObjectStream extends InputStream {\n" + |
| 3133 |
" static class Filter extends ObjectStream {\n" + |
| 3134 |
" Filter(int type, int size, InputStream in) { }\n" + |
| 3135 |
" }\n" + |
| 3136 |
" ObjectStream openStream() { return this; }\n" + |
| 3137 |
" public int read() { return 0; }\n" + |
| 3138 |
"}\n" |
| 3139 |
}, |
| 3140 |
"----------\n" + |
| 3141 |
"1. ERROR in X.java (at line 18)\n" + |
| 3142 |
" return new ObjectStream.Filter(type, size, in);\n" + |
| 3143 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 3144 |
"Potential resource leak: \'in\' may not be closed at this location\n" + |
| 3145 |
"----------\n", |
| 3146 |
null, |
| 3147 |
true, |
| 3148 |
options); |
| 3149 |
} |
| 3150 |
// Bug 368709 - Endless loop in FakedTrackingVariable.markPassedToOutside |
| 3151 |
// minimal test case: constructing an indirect self-wrapper |
| 3152 |
public void testBug368709b() { |
| 3153 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) return; |
| 3154 |
Map options = getCompilerOptions(); |
| 3155 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3156 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3157 |
this.runNegativeTest( |
| 3158 |
new String[] { |
| 3159 |
"X.java", |
| 3160 |
"import java.io.*;\n" + |
| 3161 |
"import java.util.zip.*;\n" + |
| 3162 |
"public class X {\n" + |
| 3163 |
" void doit() throws IOException {\n" + |
| 3164 |
" InputStream in = new FileInputStream(\"somefile\");\n" + |
| 3165 |
" in = new BufferedInputStream(new InflaterInputStream(in, inflater(), 8192), 8192);\n" + |
| 3166 |
" process(in);\n" + |
| 3167 |
" }\n" + |
| 3168 |
" Inflater inflater() { return null; }\n" + |
| 3169 |
" void process(InputStream is) { }\n" + |
| 3170 |
"}\n" |
| 3171 |
}, |
| 3172 |
"----------\n" + |
| 3173 |
"1. ERROR in X.java (at line 5)\n" + |
| 3174 |
" InputStream in = new FileInputStream(\"somefile\");\n" + |
| 3175 |
" ^^\n" + |
| 3176 |
"Potential resource leak: \'in\' may not be closed\n" + |
| 3177 |
"----------\n", |
| 3178 |
null, |
| 3179 |
true, |
| 3180 |
options); |
| 3181 |
} |
| 3182 |
|
| 3183 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3184 |
// example from comment 3 |
| 3185 |
public void test064() { |
| 3186 |
Map options = getCompilerOptions(); |
| 3187 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3188 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3189 |
this.runNegativeTest(new String[] { |
| 3190 |
"Test064.java", |
| 3191 |
"import java.io.*;\n" + |
| 3192 |
"public class Test064 {\n" + |
| 3193 |
" void foo(File outfile) {\n" + |
| 3194 |
" OutputStream out= System.out;\n" + |
| 3195 |
" if (outfile != null) {\n" + |
| 3196 |
" try {\n" + |
| 3197 |
" out = new FileOutputStream(outfile);\n" + |
| 3198 |
" } catch (java.io.IOException e) {\n" + |
| 3199 |
" throw new RuntimeException(e);\n" + |
| 3200 |
" }\n" + |
| 3201 |
" }\n" + |
| 3202 |
" setOutput(out);\n" + |
| 3203 |
" }\n" + |
| 3204 |
" private void setOutput(OutputStream out) { }\n" + |
| 3205 |
"}\n" |
| 3206 |
}, |
| 3207 |
"----------\n" + |
| 3208 |
"1. ERROR in Test064.java (at line 7)\n" + |
| 3209 |
" out = new FileOutputStream(outfile);\n" + |
| 3210 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 3211 |
"Potential resource leak: \'out\' may not be closed\n" + |
| 3212 |
"----------\n", |
| 3213 |
null, |
| 3214 |
true, |
| 3215 |
options); |
| 3216 |
} |
| 3217 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3218 |
// example from comment 10 |
| 3219 |
// disabled, because basic null-analysis machinery doesn't support this pattern |
| 3220 |
// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise |
| 3221 |
public void _test065() { |
| 3222 |
Map options = getCompilerOptions(); |
| 3223 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3224 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3225 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3226 |
this.runConformTest(new String[] { |
| 3227 |
"Test065.java", |
| 3228 |
"import java.io.*;\n" + |
| 3229 |
"class MyException extends Exception{}\n" + |
| 3230 |
"public class Test065 {\n" + |
| 3231 |
" void foo(String fileName) throws IOException, MyException {\n" + |
| 3232 |
" FileReader fileRead = new FileReader(fileName);\n" + |
| 3233 |
" BufferedReader bufRead = new BufferedReader(fileRead);\n" + |
| 3234 |
" LineNumberReader lineReader = new LineNumberReader(bufRead);\n" + |
| 3235 |
" try {\n" + |
| 3236 |
" while (lineReader.readLine() != null) {\n" + |
| 3237 |
" bufRead.close();\n" + |
| 3238 |
" callSome(); // only this can throw MyException\n" + |
| 3239 |
" }\n" + |
| 3240 |
" } catch (MyException e) {\n" + |
| 3241 |
" throw e; // Pot. leak reported here\n" + |
| 3242 |
" }\n" + |
| 3243 |
" bufRead.close(); \n" + |
| 3244 |
" }\n" + |
| 3245 |
" private void callSome() throws MyException\n" + |
| 3246 |
" {\n" + |
| 3247 |
" \n" + |
| 3248 |
" }\n" + |
| 3249 |
"}\n" |
| 3250 |
}, |
| 3251 |
"", |
| 3252 |
null, |
| 3253 |
true, |
| 3254 |
null, |
| 3255 |
options, |
| 3256 |
null); |
| 3257 |
} |
| 3258 |
|
| 3259 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3260 |
// example from comment 11 |
| 3261 |
public void test066() { |
| 3262 |
Map options = getCompilerOptions(); |
| 3263 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3264 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3265 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3266 |
this.runNegativeTest(new String[] { |
| 3267 |
"Test066.java", |
| 3268 |
"import java.io.*;\n" + |
| 3269 |
"class MyException extends Exception{}\n" + |
| 3270 |
"public class Test066 {\n" + |
| 3271 |
" void countFileLines(String fileName) throws IOException {\n" + |
| 3272 |
" FileReader fileRead = new FileReader(fileName);\n" + |
| 3273 |
" BufferedReader bufRead = new BufferedReader(fileRead);\n" + |
| 3274 |
" LineNumberReader lineReader = new LineNumberReader(bufRead);\n" + |
| 3275 |
" while (lineReader.readLine() != null) {\n" + |
| 3276 |
" if (lineReader.markSupported())\n" + |
| 3277 |
" throw new IOException();\n" + |
| 3278 |
" bufRead.close();\n" + |
| 3279 |
" }\n" + |
| 3280 |
" bufRead.close();\n" + |
| 3281 |
" }\n" + |
| 3282 |
"}\n" |
| 3283 |
}, |
| 3284 |
"----------\n" + |
| 3285 |
"1. ERROR in Test066.java (at line 10)\n" + |
| 3286 |
" throw new IOException();\n" + |
| 3287 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 3288 |
"Potential resource leak: \'lineReader\' may not be closed at this location\n" + |
| 3289 |
"----------\n", |
| 3290 |
null, |
| 3291 |
true, |
| 3292 |
options); |
| 3293 |
} |
| 3294 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3295 |
// example from comment 11 - variant with closing top-level resource |
| 3296 |
public void test066b() { |
| 3297 |
Map options = getCompilerOptions(); |
| 3298 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3299 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3300 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3301 |
this.runNegativeTest(new String[] { |
| 3302 |
"Test066.java", |
| 3303 |
"import java.io.*;\n" + |
| 3304 |
"class MyException extends Exception{}\n" + |
| 3305 |
"public class Test066 {\n" + |
| 3306 |
" void countFileLines(String fileName) throws IOException {\n" + |
| 3307 |
" FileReader fileRead = new FileReader(fileName);\n" + |
| 3308 |
" BufferedReader bufRead = new BufferedReader(fileRead);\n" + |
| 3309 |
" LineNumberReader lineReader = new LineNumberReader(bufRead);\n" + |
| 3310 |
" while (lineReader.readLine() != null) {\n" + |
| 3311 |
" if (lineReader.markSupported())\n" + |
| 3312 |
" throw new IOException();\n" + |
| 3313 |
" lineReader.close();\n" + |
| 3314 |
" }\n" + |
| 3315 |
" lineReader.close();\n" + |
| 3316 |
" }\n" + |
| 3317 |
"}\n" |
| 3318 |
}, |
| 3319 |
"----------\n" + |
| 3320 |
"1. ERROR in Test066.java (at line 10)\n" + |
| 3321 |
" throw new IOException();\n" + |
| 3322 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
| 3323 |
"Potential resource leak: \'lineReader\' may not be closed at this location\n" + |
| 3324 |
"----------\n", |
| 3325 |
null, |
| 3326 |
true, |
| 3327 |
options); |
| 3328 |
} |
| 3329 |
|
| 3330 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3331 |
// example from comment 12 |
| 3332 |
// disabled because null info after try-catch is too weak, |
| 3333 |
// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise |
| 3334 |
public void _test067() { |
| 3335 |
Map options = getCompilerOptions(); |
| 3336 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3337 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3338 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3339 |
this.runConformTest(new String[] { |
| 3340 |
"Test067.java", |
| 3341 |
"import java.io.*;\n" + |
| 3342 |
"public class Test067 {\n" + |
| 3343 |
" public void comment12() throws IOException {\n" + |
| 3344 |
" LineNumberReader o = null;\n" + |
| 3345 |
" try {\n" + |
| 3346 |
" o = new LineNumberReader(null); \n" + |
| 3347 |
" } catch (NumberFormatException e) { \n" + |
| 3348 |
" }\n" + |
| 3349 |
" }\n" + |
| 3350 |
"}\n" |
| 3351 |
}, |
| 3352 |
"", |
| 3353 |
null, |
| 3354 |
true, |
| 3355 |
null, |
| 3356 |
options, |
| 3357 |
null); |
| 3358 |
} |
| 3359 |
|
| 3360 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3361 |
// example from comment 12 |
| 3362 |
// disabled because null info after try-catch is too weak, |
| 3363 |
// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise |
| 3364 |
public void _test067b() { |
| 3365 |
Map options = getCompilerOptions(); |
| 3366 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3367 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3368 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3369 |
this.runConformTest(new String[] { |
| 3370 |
"Test067.java", |
| 3371 |
"import java.io.*;\n" + |
| 3372 |
"public class Test067 {\n" + |
| 3373 |
" public void comment12b() throws IOException {\n" + |
| 3374 |
" LineNumberReader o = new LineNumberReader(null);\n" + |
| 3375 |
" try {\n" + |
| 3376 |
" o.close();\n" + |
| 3377 |
" } catch (NumberFormatException e) {\n" + |
| 3378 |
" }\n" + |
| 3379 |
" }\n" + |
| 3380 |
"}\n" |
| 3381 |
}, |
| 3382 |
"", |
| 3383 |
null, |
| 3384 |
true, |
| 3385 |
null, |
| 3386 |
options, |
| 3387 |
null); |
| 3388 |
} |
| 3389 |
|
| 3390 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3391 |
// example from comment 13 |
| 3392 |
public void test068() { |
| 3393 |
Map options = getCompilerOptions(); |
| 3394 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3395 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3396 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3397 |
this.runConformTest(new String[] { |
| 3398 |
"Test068.java", |
| 3399 |
"import java.io.*;\n" + |
| 3400 |
"public class Test068 {\n" + |
| 3401 |
" class ProcessingStep extends OutputStream {\n" + |
| 3402 |
" public void write(int b) throws IOException {}\n" + |
| 3403 |
" public OutputStream getDestination() { return null; }\n" + |
| 3404 |
" }\n" + |
| 3405 |
" class ArtifactOutputStream extends OutputStream {\n" + |
| 3406 |
" public void write(int b) throws IOException {}\n" + |
| 3407 |
" }" + |
| 3408 |
" ArtifactOutputStream comment13(OutputStream stream) {\n" + |
| 3409 |
" OutputStream current = stream;\n" + |
| 3410 |
" while (current instanceof ProcessingStep)\n" + |
| 3411 |
" current = ((ProcessingStep) current).getDestination();\n" + // we previously saw a bogus warning here. |
| 3412 |
" if (current instanceof ArtifactOutputStream)\n" + |
| 3413 |
" return (ArtifactOutputStream) current;\n" + |
| 3414 |
" return null;\n" + |
| 3415 |
" }\n" + |
| 3416 |
"}\n" |
| 3417 |
}, |
| 3418 |
"", |
| 3419 |
null, |
| 3420 |
true, |
| 3421 |
null, |
| 3422 |
options, |
| 3423 |
null); |
| 3424 |
} |
| 3425 |
|
| 3426 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3427 |
// example from comment 16 |
| 3428 |
public void test069() { |
| 3429 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // generics used |
| 3430 |
Map options = getCompilerOptions(); |
| 3431 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3432 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3433 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3434 |
this.runConformTest(new String[] { |
| 3435 |
"Test069.java", |
| 3436 |
"import java.io.*;\n" + |
| 3437 |
"import java.util.Collection;\n" + |
| 3438 |
"public class Test069 {\n" + |
| 3439 |
" class Profile {}\n" + |
| 3440 |
" class CoreException extends Exception {}\n" + |
| 3441 |
" void writeProfilesToStream(Collection<Profile> p, OutputStream s, String enc) {}\n" + |
| 3442 |
" CoreException createException(IOException ioex, String message) { return new CoreException(); }\n" + |
| 3443 |
" public void comment16(Collection<Profile> profiles, File file, String encoding) throws CoreException {\n" + |
| 3444 |
" final OutputStream stream;\n" + |
| 3445 |
" try {\n" + |
| 3446 |
" stream= new FileOutputStream(file);\n" + |
| 3447 |
" try {\n" + |
| 3448 |
" writeProfilesToStream(profiles, stream, encoding);\n" + |
| 3449 |
" } finally {\n" + |
| 3450 |
" try { stream.close(); } catch (IOException e) { /* ignore */ }\n" + |
| 3451 |
" }\n" + |
| 3452 |
" } catch (IOException e) {\n" + |
| 3453 |
" throw createException(e, \"message\"); // should not shout here\n" + |
| 3454 |
" }\n" + |
| 3455 |
" }\n" + |
| 3456 |
"}\n" |
| 3457 |
}, |
| 3458 |
"", |
| 3459 |
null, |
| 3460 |
true, |
| 3461 |
null, |
| 3462 |
options, |
| 3463 |
null); |
| 3464 |
} |
| 3465 |
|
| 3466 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3467 |
// referenced in array initializer |
| 3468 |
public void test070() { |
| 3469 |
Map options = getCompilerOptions(); |
| 3470 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3471 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3472 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3473 |
this.runNegativeTest(new String[] { |
| 3474 |
"Test070.java", |
| 3475 |
"import java.io.*;\n" + |
| 3476 |
"public class Test070 {\n" + |
| 3477 |
" void storeInArray(String fileName) throws IOException {\n" + |
| 3478 |
" FileReader fileRead = new FileReader(fileName);\n" + |
| 3479 |
" closeThemAll(new FileReader[] { fileRead });\n" + |
| 3480 |
" }\n" + |
| 3481 |
" void closeThemAll(FileReader[] readers) { }\n" + |
| 3482 |
"}\n" |
| 3483 |
}, |
| 3484 |
"----------\n" + |
| 3485 |
"1. ERROR in Test070.java (at line 4)\n" + |
| 3486 |
" FileReader fileRead = new FileReader(fileName);\n" + |
| 3487 |
" ^^^^^^^^\n" + |
| 3488 |
"Potential resource leak: \'fileRead\' may not be closed\n" + |
| 3489 |
"----------\n", |
| 3490 |
null, |
| 3491 |
true, |
| 3492 |
options); |
| 3493 |
} |
| 3494 |
|
| 3495 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3496 |
// referenced in array initializer |
| 3497 |
public void test071() { |
| 3498 |
Map options = getCompilerOptions(); |
| 3499 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3500 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3501 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3502 |
this.runNegativeTest(new String[] { |
| 3503 |
"Test071.java", |
| 3504 |
"import java.io.*;\n" + |
| 3505 |
"public class Test071 {\n" + |
| 3506 |
" class ReaderHolder {\n" + |
| 3507 |
" FileReader reader;\n" + |
| 3508 |
" }\n" + |
| 3509 |
" private FileReader getReader() {\n" + |
| 3510 |
" return null;\n" + |
| 3511 |
" }\n" + |
| 3512 |
" void invokeCompiler(ReaderHolder readerHolder, boolean flag) throws FileNotFoundException {\n" + |
| 3513 |
" FileReader reader = readerHolder.reader;\n" + |
| 3514 |
" if (reader == null)\n" + |
| 3515 |
" reader = getReader();\n" + |
| 3516 |
" try {\n" + |
| 3517 |
" return;\n" + |
| 3518 |
" } finally {\n" + |
| 3519 |
" try {\n" + |
| 3520 |
" if (flag)\n" + |
| 3521 |
" reader.close();\n" + |
| 3522 |
" } catch (IOException e) {\n" + |
| 3523 |
" // nop\n" + |
| 3524 |
" }\n" + |
| 3525 |
" }\n" + |
| 3526 |
" }\n" + |
| 3527 |
"}\n" |
| 3528 |
}, |
| 3529 |
"----------\n" + |
| 3530 |
"1. ERROR in Test071.java (at line 14)\n" + |
| 3531 |
" return;\n" + |
| 3532 |
" ^^^^^^^\n" + |
| 3533 |
"Potential resource leak: \'reader\' may not be closed at this location\n" + |
| 3534 |
"----------\n", |
| 3535 |
null, |
| 3536 |
true, |
| 3537 |
options); |
| 3538 |
} |
| 3539 |
|
| 3540 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3541 |
// referenced in array initializer |
| 3542 |
// disabled because it would require correlation analysis between the tracking variable and its original |
| 3543 |
// need to pass to downstream: either (nonnull & open) or (null) |
| 3544 |
public void _test071b() { |
| 3545 |
Map options = getCompilerOptions(); |
| 3546 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3547 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3548 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3549 |
this.runNegativeTest(new String[] { |
| 3550 |
"Test071b.java", |
| 3551 |
"import java.io.*;\n" + |
| 3552 |
"public class Test071b {\n" + |
| 3553 |
" private FileReader getReader() {\n" + |
| 3554 |
" return null;\n" + |
| 3555 |
" }\n" + |
| 3556 |
" void invokeCompiler(boolean flag) throws FileNotFoundException {\n" + |
| 3557 |
" FileReader reader = null;\n" + |
| 3558 |
" if (flag)\n" + |
| 3559 |
" reader = new FileReader(\"file\");\n" + |
| 3560 |
" if (reader == null)\n" + |
| 3561 |
" reader = getReader();\n" + |
| 3562 |
" try {\n" + |
| 3563 |
" return;\n" + |
| 3564 |
" } finally {\n" + |
| 3565 |
" try {\n" + |
| 3566 |
" if (flag)\n" + |
| 3567 |
" reader.close();\n" + |
| 3568 |
" } catch (IOException e) {\n" + |
| 3569 |
" // nop\n" + |
| 3570 |
" }\n" + |
| 3571 |
" }\n" + |
| 3572 |
" }\n" + |
| 3573 |
"}\n" |
| 3574 |
}, |
| 3575 |
"----------\n" + |
| 3576 |
"1. ERROR in Test071b.java (at line 13)\n" + |
| 3577 |
" return;\n" + |
| 3578 |
" ^^^^^^^\n" + |
| 3579 |
"Potential resource leak: \'reader\' may not be closed at this location\n" + |
| 3580 |
"----------\n", |
| 3581 |
null, |
| 3582 |
true, |
| 3583 |
options); |
| 3584 |
} |
| 3585 |
|
| 3586 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3587 |
// throw inside loop inside try - while closed in finally |
| 3588 |
public void test072() { |
| 3589 |
Map options = getCompilerOptions(); |
| 3590 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3591 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3592 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3593 |
this.runConformTest(new String[] { |
| 3594 |
"Test072.java", |
| 3595 |
"import java.io.*;\n" + |
| 3596 |
"public class Test072 {\n" + |
| 3597 |
" void readState(File file) {\n" + |
| 3598 |
" DataInputStream in = null;\n" + |
| 3599 |
" try {\n" + |
| 3600 |
" in= new DataInputStream(new BufferedInputStream(new FileInputStream(file)));\n" + |
| 3601 |
" int sizeOfFlags = in.readInt();\n" + |
| 3602 |
" for (int i = 0; i < sizeOfFlags; ++i) {\n" + |
| 3603 |
" String childPath = in.readUTF();\n" + |
| 3604 |
" if (childPath.length() == 0)\n" + |
| 3605 |
" throw new IOException();\n" + |
| 3606 |
" }\n" + |
| 3607 |
" }\n" + |
| 3608 |
" catch (IOException ioe) { /* nop */ }\n" + |
| 3609 |
" finally {\n" + |
| 3610 |
" if (in != null) {\n" + |
| 3611 |
" try {in.close();} catch (IOException ioe) {}\n" + |
| 3612 |
" }\n" + |
| 3613 |
" }\n" + |
| 3614 |
" }\n" + |
| 3615 |
"}\n" |
| 3616 |
}, |
| 3617 |
"", |
| 3618 |
null, |
| 3619 |
true, |
| 3620 |
null, |
| 3621 |
options, |
| 3622 |
null); |
| 3623 |
} |
| 3624 |
|
| 3625 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3626 |
// unspecific parameter is casted into a resource, yet need to mark as OWNED_BY_OUTSIDE |
| 3627 |
public void test073() { |
| 3628 |
Map options = getCompilerOptions(); |
| 3629 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3630 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3631 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3632 |
this.runConformTest(new String[] { |
| 3633 |
"Test073.java", |
| 3634 |
"import java.io.*;\n" + |
| 3635 |
"public class Test073 {\n" + |
| 3636 |
" String getEncoding(Object reader) {\n" + |
| 3637 |
" if (reader instanceof FileReader) {\n" + |
| 3638 |
" final FileReader fr = (FileReader) reader;\n" + |
| 3639 |
" return fr.getEncoding();\n" + |
| 3640 |
" }\n" + |
| 3641 |
" return null;\n" + |
| 3642 |
" }\n" + |
| 3643 |
"}\n" |
| 3644 |
}, |
| 3645 |
"", |
| 3646 |
null, |
| 3647 |
true, |
| 3648 |
null, |
| 3649 |
options, |
| 3650 |
null); |
| 3651 |
} |
| 3652 |
|
| 3653 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
| 3654 |
// status after nested try-finally |
| 3655 |
public void test074() { |
| 3656 |
Map options = getCompilerOptions(); |
| 3657 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
| 3658 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
| 3659 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
| 3660 |
this.runNegativeTest(new String[] { |
| 3661 |
"Test074.java", |
| 3662 |
"import java.io.*;\n" + |
| 3663 |
"public class Test074 {\n" + |
| 3664 |
" void foo() throws FileNotFoundException {\n" + |
| 3665 |
" FileOutputStream out = null;\n" + |
| 3666 |
" try {\n" + |
| 3667 |
" out = new FileOutputStream(\"outfile\");\n" + |
| 3668 |
" } finally {\n" + |
| 3669 |
" try {\n" + |
| 3670 |
" out.flush();\n" + |
| 3671 |
" out.close();\n" + |
| 3672 |
" } catch (IOException e) {\n" + |
| 3673 |
" e.printStackTrace();\n" + |
| 3674 |
" }\n" + |
| 3675 |
" out = null;\n" + // unclosed if exception occurred on flush() |
| 3676 |
" }\n" + |
| 3677 |
" }\n" + |
| 3678 |
"}\n" |
| 3679 |
}, |
| 3680 |
"----------\n" + |
| 3681 |
"1. ERROR in Test074.java (at line 14)\n" + |
| 3682 |
" out = null;\n" + |
| 3683 |
" ^^^^^^^^^^\n" + |
| 3684 |
"Potential resource leak: \'out\' may not be closed at this location\n" + |
| 3685 |
"----------\n", |
| 3686 |
null, |
| 3687 |
true, |
| 3688 |
options); |
| 3689 |
} |
| 3690 |
// Bug 370639 - [compiler][resource] restore the default for resource leak warnings |
| 3691 |
// check that the default is warning |
| 3692 |
public void test075() { |
| 3693 |
this.runNegativeTest( |
| 3694 |
new String[] { |
| 3695 |
"X.java", |
| 3696 |
"import java.io.File;\n" + |
| 3697 |
"import java.io.FileReader;\n" + |
| 3698 |
"import java.io.IOException;\n" + |
| 3699 |
"public class X {\n" + |
| 3700 |
" void foo() throws IOException {\n" + |
| 3701 |
" File file = new File(\"somefile\");\n" + |
| 3702 |
" FileReader fileReader = new FileReader(file);\n" + |
| 3703 |
" }\n" + |
| 3704 |
"}\n" |
| 3705 |
}, |
| 3706 |
"----------\n" + |
| 3707 |
"1. WARNING in X.java (at line 7)\n" + |
| 3708 |
" FileReader fileReader = new FileReader(file);\n" + |
| 3709 |
" ^^^^^^^^^^\n" + |
| 3710 |
"Resource leak: 'fileReader' is never closed\n" + |
| 3711 |
"----------\n"); |
| 3712 |
} |
| 3713 |
} |