Lines 26-32
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
Link Here
|
26 |
public class ResourceLeakTests extends AbstractRegressionTest { |
26 |
public class ResourceLeakTests extends AbstractRegressionTest { |
27 |
|
27 |
|
28 |
static { |
28 |
static { |
29 |
// TESTS_NAMES = new String[] { "testBug368709"}; |
29 |
// TESTS_NAMES = new String[] { "test074"}; |
30 |
// TESTS_NUMBERS = new int[] { 50 }; |
30 |
// TESTS_NUMBERS = new int[] { 50 }; |
31 |
// TESTS_RANGE = new int[] { 11, -1 }; |
31 |
// TESTS_RANGE = new int[] { 11, -1 }; |
32 |
} |
32 |
} |
Lines 1489-1495
public void test056y() {
Link Here
|
1489 |
" final FileReader reader23 = new FileReader(\"file\");\n" + |
1489 |
" final FileReader reader23 = new FileReader(\"file\");\n" + |
1490 |
" provider = new ResourceProvider() {\n" + |
1490 |
" provider = new ResourceProvider() {\n" + |
1491 |
" public FileReader provide() {\n" + |
1491 |
" public FileReader provide() {\n" + |
1492 |
" return reader23;\n" + |
1492 |
" return reader23;\n" + // responsibility now lies at the caller of this method |
1493 |
" }\n" + |
1493 |
" }\n" + |
1494 |
" };\n" + |
1494 |
" };\n" + |
1495 |
" }\n" + |
1495 |
" }\n" + |
Lines 1500-1510
public void test056y() {
Link Here
|
1500 |
" final FileReader reader31 = new FileReader(\"file\");\n" + |
1500 |
" final FileReader reader31 = new FileReader(\"file\");\n" + |
1501 |
" ^^^^^^^^\n" + |
1501 |
" ^^^^^^^^\n" + |
1502 |
"Potential resource leak: 'reader31' may not be closed\n" + |
1502 |
"Potential resource leak: 'reader31' may not be closed\n" + |
1503 |
"----------\n" + |
|
|
1504 |
"2. WARNING in X.java (at line 17)\n" + |
1505 |
" final FileReader reader23 = new FileReader(\"file\");\n" + |
1506 |
" ^^^^^^^^\n" + |
1507 |
"Potential resource leak: 'reader23' may not be closed\n" + |
1508 |
"----------\n", |
1503 |
"----------\n", |
1509 |
null, |
1504 |
null, |
1510 |
true, |
1505 |
true, |
Lines 3143-3154
public void testBug368709a() {
Link Here
|
3143 |
"}\n" |
3138 |
"}\n" |
3144 |
}, |
3139 |
}, |
3145 |
"----------\n" + |
3140 |
"----------\n" + |
3146 |
"1. ERROR in X.java (at line 15)\n" + |
3141 |
"1. ERROR in X.java (at line 18)\n" + |
3147 |
" return wc.open(getObjectId(), type).openStream();\n" + |
|
|
3148 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3149 |
"Resource leak: \'in\' is not closed at this location\n" + |
3150 |
"----------\n" + |
3151 |
"2. ERROR in X.java (at line 18)\n" + |
3152 |
" return new ObjectStream.Filter(type, size, in);\n" + |
3142 |
" return new ObjectStream.Filter(type, size, in);\n" + |
3153 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3143 |
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3154 |
"Potential resource leak: \'in\' may not be closed at this location\n" + |
3144 |
"Potential resource leak: \'in\' may not be closed at this location\n" + |
Lines 3189-3192
public void testBug368709b() {
Link Here
|
3189 |
true, |
3179 |
true, |
3190 |
options); |
3180 |
options); |
3191 |
} |
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.runConformTest(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 |
"", |
3208 |
null, |
3209 |
true, |
3210 |
null, |
3211 |
options, |
3212 |
null); |
3213 |
} |
3214 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3215 |
// example from comment 10 |
3216 |
// disabled, because basic null-analysis machinery doesn't support this pattern |
3217 |
// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise |
3218 |
public void _test065() { |
3219 |
Map options = getCompilerOptions(); |
3220 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3221 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3222 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3223 |
this.runConformTest(new String[] { |
3224 |
"Test065.java", |
3225 |
"import java.io.*;\n" + |
3226 |
"class MyException extends Exception{}\n" + |
3227 |
"public class Test065 {\n" + |
3228 |
" void foo(String fileName) throws IOException, MyException {\n" + |
3229 |
" FileReader fileRead = new FileReader(fileName);\n" + |
3230 |
" BufferedReader bufRead = new BufferedReader(fileRead);\n" + |
3231 |
" LineNumberReader lineReader = new LineNumberReader(bufRead);\n" + |
3232 |
" try {\n" + |
3233 |
" while (lineReader.readLine() != null) {\n" + |
3234 |
" bufRead.close();\n" + |
3235 |
" callSome(); // only this can throw MyException\n" + |
3236 |
" }\n" + |
3237 |
" } catch (MyException e) {\n" + |
3238 |
" throw e; // Pot. leak reported here\n" + |
3239 |
" }\n" + |
3240 |
" bufRead.close(); \n" + |
3241 |
" }\n" + |
3242 |
" private void callSome() throws MyException\n" + |
3243 |
" {\n" + |
3244 |
" \n" + |
3245 |
" }\n" + |
3246 |
"}\n" |
3247 |
}, |
3248 |
"", |
3249 |
null, |
3250 |
true, |
3251 |
null, |
3252 |
options, |
3253 |
null); |
3254 |
} |
3255 |
|
3256 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3257 |
// example from comment 11 |
3258 |
public void test066() { |
3259 |
Map options = getCompilerOptions(); |
3260 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3261 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3262 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3263 |
this.runNegativeTest(new String[] { |
3264 |
"Test066.java", |
3265 |
"import java.io.*;\n" + |
3266 |
"class MyException extends Exception{}\n" + |
3267 |
"public class Test066 {\n" + |
3268 |
" void countFileLines(String fileName) throws IOException {\n" + |
3269 |
" FileReader fileRead = new FileReader(fileName);\n" + |
3270 |
" BufferedReader bufRead = new BufferedReader(fileRead);\n" + |
3271 |
" LineNumberReader lineReader = new LineNumberReader(bufRead);\n" + |
3272 |
" while (lineReader.readLine() != null) {\n" + |
3273 |
" if (lineReader.markSupported())\n" + |
3274 |
" throw new IOException();\n" + |
3275 |
" bufRead.close();\n" + |
3276 |
" }\n" + |
3277 |
" bufRead.close();\n" + |
3278 |
" }\n" + |
3279 |
"}\n" |
3280 |
}, |
3281 |
"----------\n" + |
3282 |
"1. ERROR in Test066.java (at line 10)\n" + |
3283 |
" throw new IOException();\n" + |
3284 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3285 |
"Resource leak: \'lineReader\' is not closed at this location\n" + |
3286 |
"----------\n", |
3287 |
null, |
3288 |
true, |
3289 |
options); |
3290 |
} |
3291 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3292 |
// example from comment 11 - variant with closing top-level resource |
3293 |
public void test066b() { |
3294 |
Map options = getCompilerOptions(); |
3295 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3296 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3297 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3298 |
this.runNegativeTest(new String[] { |
3299 |
"Test066.java", |
3300 |
"import java.io.*;\n" + |
3301 |
"class MyException extends Exception{}\n" + |
3302 |
"public class Test066 {\n" + |
3303 |
" void countFileLines(String fileName) throws IOException {\n" + |
3304 |
" FileReader fileRead = new FileReader(fileName);\n" + |
3305 |
" BufferedReader bufRead = new BufferedReader(fileRead);\n" + |
3306 |
" LineNumberReader lineReader = new LineNumberReader(bufRead);\n" + |
3307 |
" while (lineReader.readLine() != null) {\n" + |
3308 |
" if (lineReader.markSupported())\n" + |
3309 |
" throw new IOException();\n" + |
3310 |
" lineReader.close();\n" + |
3311 |
" }\n" + |
3312 |
" lineReader.close();\n" + |
3313 |
" }\n" + |
3314 |
"}\n" |
3315 |
}, |
3316 |
"----------\n" + |
3317 |
"1. ERROR in Test066.java (at line 10)\n" + |
3318 |
" throw new IOException();\n" + |
3319 |
" ^^^^^^^^^^^^^^^^^^^^^^^^\n" + |
3320 |
"Potential resource leak: \'lineReader\' may not be closed at this location\n" + |
3321 |
"----------\n", |
3322 |
null, |
3323 |
true, |
3324 |
options); |
3325 |
} |
3326 |
|
3327 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3328 |
// example from comment 12 |
3329 |
// disabled because null info after try-catch is too weak, |
3330 |
// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise |
3331 |
public void _test067() { |
3332 |
Map options = getCompilerOptions(); |
3333 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3334 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3335 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3336 |
this.runConformTest(new String[] { |
3337 |
"Test067.java", |
3338 |
"import java.io.*;\n" + |
3339 |
"public class Test067 {\n" + |
3340 |
" public void comment12() throws IOException {\n" + |
3341 |
" LineNumberReader o = null;\n" + |
3342 |
" try {\n" + |
3343 |
" o = new LineNumberReader(null); \n" + |
3344 |
" } catch (NumberFormatException e) { \n" + |
3345 |
" }\n" + |
3346 |
" }\n" + |
3347 |
"}\n" |
3348 |
}, |
3349 |
"", |
3350 |
null, |
3351 |
true, |
3352 |
null, |
3353 |
options, |
3354 |
null); |
3355 |
} |
3356 |
|
3357 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3358 |
// example from comment 12 |
3359 |
// disabled because null info after try-catch is too weak, |
3360 |
// see also Bug 370424 - [compiler][null] throw-catch analysis for null flow could be more precise |
3361 |
public void _test067b() { |
3362 |
Map options = getCompilerOptions(); |
3363 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3364 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3365 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3366 |
this.runConformTest(new String[] { |
3367 |
"Test067.java", |
3368 |
"import java.io.*;\n" + |
3369 |
"public class Test067 {\n" + |
3370 |
" public void comment12b() throws IOException {\n" + |
3371 |
" LineNumberReader o = new LineNumberReader(null);\n" + |
3372 |
" try {\n" + |
3373 |
" o.close();\n" + |
3374 |
" } catch (NumberFormatException e) {\n" + |
3375 |
" }\n" + |
3376 |
" }\n" + |
3377 |
"}\n" |
3378 |
}, |
3379 |
"", |
3380 |
null, |
3381 |
true, |
3382 |
null, |
3383 |
options, |
3384 |
null); |
3385 |
} |
3386 |
|
3387 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3388 |
// example from comment 13 |
3389 |
public void test068() { |
3390 |
Map options = getCompilerOptions(); |
3391 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3392 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3393 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3394 |
this.runConformTest(new String[] { |
3395 |
"Test068.java", |
3396 |
"import java.io.*;\n" + |
3397 |
"public class Test068 {\n" + |
3398 |
" class ProcessingStep extends OutputStream {\n" + |
3399 |
" public void write(int b) throws IOException {}\n" + |
3400 |
" public OutputStream getDestination() { return null; }\n" + |
3401 |
" }\n" + |
3402 |
" class ArtifactOutputStream extends OutputStream {\n" + |
3403 |
" public void write(int b) throws IOException {}\n" + |
3404 |
" }" + |
3405 |
" ArtifactOutputStream comment13(OutputStream stream) {\n" + |
3406 |
" OutputStream current = stream;\n" + |
3407 |
" while (current instanceof ProcessingStep)\n" + |
3408 |
" current = ((ProcessingStep) current).getDestination(); //warning\n" + |
3409 |
" if (current instanceof ArtifactOutputStream)\n" + |
3410 |
" return (ArtifactOutputStream) current;\n" + |
3411 |
" return null;\n" + |
3412 |
" }\n" + |
3413 |
"}\n" |
3414 |
}, |
3415 |
"", |
3416 |
null, |
3417 |
true, |
3418 |
null, |
3419 |
options, |
3420 |
null); |
3421 |
} |
3422 |
|
3423 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3424 |
// example from comment 16 |
3425 |
public void test069() { |
3426 |
if (this.complianceLevel < ClassFileConstants.JDK1_5) return; // generics used |
3427 |
Map options = getCompilerOptions(); |
3428 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3429 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3430 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3431 |
this.runConformTest(new String[] { |
3432 |
"Test069.java", |
3433 |
"import java.io.*;\n" + |
3434 |
"import java.util.Collection;\n" + |
3435 |
"public class Test069 {\n" + |
3436 |
" class Profile {}\n" + |
3437 |
" class CoreException extends Exception {}\n" + |
3438 |
" void writeProfilesToStream(Collection<Profile> p, OutputStream s, String enc) {}\n" + |
3439 |
" CoreException createException(IOException ioex, String message) { return new CoreException(); }\n" + |
3440 |
" public void comment16(Collection<Profile> profiles, File file, String encoding) throws CoreException {\n" + |
3441 |
" final OutputStream stream;\n" + |
3442 |
" try {\n" + |
3443 |
" stream= new FileOutputStream(file);\n" + |
3444 |
" try {\n" + |
3445 |
" writeProfilesToStream(profiles, stream, encoding);\n" + |
3446 |
" } finally {\n" + |
3447 |
" try { stream.close(); } catch (IOException e) { /* ignore */ }\n" + |
3448 |
" }\n" + |
3449 |
" } catch (IOException e) {\n" + |
3450 |
" throw createException(e, \"message\"); // should not shout here\n" + |
3451 |
" }\n" + |
3452 |
" }\n" + |
3453 |
"}\n" |
3454 |
}, |
3455 |
"", |
3456 |
null, |
3457 |
true, |
3458 |
null, |
3459 |
options, |
3460 |
null); |
3461 |
} |
3462 |
|
3463 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3464 |
// referenced in array initializer |
3465 |
public void test070() { |
3466 |
Map options = getCompilerOptions(); |
3467 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3468 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3469 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3470 |
this.runNegativeTest(new String[] { |
3471 |
"Test070.java", |
3472 |
"import java.io.*;\n" + |
3473 |
"public class Test070 {\n" + |
3474 |
" void storeInArray(String fileName) throws IOException {\n" + |
3475 |
" FileReader fileRead = new FileReader(fileName);\n" + |
3476 |
" closeThemAll(new FileReader[] { fileRead });\n" + |
3477 |
" }\n" + |
3478 |
" void closeThemAll(FileReader[] readers) { }\n" + |
3479 |
"}\n" |
3480 |
}, |
3481 |
"----------\n" + |
3482 |
"1. ERROR in Test070.java (at line 4)\n" + |
3483 |
" FileReader fileRead = new FileReader(fileName);\n" + |
3484 |
" ^^^^^^^^\n" + |
3485 |
"Potential resource leak: \'fileRead\' may not be closed\n" + |
3486 |
"----------\n", |
3487 |
null, |
3488 |
true, |
3489 |
options); |
3490 |
} |
3491 |
|
3492 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3493 |
// referenced in array initializer |
3494 |
public void test071() { |
3495 |
Map options = getCompilerOptions(); |
3496 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3497 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3498 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3499 |
this.runNegativeTest(new String[] { |
3500 |
"Test071.java", |
3501 |
"import java.io.*;\n" + |
3502 |
"public class Test071 {\n" + |
3503 |
" class ReaderHolder {\n" + |
3504 |
" FileReader reader;\n" + |
3505 |
" }\n" + |
3506 |
" private FileReader getReader() {\n" + |
3507 |
" return null;\n" + |
3508 |
" }\n" + |
3509 |
" void invokeCompiler(ReaderHolder readerHolder, boolean flag) throws FileNotFoundException {\n" + |
3510 |
" FileReader reader = readerHolder.reader;\n" + |
3511 |
" if (reader == null)\n" + |
3512 |
" reader = getReader();\n" + |
3513 |
" try {\n" + |
3514 |
" return;\n" + |
3515 |
" } finally {\n" + |
3516 |
" try {\n" + |
3517 |
" if (flag)\n" + |
3518 |
" reader.close();\n" + |
3519 |
" } catch (IOException e) {\n" + |
3520 |
" // nop\n" + |
3521 |
" }\n" + |
3522 |
" }\n" + |
3523 |
" }\n" + |
3524 |
"}\n" |
3525 |
}, |
3526 |
"----------\n" + |
3527 |
"1. ERROR in Test071.java (at line 14)\n" + |
3528 |
" return;\n" + |
3529 |
" ^^^^^^^\n" + |
3530 |
"Potential resource leak: \'reader\' may not be closed at this location\n" + |
3531 |
"----------\n", |
3532 |
null, |
3533 |
true, |
3534 |
options); |
3535 |
} |
3536 |
|
3537 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3538 |
// referenced in array initializer |
3539 |
// disabled because it would require correlation analysis between the tracking variable and its original |
3540 |
// need to pass to downstream: either (nonnull & open) or (null) |
3541 |
public void _test071b() { |
3542 |
Map options = getCompilerOptions(); |
3543 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3544 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3545 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3546 |
this.runNegativeTest(new String[] { |
3547 |
"Test071b.java", |
3548 |
"import java.io.*;\n" + |
3549 |
"public class Test071b {\n" + |
3550 |
" private FileReader getReader() {\n" + |
3551 |
" return null;\n" + |
3552 |
" }\n" + |
3553 |
" void invokeCompiler(boolean flag) throws FileNotFoundException {\n" + |
3554 |
" FileReader reader = null;\n" + |
3555 |
" if (flag)\n" + |
3556 |
" reader = new FileReader(\"file\");\n" + |
3557 |
" if (reader == null)\n" + |
3558 |
" reader = getReader();\n" + |
3559 |
" try {\n" + |
3560 |
" return;\n" + |
3561 |
" } finally {\n" + |
3562 |
" try {\n" + |
3563 |
" if (flag)\n" + |
3564 |
" reader.close();\n" + |
3565 |
" } catch (IOException e) {\n" + |
3566 |
" // nop\n" + |
3567 |
" }\n" + |
3568 |
" }\n" + |
3569 |
" }\n" + |
3570 |
"}\n" |
3571 |
}, |
3572 |
"----------\n" + |
3573 |
"1. ERROR in Test071.java (at line 14)\n" + |
3574 |
" return;\n" + |
3575 |
" ^^^^^^^\n" + |
3576 |
"Potential resource leak: \'reader\' may not be closed at this location\n" + |
3577 |
"----------\n", |
3578 |
null, |
3579 |
true, |
3580 |
options); |
3581 |
} |
3582 |
|
3583 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3584 |
// throw inside loop inside try - while closed in finally |
3585 |
public void test072() { |
3586 |
Map options = getCompilerOptions(); |
3587 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3588 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3589 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3590 |
this.runConformTest(new String[] { |
3591 |
"Test072.java", |
3592 |
"import java.io.*;\n" + |
3593 |
"public class Test072 {\n" + |
3594 |
" void readState(File file) {\n" + |
3595 |
" DataInputStream in = null;\n" + |
3596 |
" try {\n" + |
3597 |
" in= new DataInputStream(new BufferedInputStream(new FileInputStream(file)));\n" + |
3598 |
" int sizeOfFlags = in.readInt();\n" + |
3599 |
" for (int i = 0; i < sizeOfFlags; ++i) {\n" + |
3600 |
" String childPath = in.readUTF();\n" + |
3601 |
" if (childPath.length() == 0)\n" + |
3602 |
" throw new IOException();\n" + |
3603 |
" }\n" + |
3604 |
" }\n" + |
3605 |
" catch (IOException ioe) { /* nop */ }\n" + |
3606 |
" finally {\n" + |
3607 |
" if (in != null) {\n" + |
3608 |
" try {in.close();} catch (IOException ioe) {}\n" + |
3609 |
" }\n" + |
3610 |
" }\n" + |
3611 |
" }\n" + |
3612 |
"}\n" |
3613 |
}, |
3614 |
"", |
3615 |
null, |
3616 |
true, |
3617 |
null, |
3618 |
options, |
3619 |
null); |
3620 |
} |
3621 |
|
3622 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3623 |
// unspecific parameter is casted into a resource, yet need to mark as OWNED_BY_OUTSIDE |
3624 |
public void test073() { |
3625 |
Map options = getCompilerOptions(); |
3626 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3627 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3628 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3629 |
this.runConformTest(new String[] { |
3630 |
"Test073.java", |
3631 |
"import java.io.*;\n" + |
3632 |
"public class Test073 {\n" + |
3633 |
" String getEncoding(Object reader) {\n" + |
3634 |
" if (reader instanceof FileReader) {\n" + |
3635 |
" final FileReader fr = (FileReader) reader;\n" + |
3636 |
" return fr.getEncoding();\n" + |
3637 |
" }\n" + |
3638 |
" return null;\n" + |
3639 |
" }\n" + |
3640 |
"}\n" |
3641 |
}, |
3642 |
"", |
3643 |
null, |
3644 |
true, |
3645 |
null, |
3646 |
options, |
3647 |
null); |
3648 |
} |
3649 |
|
3650 |
// Bug 368546 - [compiler][resource] Avoid remaining false positives found when compiling the Eclipse SDK |
3651 |
// status after nested try-finally |
3652 |
public void test074() { |
3653 |
Map options = getCompilerOptions(); |
3654 |
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR); |
3655 |
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR); |
3656 |
options.put(CompilerOptions.OPTION_ReportMissingSerialVersion, CompilerOptions.IGNORE); |
3657 |
this.runNegativeTest(new String[] { |
3658 |
"Test074.java", |
3659 |
"import java.io.*;\n" + |
3660 |
"public class Test074 {\n" + |
3661 |
" void foo() throws FileNotFoundException {\n" + |
3662 |
" FileOutputStream out = null;\n" + |
3663 |
" try {\n" + |
3664 |
" out = new FileOutputStream(\"outfile\");\n" + |
3665 |
" } finally {\n" + |
3666 |
" try {\n" + |
3667 |
" out.flush();\n" + |
3668 |
" out.close();\n" + |
3669 |
" } catch (IOException e) {\n" + |
3670 |
" e.printStackTrace();\n" + |
3671 |
" }\n" + |
3672 |
" out = null;\n" + // unclosed if exception occurred on flush() |
3673 |
" }\n" + |
3674 |
" }\n" + |
3675 |
"}\n" |
3676 |
}, |
3677 |
"----------\n" + |
3678 |
"1. ERROR in Test074.java (at line 14)\n" + |
3679 |
" out = null;\n" + |
3680 |
" ^^^^^^^^^^\n" + |
3681 |
"Potential resource leak: \'out\' may not be closed at this location\n" + |
3682 |
"----------\n", |
3683 |
null, |
3684 |
true, |
3685 |
options); |
3686 |
} |
3192 |
} |
3687 |
} |