forked from eclipse-jdt/eclipse.jdt.core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResourceLeakAnnotatedTests.java
1716 lines (1674 loc) · 47.7 KB
/
ResourceLeakAnnotatedTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright (c) 2024 GK Software SE.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Stephan Herrmann - initial API and implementation
*******************************************************************************/
package org.eclipse.jdt.core.tests.compiler.regression;
import java.util.List;
import java.util.Map;
import org.eclipse.jdt.core.tests.util.AbstractCompilerTest;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import junit.framework.Test;
import junit.framework.TestSuite;
public class ResourceLeakAnnotatedTests extends ResourceLeakTests {
static {
TESTS_NAMES = null; // clear forgotten filter from super class
// TESTS_NAMES = new String[] { "test056l" };
}
// marker field that influences the call to buildTestsList():
public static final int INHERITED_DEPTH = 1;
public ResourceLeakAnnotatedTests(String name) {
super(name);
}
public static Test suite() {
TestSuite suite = new TestSuite(ResourceLeakAnnotatedTests.class.getName());
// argument 'inheritedDepth' is not exposed in original API, therefore these helpers are copied below with this arg added
buildMinimalComplianceTestSuite(F_1_7, 1, suite, ResourceLeakAnnotatedTests.class);
return suite;
}
private static void buildMinimalComplianceTestSuite(int minimalCompliance, int inheritedDepth, TestSuite suite, Class<?> evaluationTestClass) {
int complianceLevels = AbstractCompilerTest.getPossibleComplianceLevels();
for (int[] map : complianceTestLevelMapping) {
if ((complianceLevels & map[0]) != 0) {
long complianceLevelForJavaVersion = ClassFileConstants.getComplianceLevelForJavaVersion(map[1]);
checkCompliance(evaluationTestClass, minimalCompliance, suite, complianceLevels, inheritedDepth, map[0], map[1], getVersionString(complianceLevelForJavaVersion));
}
}
}
protected static void checkCompliance(Class<?> evaluationTestClass, int minimalCompliance, TestSuite suite, int complianceLevels,
int inheritedDepth, int abstractCompilerTestCompliance, int classFileConstantsVersion, String release) {
int lev = complianceLevels & abstractCompilerTestCompliance;
if (lev != 0) {
if (lev < minimalCompliance) {
System.err.println("Cannot run "+evaluationTestClass.getName()+" at compliance " + release + "!");
} else {
suite.addTest(buildUniqueComplianceTestSuite(evaluationTestClass, ClassFileConstants.getComplianceLevelForJavaVersion(classFileConstantsVersion), inheritedDepth));
}
}
}
public static Test buildUniqueComplianceTestSuite(Class<?> evaluationTestClass, long uniqueCompliance, int inheritedDepth) {
long highestLevel = highestComplianceLevels();
if (highestLevel < uniqueCompliance) {
String complianceString;
if (highestLevel == ClassFileConstants.JDK10)
complianceString = "10";
else if (highestLevel == ClassFileConstants.JDK9)
complianceString = "9";
else if (highestLevel == ClassFileConstants.JDK1_8)
complianceString = "1.8";
else if (highestLevel == ClassFileConstants.JDK1_7)
complianceString = "1.7";
else if (highestLevel == ClassFileConstants.JDK1_6)
complianceString = "1.6";
else if (highestLevel == ClassFileConstants.JDK1_5)
complianceString = "1.5";
else if (highestLevel == ClassFileConstants.JDK1_4)
complianceString = "1.4";
else if (highestLevel == ClassFileConstants.JDK1_3)
complianceString = "1.3";
else {
highestLevel = ClassFileConstants.getLatestJDKLevel();
if (highestLevel > 0) {
complianceString = CompilerOptions.versionFromJdkLevel(highestLevel);
} else {
complianceString = "unknown";
}
}
System.err.println("Cannot run "+evaluationTestClass.getName()+" at compliance "+complianceString+"!");
return new TestSuite();
}
TestSuite complianceSuite = new RegressionTestSetup(uniqueCompliance);
List<Test> tests = buildTestsList(evaluationTestClass, inheritedDepth);
for (int index=0, size=tests.size(); index<size; index++) {
complianceSuite.addTest(tests.get(index));
}
return complianceSuite;
}
@Override
protected Map<String, String> getCompilerOptions() {
Map<String, String> options = super.getCompilerOptions();
options.put(CompilerOptions.OPTION_AnnotationBasedResourceAnalysis, CompilerOptions.ENABLED);
options.put(CompilerOptions.OPTION_ReportInsufficientResourceManagement, CompilerOptions.IGNORE);
options.put(CompilerOptions.OPTION_ReportIncompatibleOwningContract, CompilerOptions.ERROR);
return options;
}
protected static final String OWNING_JAVA = "org/eclipse/jdt/annotation/Owning.java";
protected static final String OWNING_CONTENT =
"""
package org.eclipse.jdt.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE_USE})
public @interface Owning {}
""";
protected static final String NOTOWNING_JAVA = "org/eclipse/jdt/annotation/NotOwning.java";
protected static final String NOTOWNING_CONTENT =
"""
package org.eclipse.jdt.annotation;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
public @interface NotOwning {}
""";
@Override
protected String potentialLeakOrCloseNotShown(String resourceName) {
// some leaks are considered as definite when annotations are used.
return "Mandatory close of resource '"+resourceName+"' has not been shown\n";
}
@Override
protected String potentialLeakOrCloseNotShownAtExit(String resourceName) {
// some leaks are considered as definite when annotations are used - variant
// at-exit is irrelevant for unassigned closeables:
String suffix = resourceName.startsWith("<unassigned Closeable") ? "\n" : " at this location\n";
return "Mandatory close of resource '"+resourceName+"' has not been shown"+suffix;
}
@Override
protected String potentialOrDefiniteLeak(String string) {
return "Resource leak: '"+string+"' is never closed\n";
}
protected String fieldDeclPrefix() {
return "@org.eclipse.jdt.annotation.Owning "; // intentionally no linebreak
}
/** Override to add annotation to some tests from the super class. */
@Override
protected void runConformTest(String[] testFiles, String expectedOutput, Map<String, String> customOptions) {
testFiles = addAnnotationSources(testFiles);
super.runConformTest(testFiles, expectedOutput, customOptions);
}
/** Override to add annotation to some tests from the super class. */
@Override
protected void runLeakTest(String[] testFiles, String expectedCompileError, Map options) {
testFiles = addAnnotationSources(testFiles);
super.runLeakTest(testFiles, expectedCompileError, options);
}
private void runLeakTestWithAnnotations(String[] testFiles, String expectedProblems, Map<String, String> options) {
runLeakTestWithAnnotations(testFiles, expectedProblems, options, true);
}
private void runLeakTestWithAnnotations(String[] testFiles, String expectedProblems, Map<String,String> options, boolean shouldFlushOutputDirectory) {
if (options == null)
options = getCompilerOptions();
options.put(CompilerOptions.OPTION_ReportPotentiallyUnclosedCloseable, CompilerOptions.ERROR);
options.put(CompilerOptions.OPTION_ReportUnclosedCloseable, CompilerOptions.ERROR);
options.put(CompilerOptions.OPTION_ReportExplicitlyClosedAutoCloseable, CompilerOptions.INFO);
if (options.get(CompilerOptions.OPTION_ReportInsufficientResourceManagement).equals(CompilerOptions.IGNORE))
options.put(CompilerOptions.OPTION_ReportInsufficientResourceManagement, CompilerOptions.INFO);
testFiles = addAnnotationSources(testFiles);
runLeakTest(testFiles, expectedProblems, options, shouldFlushOutputDirectory);
}
private String[] addAnnotationSources(String[] testFiles) {
int length = testFiles.length;
System.arraycopy(testFiles, 0, testFiles = new String[length+4], 0, length);
testFiles[length+0] = OWNING_JAVA;
testFiles[length+1] = OWNING_CONTENT;
testFiles[length+2] = NOTOWNING_JAVA;
testFiles[length+3] = NOTOWNING_CONTENT;
return testFiles;
}
@Override
protected String getTest056e_log() {
// error is more precise due to default of @Owning on method return
return """
----------
2. ERROR in X.java (at line 11)
FileReader reader = getReader("somefile");
^^^^^^
Resource leak: 'reader' is never closed
----------
""";
}
@Override
protected String getTest056y_log() {
return """
----------
1. ERROR in X.java (at line 4)
final FileReader reader31 = new FileReader("file");
^^^^^^^^
Mandatory close of resource 'reader31' has not been shown
----------
2. WARNING in X.java (at line 17)
final FileReader reader23 = new FileReader("file");
^^^^^^^^
Potential resource leak: 'reader23' may not be closed
----------
""";
}
@Override
protected String getTest061l2_log() {
return """
----------
1. ERROR in xy\\Leaks.java (at line 10)
FileInputStream fileInputStream= new FileInputStream(name);
^^^^^^^^^^^^^^^
Mandatory close of resource 'fileInputStream' has not been shown
----------
2. ERROR in xy\\Leaks.java (at line 18)
this(new FileInputStream("default")); // potential problem
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Mandatory close of resource '<unassigned Closeable value>' has not been shown
----------
3. ERROR in xy\\Leaks.java (at line 25)
FileInputStream fileInputStream= new FileInputStream(name);
^^^^^^^^^^^^^^^
Mandatory close of resource 'fileInputStream' has not been shown
----------
""";
}
@Override
protected String getTest063c_log() {
return """
----------
1. ERROR in X.java (at line 8)
BufferedInputStream bis = new BufferedInputStream(s);
^^^
Mandatory close of resource 'bis' has not been shown
----------
""";
}
@Override
protected String getTestBug440282_log() {
return
"----------\n" +
"1. ERROR in ResourceLeakFalseNegative.java (at line 36)\n" +
" final FileInputStream in = new FileInputStream(\"/dev/null\");\n" +
" ^^\n" +
"Resource leak: 'in' is never closed\n" +
"----------\n" +
"2. ERROR in ResourceLeakFalseNegative.java (at line 39)\n" +
" return new Foo(reader).read();\n" +
" ^^^^^^^^^^^^^^^\n" +
"Resource leak: \'<unassigned Closeable value>\' is never closed\n" +
"----------\n";
}
public void testBug411098_comment19_annotated() {
runLeakTestWithAnnotations(
new String[] {
"A.java",
"import java.io.PrintWriter;\n" +
"import org.eclipse.jdt.annotation.Owning;\n" +
"public class A implements AutoCloseable {\n" +
" @Owning PrintWriter fWriter;\n" +
" boolean useField = false;\n" +
" public void close() {\n" +
" PrintWriter bug= useField ? fWriter : null;\n" +
" System.out.println(bug);\n" +
" }\n" +
"}"
},
"""
----------
1. ERROR in A.java (at line 6)
public void close() {
^^^^^^^
Mandatory close of resource 'this.fWriter' has not been shown
----------
""",
null);
}
public void testBug440282_annotated() {
runLeakTestWithAnnotations(
new String[] {
"ResourceLeakFalseNegative.java",
"import java.io.*;\n" +
"\n" +
"import org.eclipse.jdt.annotation.*;\n" +
"\n" +
"public final class ResourceLeakFalseNegative {\n" +
"\n" +
" private static final class Foo implements AutoCloseable {\n" +
" @Owning final InputStreamReader reader;\n" +
"\n" +
" Foo(@Owning final InputStreamReader reader) {\n" +
" this.reader = reader;\n" +
" }\n" +
" \n" +
" public int read() throws IOException {\n" +
" return reader.read();\n" +
" }\n" +
"\n" +
" public void close() throws IOException {\n" +
" reader.close();\n" +
" }\n" +
" }\n" +
"\n" +
" private static final class Bar {\n" +
" final int read;\n" +
"\n" +
" Bar(final InputStreamReader reader) throws IOException {\n" +
" read = reader.read();\n" +
" }\n" +
" \n" +
" public int read() {\n" +
" return read;\n" +
" }\n" +
" }\n" +
"\n" +
" public final static int foo() throws IOException {\n" +
" final FileInputStream in = new FileInputStream(\"/dev/null\");\n" +
" final InputStreamReader reader = new InputStreamReader(in);\n" +
" try {\n" +
" return new Foo(reader).read();\n" +
" } finally {\n" +
" // even though Foo is not closed, no potential resource leak is reported.\n" +
" }\n" +
" }\n" +
"\n" +
" public final static int bar() throws IOException {\n" +
" final FileInputStream in = new FileInputStream(\"/dev/null\");\n" +
" final InputStreamReader reader = new InputStreamReader(in);\n" +
" try {\n" +
" final Bar bar = new Bar(reader);\n" +
" return bar.read();\n" +
" } finally {\n" +
" // Removing the close correctly reports potential resource leak as a warning,\n" +
" // because Bar does not implement AutoCloseable.\n" +
" reader.close();\n" +
" }\n" +
" }\n" +
"\n" +
" public static void main(String[] args) throws IOException {\n" +
" for (;;) {\n" +
" foo();\n" +
" bar();\n" +
" }\n" +
" }\n" +
"}\n"
},
"----------\n" +
"1. ERROR in ResourceLeakFalseNegative.java (at line 39)\n" +
" return new Foo(reader).read();\n" +
" ^^^^^^^^^^^^^^^\n" +
"Resource leak: \'<unassigned Closeable value>\' is never closed\n" +
"----------\n" +
"2. INFO in ResourceLeakFalseNegative.java (at line 46)\n" +
" final FileInputStream in = new FileInputStream(\"/dev/null\");\n" +
" ^^\n" +
"Resource \'in\' should be managed by try-with-resource\n" +
"----------\n" +
"3. INFO in ResourceLeakFalseNegative.java (at line 47)\n" +
" final InputStreamReader reader = new InputStreamReader(in);\n" +
" ^^^^^^\n" +
"Resource \'reader\' should be managed by try-with-resource\n" +
"----------\n",
null);
}
public void testOwningField_NOK1() {
runLeakTestWithAnnotations(
new String[] {
"A.java",
"""
import java.io.PrintWriter;
import org.eclipse.jdt.annotation.Owning;
import org.eclipse.jdt.annotation.NotOwning;
public class A implements AutoCloseable {
@Owning PrintWriter fWriter;
PrintWriter fOther;
boolean useField = false;
A(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
fWriter = writer1; // OK
fOther = writer2; // not sufficient
}
public void close() {
PrintWriter bug= useField ? fWriter : null;
println(bug);
new A(null, null).fWriter.close(); // closing fWriter of other instance is no good!
}
void println(@NotOwning AutoCloseable rc) { System.out.println(rc); }
}
"""
},
"""
----------
1. INFO in A.java (at line 6)
PrintWriter fOther;
^^^^^^
It is recommended to mark resource fields as '@Owning' to ensure proper closing
----------
2. ERROR in A.java (at line 8)
A(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
^^^^^^^
Mandatory close of resource 'writer2' has not been shown
----------
3. ERROR in A.java (at line 12)
public void close() {
^^^^^^^
Resource leak: 'this.fWriter' is never closed
----------
4. ERROR in A.java (at line 15)
new A(null, null).fWriter.close(); // closing fWriter of other instance is no good!
^^^^^^^^^^^^^^^^^
Resource leak: '<unassigned Closeable value>' is never closed
----------
""",
null);
}
public void testOwningField_NOK2() {
runLeakTestWithAnnotations(
new String[] {
"A.java",
"""
import java.io.PrintWriter;
import org.eclipse.jdt.annotation.Owning;
class ASuper implements AutoCloseable {
public void close() { /* nothing */ }
}
public class A extends ASuper {
@Owning PrintWriter fWriter;
PrintWriter fOther;
boolean useField = false;
A(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
fWriter = writer1; // OK
fOther = writer2; // not sufficient
}
}
"""
},
"""
----------
1. INFO in A.java (at line 7)
@Owning PrintWriter fWriter;
^^^^^^^
Class with resource fields tagged as '@Owning' should implement 'close()'
----------
2. INFO in A.java (at line 8)
PrintWriter fOther;
^^^^^^
It is recommended to mark resource fields as '@Owning' to ensure proper closing
----------
3. ERROR in A.java (at line 10)
A(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
^^^^^^^
Mandatory close of resource 'writer2' has not been shown
----------
""",
null);
}
public void testOwningField_binaryField_lazyInit() {
runLeakTestWithAnnotations(
new String[] {
"A.java",
"""
import java.io.PrintWriter;
import org.eclipse.jdt.annotation.Owning;
public abstract class A implements AutoCloseable {
@Owning PrintWriter fWriter;
PrintWriter fOther;
boolean useField = false;
}
"""
},
"""
----------
1. INFO in A.java (at line 4)
@Owning PrintWriter fWriter;
^^^^^^^
Class with resource fields tagged as '@Owning' should implement 'close()'
----------
2. INFO in A.java (at line 5)
PrintWriter fOther;
^^^^^^
It is recommended to mark resource fields as '@Owning' to ensure proper closing
----------
""",
null);
runLeakTestWithAnnotations(
new String[] {
"AImpl.java",
"""
import java.io.PrintWriter;
import org.eclipse.jdt.annotation.Owning;
public class AImpl extends A {
void init(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
fWriter = writer1; // OK
fOther = writer2; // not sufficient
}
public void close() {
// not closing any fields
}
}
"""
},
"""
----------
1. ERROR in AImpl.java (at line 4)
void init(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
^^^^^^^
Mandatory close of resource 'writer2' has not been shown
----------
2. ERROR in AImpl.java (at line 8)
public void close() {
^^^^^^^
Resource leak: 'this.fWriter' is never closed
----------
""",
null,
false);
}
public void testOwningField_binaryField_lazyInit2() {
runLeakTestWithAnnotations(
new String[] {
"A.java",
"""
import java.io.PrintWriter;
import org.eclipse.jdt.annotation.Owning;
public class A implements AutoCloseable {
@Owning PrintWriter fWriter;
PrintWriter fOther;
boolean useField = false;
public void close() {
if (fWriter != null)
fWriter.close();
}
}
"""
},
"""
----------
1. INFO in A.java (at line 5)
PrintWriter fOther;
^^^^^^
It is recommended to mark resource fields as '@Owning' to ensure proper closing
----------
""",
null);
runLeakTestWithAnnotations(
new String[] {
"AImpl.java",
"""
import java.io.PrintWriter;
import org.eclipse.jdt.annotation.Owning;
public class AImpl extends A {
boolean should = false;
void init(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
fWriter = writer1; // OK
fOther = writer2; // not sufficient
}
public void close() {
if (should)
super.close();
}
}
"""
},
"""
----------
1. ERROR in AImpl.java (at line 5)
void init(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
^^^^^^^
Mandatory close of resource 'writer2' has not been shown
----------
2. ERROR in AImpl.java (at line 9)
public void close() {
^^^^^^^
Potential resource leak: 'this.fWriter' may not be closed
----------
""",
null,
false);
}
public void testOwningField_OK() {
runLeakTestWithAnnotations(
new String[] {
"A.java",
"""
import java.io.PrintWriter;
import org.eclipse.jdt.annotation.Owning;
import org.eclipse.jdt.annotation.NotOwning;
public class A implements AutoCloseable {
@Owning PrintWriter fWriter;
@Owning PrintWriter fOther;
boolean useField = false;
A(@Owning PrintWriter writer1, @Owning PrintWriter writer2) {
fWriter = writer1;
this.fOther = writer2;
}
public void close() {
fWriter.close();
this.fOther.close();
}
void println(@NotOwning AutoCloseable rc) { System.out.println(rc); }
}
"""
},
"",
null);
}
public void testSharedField() {
Map<String, String> options = getCompilerOptions();
options.put(CompilerOptions.OPTION_ReportInsufficientResourceManagement, CompilerOptions.WARNING);
runLeakTestWithAnnotations(
new String[] {
"A.java",
"""
import java.io.PrintWriter;
import org.eclipse.jdt.annotation.Owning;
import org.eclipse.jdt.annotation.NotOwning;
public class A {
@NotOwning PrintWriter fWriter;
PrintWriter fOther;
@Owning PrintWriter fProper;
boolean useField = false;
A(PrintWriter writer1, PrintWriter writer2, @Owning PrintWriter writer3) {
fWriter = writer1;
fOther = writer2;
fProper = writer3;
}
}
"""
},
"""
----------
1. WARNING in A.java (at line 5)
@NotOwning PrintWriter fWriter;
^^^^^^^
It is recommended to mark resource fields as '@Owning' to ensure proper closing
----------
2. WARNING in A.java (at line 6)
PrintWriter fOther;
^^^^^^
It is recommended to mark resource fields as '@Owning' to ensure proper closing
----------
3. WARNING in A.java (at line 7)
@Owning PrintWriter fProper;
^^^^^^^
Class with resource fields tagged as '@Owning' should implement AutoCloseable
----------
""",
options
);
}
public void testOwning_receiving_parameter() {
if (this.complianceLevel < ClassFileConstants.JDK9)
return; // t-w-r with pre-declared local available since 9
runLeakTestWithAnnotations(
new String[] {
"X.java",
"""
import org.eclipse.jdt.annotation.Owning;
public class X {
void nok(@Owning AutoCloseable c) {
System.out.print(1);
}
void ok(@Owning AutoCloseable c) throws Exception {
try (c) {
System.out.print(2);
};
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 3)
void nok(@Owning AutoCloseable c) {
^
Resource leak: 'c' is never closed
----------
""",
null);
}
public void testOwning_sending() {
if (this.complianceLevel < ClassFileConstants.JDK9)
return; // t-w-r with pre-declared local available since 9
runLeakTestWithAnnotations(
new String[] {
"X.java",
"""
import org.eclipse.jdt.annotation.Owning;
class Rc implements AutoCloseable {
@Override public void close() {}
}
public class X {
void consume(@Owning Rc rc1) {
try (rc1) {
System.out.print(2);
};
}
void nok() {
Rc rc2 = new Rc();
System.out.print(rc2);
}
void unsafe(boolean f) {
Rc rc3 = new Rc();
if (f)
consume(rc3);
}
void leakAtReturn(boolean f) {
Rc rc4 = new Rc();
if (f)
return;
consume(rc4);
}
void ok(boolean f) {
Rc rc5 = new Rc();
if (f)
consume(rc5);
else
rc5.close();
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 12)
Rc rc2 = new Rc();
^^^
Mandatory close of resource 'rc2' has not been shown
----------
2. ERROR in X.java (at line 16)
Rc rc3 = new Rc();
^^^
Potential resource leak: 'rc3' may not be closed
----------
3. ERROR in X.java (at line 23)
return;
^^^^^^^
Resource leak: 'rc4' is not closed at this location
----------
""",
null);
}
public void testOwning_sending_toBinary() {
if (this.complianceLevel < ClassFileConstants.JDK9)
return; // t-w-r with pre-declared local available since 9
runLeakTestWithAnnotations(
new String[] {
"p/Consume.java",
"""
package p;
import org.eclipse.jdt.annotation.Owning;
public class Consume {
public void consume(@Owning AutoCloseable rc1) {
try (rc1) {
System.out.print(2);
} catch (Exception e) {
// nothing
}
}
}
"""
},
"",
null);
runLeakTestWithAnnotations(
new String[] {
"X.java",
"""
class Rc implements AutoCloseable {
@Override public void close() {}
}
public class X {
p.Consume consumer;
void unsafe(boolean f) {
Rc rc3 = new Rc();
if (f)
consumer.consume(rc3);
}
void leakAtReturn(boolean f) {
Rc rc4 = new Rc();
if (f)
return;
consumer.consume(rc4);
}
void ok(boolean f) {
Rc rc5 = new Rc();
if (f)
consumer.consume(rc5);
else
rc5.close();
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 8)
Rc rc3 = new Rc();
^^^
Potential resource leak: 'rc3' may not be closed
----------
2. ERROR in X.java (at line 15)
return;
^^^^^^^
Resource leak: 'rc4' is not closed at this location
----------
""",
null,
false);
}
public void testOwning_receiving_from_call() {
runLeakTestWithAnnotations(
new String[] {
"X.java",
"""
import org.eclipse.jdt.annotation.*;
import java.io.*;
public class X {
void err() {
AutoCloseable rc1 = provideOwned();
if (rc1 == null) System.out.print("null");
}
void warn() {
AutoCloseable rc2 = provideShared(); // not responsible
if (rc2 == null) System.out.print("null");
}
void err_without_local() {
if (provideOwned() == null) System.out.print("null");
}
String err_wrapped() {
BufferedInputStream bis = new BufferedInputStream(provideOwned());
return bis.toString();
}
void ok() throws Exception {
try (AutoCloseable c = provideOwned()) {
System.out.print(2);
};
}
@NotOwning AutoCloseable provideShared() {
return null;
}
@Owning InputStream provideOwned() {
return null;
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 5)
AutoCloseable rc1 = provideOwned();
^^^
Resource leak: 'rc1' is never closed
----------
2. ERROR in X.java (at line 13)
if (provideOwned() == null) System.out.print("null");
^^^^^^^^^^^^^^
Resource leak: '<unassigned Closeable value>' is never closed
----------
3. ERROR in X.java (at line 16)
BufferedInputStream bis = new BufferedInputStream(provideOwned());
^^^
Resource leak: 'bis' is never closed
----------
""",
null);
}
public void testOwning_receiving_from_binaryCall() {
runLeakTestWithAnnotations(
new String[] {
"p/Produce.java",
"""
package p;
import org.eclipse.jdt.annotation.*;
import java.io.*;
public class Produce {
public @NotOwning AutoCloseable provideShared() {
return null;
}
public @Owning InputStream provideOwned() {
return null;
}
}
"""
},
"",
null);
runLeakTestWithAnnotations(
new String[] {
"X.java",
"""
import java.io.*;
public class X {
p.Produce producer;
void err() {
AutoCloseable rc1 = producer.provideOwned();
if (rc1 == null) System.out.print("null");
}
void warn() {
AutoCloseable rc2 = producer.provideShared();
if (rc2 == null) System.out.print("null");
}
void err_without_local() {
if (producer.provideOwned() == null) System.out.print("null");
}
String err_wrapped() {
BufferedInputStream bis = new BufferedInputStream(producer.provideOwned());
return bis.toString();
}
void ok() throws Exception {
try (AutoCloseable c = producer.provideOwned()) {
System.out.print(2);
};
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 5)
AutoCloseable rc1 = producer.provideOwned();
^^^
Resource leak: 'rc1' is never closed
----------
2. ERROR in X.java (at line 13)
if (producer.provideOwned() == null) System.out.print("null");
^^^^^^^^^^^^^^^^^^^^^^^
Resource leak: '<unassigned Closeable value>' is never closed
----------
3. ERROR in X.java (at line 16)
BufferedInputStream bis = new BufferedInputStream(producer.provideOwned());
^^^
Resource leak: 'bis' is never closed
----------
""",
null,
false);
}
public void testOwning_return() {
runLeakTestWithAnnotations(
new String[] {
"X.java",
"""
import java.io.*;
import org.eclipse.jdt.annotation.Owning;
public class X {
@Owning AutoCloseable ok(String fn) throws IOException {
return new FileInputStream(fn);
}
@Owning AutoCloseable somePath(String fn) throws IOException {
FileInputStream fis = new FileInputStream(fn);
if (fn.length() > 1)
return fis;
return null;
}
@Owning AutoCloseable mixed(String fn) throws IOException {
FileInputStream fis = new FileInputStream(fn);
if (fn.length() > 1)
return new FileInputStream(fn);
return fis;
}
}
"""
},
"""
----------
1. ERROR in X.java (at line 11)
return null;
^^^^^^^^^^^^
Mandatory close of resource 'fis' has not been shown at this location
----------
2. ERROR in X.java (at line 16)
return new FileInputStream(fn);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^