-
Notifications
You must be signed in to change notification settings - Fork 107
/
NullAnnotationTests21.java
1104 lines (1045 loc) · 36.7 KB
/
NullAnnotationTests21.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) 2021, 2023 GK Software SE, and others.
*
* 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.io.File;
import java.io.IOException;
import java.util.Map;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.tests.util.Util;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import junit.framework.Test;
public class NullAnnotationTests21 extends AbstractNullAnnotationTest {
public NullAnnotationTests21(String name) {
super(name);
}
static {
// TESTS_NAMES = new String[] { "test_totalTypePatternNonNullExpression" };
// TESTS_NUMBERS = new int[] { 001 };
// TESTS_RANGE = new int[] { 1, 12 };
}
public static Test suite() {
return buildMinimalComplianceTestSuite(testClass(), F_21);
}
public static Class<?> testClass() {
return NullAnnotationTests21.class;
}
@Deprecated // super method is deprecated
@Override
protected void setUpAnnotationLib() throws IOException {
if (this.LIBS == null) {
String[] defaultLibs = getDefaultClassPaths();
int len = defaultLibs.length;
this.LIBS = new String[len+1];
System.arraycopy(defaultLibs, 0, this.LIBS, 0, len);
this.LIBS[len] = NullAnnotationTests9.createAnnotation_2_2_jar(Util.getOutputDirectory() + File.separator, null);
}
}
// -------- helper ------------
private Runner getDefaultRunner() {
Runner runner = new Runner();
runner.classLibraries = this.LIBS;
Map<String,String> opts = getCompilerOptions();
opts.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_21);
runner.customOptions = opts;
runner.javacTestOptions =
JavacTestOptions.Excuse.EclipseWarningConfiguredAsError;
return runner;
}
// --------- tests start -----------
public void test_typePatternIsNN() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(Object o) {\n" +
" switch (o) {\n" +
" case Integer i -> consumeInt(i);\n" +
" default -> System.out.println(\"default\");\n" +
" }\n" +
" }\n" +
" void consumeInt(@NonNull Integer i) {\n" +
" System.out.print(i);\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(3);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "3";
runner.runConformTest();
}
public void test_totalTypePatternDoesNotAdmitNull() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(Number n) {\n" +
" try {\n" +
" switch (n) {\n" +
" case Integer i -> consumeInt(i);\n" +
" case Number n0 -> consumeNumber(n0);\n" +
" }\n" +
" } catch (NullPointerException npe) {\n" +
" // ignoring the unchecked warning, and expecting the NPE:\n" +
" System.out.print(npe.getMessage());\n" +
" }\n" +
" }\n" +
" void consumeInt(@NonNull Integer i) {\n" +
" System.out.print(i);\n" +
" }\n" +
" void consumeNumber(@NonNull Number n) {\n" +
" System.out.print(n.toString());\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(null);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. WARNING in X.java (at line 7)\n" +
" case Number n0 -> consumeNumber(n0);\n" +
" ^^\n" +
"Null type safety (type annotations): The expression of type \'Number\' needs unchecked conversion to conform to \'@NonNull Number\'\n" +
"----------\n";
// runner.expectedOutputString = "Cannot invoke \"Object.toString()\" because \"n\" is null";
runner.expectedOutputString = "null";
runner.runConformTest();
}
public void test_totalTypePatternNonNullExpression() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(Number n) {\n" +
" if (n == null) return;\n" + // this prevents the NPE -> no need to warn
" switch (n) {\n" +
" case Integer i -> System.out.print(i);\n" +
" case Number n0 -> consumeNumber(n0);\n" +
" }\n" +
" }\n" +
" void consumeNumber(@NonNull Number n) {\n" +
" System.out.print(n.toString());\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(null);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "";
runner.runConformTest();
}
public void test_totalTypePatternNonNullExpression_swExpr() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" int foo(Number n) {\n" +
" if (n == null) return -1;\n" + // this prevents the NPE -> no need to warn
" return switch (n) {\n" +
" case Integer i -> i;\n" +
" case Number n0 -> consumeNumber(n0);\n" +
" };\n" +
" }\n" +
" int consumeNumber(@NonNull Number n) {\n" +
" return Integer.valueOf(n.toString());\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(null);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "";
runner.runConformTest();
}
public void test_totalTypePatternPlusNullPattern() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(Number n) {\n" +
" switch (n) {\n" +
" case null -> System.out.print(\"null\");\n" + // this prevents the NPE
" case Integer i -> System.out.print(i);\n" +
" case Number n0 -> consumeNumber(n0);\n" +
" }\n" +
" }\n" +
" void consumeNumber(@NonNull Number n) {\n" +
" System.out.print(n.toString());\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(null);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "null";
runner.runConformTest();
}
public void test_totalTypePatternNullableExpression() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(@Nullable Number n) {\n" + // @Nullable here turns "unchecked" into "null type mismatch"
" switch (n) {\n" +
" case Integer i -> System.out.print(i);\n" +
" case Number n0 -> consumeNumber(n0);\n" +
" }\n" +
" }\n" +
" void consumeNumber(@NonNull Number n) {\n" +
" System.out.print(n.toString());\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(null);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 6)\n" +
" case Number n0 -> consumeNumber(n0);\n" +
" ^^\n" +
"Null type mismatch: required \'@NonNull Number\' but the provided value is inferred as @Nullable\n" +
"----------\n";
runner.runNegativeTest();
}
public void test_switchOverNNValueWithNullCase() {
Runner runner = getDefaultRunner();
runner.customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(@NonNull Object o) {\n" +
" switch (o) {\n" +
" case Integer i -> consumeInt(i);\n" +
" case null -> System.out.print(\"null\");\n" +
" default -> System.out.println(\"default\");\n" +
" }\n" +
" }\n" +
" void consumeInt(@NonNull Integer i) {\n" +
" System.out.print(i);\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(3);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. WARNING in X.java (at line 6)\n" +
" case null -> System.out.print(\"null\");\n" +
" ^^^^^^^^^\n" +
"Unnecessary \'null\' pattern, the switch selector expression cannot be null\n" +
"----------\n";
runner.expectedOutputString = "3";
runner.runConformTest();
}
// null cannot be in the same case with pattern as per the 432+433 jep
public void _test_switchNullInSameCase() {
Runner runner = getDefaultRunner();
runner.customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(Object o) {\n" +
" switch (o) {\n" +
" case null, Integer i -> consumeInt(i);\n" +
" default -> System.out.println(\"default\");\n" +
" }\n" +
" }\n" +
" void consumeInt(@NonNull Integer i) {\n" +
" System.out.print(i);\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(3);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 5)\n" +
" case null, Integer i -> consumeInt(i);\n" +
" ^\n" +
"Null type mismatch: required \'@NonNull Integer\' but the provided value is inferred as @Nullable\n" +
"----------\n";
runner.runNegativeTest();
}
public void test_switchOverNNValueWithNullCase_swExpr() {
Runner runner = getDefaultRunner();
runner.customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" int foo(@NonNull Object o) {\n" +
" return switch (o) {\n" +
" case Integer i -> consumeInt(i);\n" +
" case null -> 0;\n" +
" default -> -1;\n" +
" };\n" +
" }\n" +
" int consumeInt(@NonNull Integer i) {\n" +
" return i;\n" +
" }\n" +
" public static void main(String... args) {\n" +
" System.out.print(new X().foo(3));\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. WARNING in X.java (at line 6)\n" +
" case null -> 0;\n" +
" ^^^^^^^^^\n" +
"Unnecessary \'null\' pattern, the switch selector expression cannot be null\n" +
"----------\n";
runner.expectedOutputString = "3";
runner.runConformTest();
}
public void test_nullHostileSwitch() {
Runner runner = getDefaultRunner();
runner.customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(@Nullable Object o) {\n" +
" switch (o) {\n" +
" case Integer i -> consumeInt(i);\n" +
" default -> System.out.println(o);\n" +
" };\n" +
" }\n" +
" void consumeInt(@NonNull Integer i) {\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(null);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 4)\n" +
" switch (o) {\n" +
" ^\n" +
"Potential null pointer access: this expression has a \'@Nullable\' type\n" +
"----------\n";
runner.runNegativeTest();
}
public void test_defaultDoesNotApplyToNull() {
Runner runner = getDefaultRunner();
runner.customOptions.put(CompilerOptions.OPTION_ReportRedundantNullCheck, CompilerOptions.WARNING);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" void foo(@Nullable Object o) {\n" +
" switch (o) {\n" +
" case Integer i -> consumeInt(i);\n" +
" case null -> System.out.print(\"null\");\n" +
" default -> System.out.println(o.toString());\n" +
" };\n" +
" }\n" +
" void consumeInt(@NonNull Integer i) {\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(null);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "null";
runner.runConformTest();
}
public void test_defaultDoesNotApplyToNull_field() {
Runner runner = getDefaultRunner();
runner.customOptions.put(CompilerOptions.OPTION_SyntacticNullAnalysisForFields, CompilerOptions.ENABLED);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" @Nullable Object o;\n" +
" void foo() {\n" +
" switch (this.o) {\n" +
" case Integer i -> consumeInt(i);\n" +
" case null -> System.out.print(\"null\");\n" +
" default -> System.out.println(this.o.toString());\n" +
" };\n" +
" }\n" +
" void consumeInt(@NonNull Integer i) {\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo();\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "null";
runner.runConformTest();
}
public void test_defaultDoesNotApplyToNull_field2() {
Runner runner = getDefaultRunner();
runner.customOptions.put(CompilerOptions.OPTION_SyntacticNullAnalysisForFields, CompilerOptions.ENABLED);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" @Nullable Object o;\n" +
" void foo(X x) {\n" +
" switch (x.o) {\n" +
" case Integer i -> consumeInt(i);\n" +
" case null -> System.out.print(\"null\");\n" +
" default -> System.out.println(x.o.toString());\n" +
" };\n" +
" }\n" +
" void consumeInt(@NonNull Integer i) {\n" +
" }\n" +
" public static void main(String... args) {\n" +
" new X().foo(new X());\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "null";
runner.runConformTest();
}
public void testBug576329() {
Runner runner = getDefaultRunner();
runner.customOptions.put(CompilerOptions.OPTION_SyntacticNullAnalysisForFields, CompilerOptions.ENABLED);
runner.testFiles = new String[] {
"Main.java",
"public class Main {\n" +
" int length;\n" +
" public String switchOnArray(Object argv[]) {\n" +
" return switch(argv.length) {\n" +
" case 0 -> \"0\";\n" +
" default -> \"x\";\n" +
" };\n" +
" }\n" +
" public static void main(String... args) {\n" +
" System.out.print(new Main().switchOnArray(args));\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "0";
runner.runConformTest();
}
public void testInstanceOfPatternIsNonNull() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" public static void consumeNonNull(@NonNull String s) {\n" +
" System.out.println(\"nonnull\");\n" +
" }\n" +
" public static void main(String... args) {\n" +
" Object o = Math.random() < 0 ? new Object() : \"blah\";\n" +
" if (o instanceof String message) {\n" +
" consumeNonNull(message);\n" +
" }\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog = "";
runner.expectedOutputString = "nonnull";
runner.runConformTest();
}
public void testInstanceOfPatternIsLaterAssignedNull() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public class X {\n" +
" public static void consumeNonNull(@NonNull String s) {\n" +
" System.out.println(\"nonnull\");\n" +
" }\n" +
" public static void main(String... args) {\n" +
" Object o = Math.random() >= 0 ? new Object() : \"blah\";\n" +
" if (o instanceof String message) {\n" +
" consumeNonNull(message);\n" +
" message = null;\n" +
" consumeNonNull(message);\n" +
" }\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 11)\n" +
" consumeNonNull(message);\n" +
" ^^^^^^^\n" +
"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" +
"----------\n";
runner.runNegativeTest();
}
// since 11: uses 'var'
public void testNullableVar() {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"Test.java",
"\n" +
"import org.eclipse.jdt.annotation.NonNull;\n" +
"\n" +
"public class Test {\n" +
" public @NonNull Test getSomeValue() { return this; }\n" +
" \n" +
" void test(boolean rainyDay) {\n" +
" var a = rainyDay ? getSomeValue() : null;\n" +
" a.getSomeValue(); // problem not detected\n" +
" }\n" +
" void test2(boolean rainyDay) {\n" +
" Test a = rainyDay ? getSomeValue() : null;\n" +
" a.getSomeValue(); // Potential null pointer access: The variable a may be null at this location\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in Test.java (at line 9)\n" +
" a.getSomeValue(); // problem not detected\n" +
" ^\n" +
"Potential null pointer access: The variable a may be null at this location\n" +
"----------\n" +
"2. ERROR in Test.java (at line 13)\n" +
" a.getSomeValue(); // Potential null pointer access: The variable a may be null at this location\n" +
" ^\n" +
"Potential null pointer access: The variable a may be null at this location\n" +
"----------\n";
runner.runNegativeTest();
}
public void _testGH629_01() {
Map<String, String> options = getCompilerOptions();
options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_18);
options.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "test.NonNull");
options.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "test.Nullable");
options.put(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.ENABLED);
runNegativeTestWithLibs(
new String[] {
"Configuration.java",
"public interface Configuration {\n" +
"}\n",
"Init.java",
"public interface Init<C extends Configuration> {\n" +
"}\n",
"Annot.java",
"public @interface Annot {\n" +
" Class<? extends Init<? extends Configuration>>[] inits(); \n" +
"}\n",
"App.java",
"interface I<T> {}\n" +
"@Annot(inits = {App.MyInit.class})\n" +
"public class App {\n" +
" static class MyInit implements I<String>, Init<Configuration> {}\n" +
"}\n"
},
options,
"");
}
public void _testGH629_02() {
Map<String, String> options = getCompilerOptions();
options.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_18);
options.put(JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, "test.NonNull");
options.put(JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, "test.Nullable");
options.put(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS, JavaCore.ENABLED);
runNegativeTestWithLibs(
new String[] {
"Annot.java",
"public @interface Annot {\n" +
" Class<? extends Init<? extends Configuration>>[] inits(); \n" +
"}\n",
"App.java",
"@Annot(inits = {App.MyInit.class})\n" +
"public class App {\n" +
" static class MyInit implements Init<Configuration> {}\n" +
"}\n",
"Configuration.java",
"public interface Configuration {\n" +
"}\n",
"Init.java",
"public interface Init<C extends Configuration> {\n" +
"}\n"
},
options,
"");
}
public void testBug572361() {
runConformTestWithLibs(
new String[] {
"NonNullByDefaultAndRecords.java",
"import org.eclipse.jdt.annotation.NonNullByDefault;\n" +
"\n" +
"@NonNullByDefault\n" +
"public record NonNullByDefaultAndRecords () { }\n"
},
getCompilerOptions(),
"");
}
public void testIssue233_ok() throws Exception {
Runner runner = getDefaultRunner();
runner.customOptions = getCompilerOptions();
runner.customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.IGNORE);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"record A1(@NonNull String ca1, String ca2) {}\n" +
"record B1(@Nullable String cb1, String cb2) {}\n" +
"@NonNullByDefault\n" +
"public class X {\n" +
" record A2(@NonNull String ca1, String ca2) {}\n" +
" record B2(@Nullable String cb1, String cb2) {}\n" +
"\n" +
" public static @NonNull String workWithA(A1 a, boolean f) {\n" +
" return f ? a.ca1() : a.ca2();\n" +
" }\n" +
" public static @NonNull String workWithA(A2 a, boolean f) {\n" +
" return f ? a.ca1() : a.ca2();\n" +
" }\n" +
" public static String workWithB(B1 b, boolean f) {\n" +
" if (f) {\n" +
" String c = b.cb1();\n" +
" return c != null ? c : \"default \";\n" +
" }\n" +
" return b.cb2();\n" +
" }\n" +
" public static String workWithB(B2 b, boolean f) {\n" +
" if (f) {\n" +
" String c = b.cb1();\n" +
" return c != null ? c : \"default \";\n" +
" }\n" +
" return b.cb2();\n" +
" }\n" +
" public static void main(String... args) {\n" +
" @NonNull String sa11 = workWithA(new A1(\"hello \", \"A11 \"), true);\n" +
" @NonNull String sa12 = workWithA(new A1(\"hello \", \"A12 \"), false);\n" +
" @NonNull String sb11 = workWithB(new B1(null, \"B11 \"), true);\n" +
" @NonNull String sb12 = workWithB(new B1(null, \"B12 \"), false);\n" +
" @NonNull String sa21 = workWithA(new A2(\"hello \", \"A21 \"), true);\n" +
" @NonNull String sa22 = workWithA(new A2(\"hello \", \"A22 \"), false);\n" +
" @NonNull String sb21 = workWithB(new B2(null, \"B21\"), true);\n" +
" @NonNull String sb22 = workWithB(new B2(null, \"B22\"), false);\n" +
" System.out.println(sa11+sa12+sb11+sb12+sa21+sa22+sb21+sb22);\n" +
" }\n" +
"}\n"
};
runner.expectedOutputString = "hello A12 default B12 hello A22 default B22";
runner.runConformTest();
}
public void testIssue233_nok() throws Exception {
// like testIssue233_ok - but annotations on record components ca1 / cb1 swapped (twice)
Runner runner = getDefaultRunner();
runner.customOptions = getCompilerOptions();
runner.customOptions.put(JavaCore.COMPILER_PB_REDUNDANT_NULL_ANNOTATION, JavaCore.IGNORE);
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"record A1(@Nullable String ca1, String ca2) {}\n" +
"record B1(@NonNull String cb1, String cb2) {}\n" +
"@NonNullByDefault\n" +
"public class X {\n" +
" record A2(@Nullable String ca1, String ca2) {}\n" +
" record B2(@NonNull String cb1, String cb2) {}\n" +
"\n" +
" public static @NonNull String workWithA(A1 a, boolean f) {\n" +
" return f ? a.ca1() : a.ca2();\n" +
" }\n" +
" public static @NonNull String workWithA(A2 a, boolean f) {\n" +
" return f ? a.ca1() : a.ca2();\n" +
" }\n" +
" public static String workWithB(B1 b, boolean f) {\n" +
" if (f) {\n" +
" String c = b.cb1();\n" +
" return c != null ? c : \"default \";\n" +
" }\n" +
" return b.cb2();\n" +
" }\n" +
" public static String workWithB(B2 b, boolean f) {\n" +
" if (f) {\n" +
" String c = b.cb1();\n" +
" return c != null ? c : \"default \";\n" +
" }\n" +
" return b.cb2();\n" +
" }\n" +
" public static void main(String... args) {\n" +
" @NonNull String sa11 = workWithA(new A1(\"hello \", \"A11 \"), true);\n" +
" @NonNull String sa12 = workWithA(new A1(\"hello \", \"A12 \"), false);\n" +
" @NonNull String sb11 = workWithB(new B1(null, \"B11 \"), true);\n" +
" @NonNull String sb12 = workWithB(new B1(null, \"B12 \"), false);\n" +
" @NonNull String sa21 = workWithA(new A2(\"hello \", \"A21 \"), true);\n" +
" @NonNull String sa22 = workWithA(new A2(\"hello \", \"A22 \"), false);\n" +
" @NonNull String sb21 = workWithB(new B2(null, \"B21\"), true);\n" +
" @NonNull String sb22 = workWithB(new B2(null, \"B22\"), false);\n" +
" System.out.println(sa11+sa12+sb11+sb12+sa21+sa22+sb21+sb22);\n" +
" }\n" +
"}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 10)\n" +
" return f ? a.ca1() : a.ca2();\n" +
" ^^^^^^^\n" +
"Null type mismatch (type annotations): required \'@NonNull String\' but this expression has type \'@Nullable String\'\n" +
"----------\n" +
"2. WARNING in X.java (at line 10)\n" +
" return f ? a.ca1() : a.ca2();\n" +
" ^^^^^^^\n" +
"Null type safety (type annotations): The expression of type \'String\' needs unchecked conversion to conform to \'@NonNull String\'\n" +
"----------\n" +
"3. ERROR in X.java (at line 13)\n" +
" return f ? a.ca1() : a.ca2();\n" +
" ^^^^^^^\n" +
"Null type mismatch (type annotations): required \'@NonNull String\' but this expression has type \'@Nullable String\'\n" +
"----------\n" +
"4. ERROR in X.java (at line 18)\n" +
" return c != null ? c : \"default \";\n" +
" ^\n" +
"Redundant null check: The variable c cannot be null at this location\n" +
"----------\n" +
"5. WARNING in X.java (at line 20)\n" +
" return b.cb2();\n" +
" ^^^^^^^\n" +
"Null type safety (type annotations): The expression of type \'String\' needs unchecked conversion to conform to \'@NonNull String\'\n" +
"----------\n" +
"6. ERROR in X.java (at line 25)\n" +
" return c != null ? c : \"default \";\n" +
" ^\n" +
"Redundant null check: The variable c cannot be null at this location\n" +
"----------\n" +
"7. ERROR in X.java (at line 32)\n" +
" @NonNull String sb11 = workWithB(new B1(null, \"B11 \"), true);\n" +
" ^^^^\n" +
"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" +
"----------\n" +
"8. ERROR in X.java (at line 33)\n" +
" @NonNull String sb12 = workWithB(new B1(null, \"B12 \"), false);\n" +
" ^^^^\n" +
"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" +
"----------\n" +
"9. ERROR in X.java (at line 36)\n" +
" @NonNull String sb21 = workWithB(new B2(null, \"B21\"), true);\n" +
" ^^^^\n" +
"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" +
"----------\n" +
"10. ERROR in X.java (at line 37)\n" +
" @NonNull String sb22 = workWithB(new B2(null, \"B22\"), false);\n" +
" ^^^^\n" +
"Null type mismatch: required \'@NonNull String\' but the provided value is null\n" +
"----------\n";
runner.expectedOutputString = "hellodefault";
runner.runNegativeTest();
}
public void testIssue233_npeWitness() throws Exception {
Runner runner = getDefaultRunner();
runner.testFiles = new String[] {
"X.java",
"import org.eclipse.jdt.annotation.*;\n" +
"public record X(@NonNull String ca1, String ca2, @Nullable String ca2) {}\n"
};
runner.expectedCompilerLog =
"----------\n" +
"1. ERROR in X.java (at line 2)\n" +
" public record X(@NonNull String ca1, String ca2, @Nullable String ca2) {}\n" +
" ^^^\n" +
"Duplicate component ca2 in record\n" +
"----------\n" +
"2. ERROR in X.java (at line 2)\n" +
" public record X(@NonNull String ca1, String ca2, @Nullable String ca2) {}\n" +
" ^^^\n" +
"Duplicate component ca2 in record\n" +
"----------\n" +
"3. ERROR in X.java (at line 2)\n" +
" public record X(@NonNull String ca1, String ca2, @Nullable String ca2) {}\n" +
" ^^^\n" +
"Duplicate parameter ca2\n" +
"----------\n";
runner.runNegativeTest();
}
public void testGH1399() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"C.java",
"""
@interface Ann { Class<? extends A> value(); }
class A {}
@Ann(C.B.class) // <- ERROR: Type mismatch: cannot convert from Class<C.B> to Class<? extends A>
class C<T extends Number> {
class B extends A {}
}
"""};
runner.runConformTest();
}
public void testGH1399_2() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"C.java",
"""
@interface Ann { Class<? extends A> value(); }
class A {}
@Ann(C.B.class)
class C<T extends java.util.List<Number>> {
class B extends A {}
}
"""};
runner.runConformTest();
}
public void testGH1302() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"p/package-info.java",
"""
@org.eclipse.jdt.annotation.NonNullByDefault
package p;
""",
"p/Parent.java",
"""
package p;
import java.util.Map;
public interface Parent {
Map<String, String> model();
}
""",
"p/Child.java",
"""
package p;
import java.util.Map;
public record Child(Map<String, String> model) implements Parent {
}
"""
};
runner.customOptions = getCompilerOptions();
runner.classLibraries = this.LIBS;
runner.runConformTest();
}
public void testGH1691_a() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"bug/package-info.java",
"""
@org.eclipse.jdt.annotation.NonNullByDefault
package bug;
""",
"bug/BlahSuper.java",
"""
package bug;
import java.io.IOException;
import java.io.OutputStream;
public sealed interface BlahSuper<T, E extends Exception> permits Blah, BlahOther { }
abstract non-sealed class BlahOther implements BlahSuper<OutputStream, IOException> { }
""",
"bug/Blah.java",
"""
package bug;
import java.io.IOException;
import java.io.OutputStream;
public abstract non-sealed class Blah<T, E extends Exception> implements BlahSuper<T, E> {
public abstract static class InnerBlah extends Blah<OutputStream, IOException> { }
}
"""
};
runner.classLibraries = this.LIBS;
runner.runConformTest();
}
public void testGH1691_b() {
// @NonNull on secondary bound is sufficient
Runner runner = new Runner();
runner.testFiles = new String[] {
"bug/Marker.java",
"""
package bug;
public interface Marker {}
""",
"bug/MyException.java",
"""
package bug;
import java.io.IOException;
public class MyException extends IOException implements Marker {}
""",
"bug/BlahSuper.java",
"""
package bug;
import java.io.OutputStream;
import org.eclipse.jdt.annotation.NonNullByDefault;
@NonNullByDefault
public sealed interface BlahSuper<T, E extends Exception & Marker> permits Blah, BlahOther { }
@NonNullByDefault
abstract non-sealed class BlahOther implements BlahSuper<OutputStream, MyException> { }
""",
"bug/Blah.java",
"""
package bug;
import java.io.OutputStream;
import org.eclipse.jdt.annotation.NonNull;
public abstract non-sealed class Blah<T, E extends Exception & @NonNull Marker> implements BlahSuper<T, E> {
public abstract static class InnerBlah extends Blah<OutputStream, @NonNull MyException> { }
}
"""
};
runner.classLibraries = this.LIBS;
runner.runConformTest();
}
public void testGH1009() {
Runner runner = new Runner();
Map<String, String> options = getCompilerOptions();
options.put(CompilerOptions.OPTION_ReportAnnotatedTypeArgumentToUnannotated, CompilerOptions.ERROR);
runner.customOptions = options;
runner.testFiles = new String[] {
"UnsafeNullTypeConversionFalsePositive.java",
"""
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNull;
public class UnsafeNullTypeConversionFalsePositive {
public static void main(final String[] args) {
final List<@NonNull StringBuffer> someList = new ArrayList<>();
List<@NonNull String> results;
// was buggy:
results = someList.stream().map(String::new).collect(Collectors.toList());
// was OK:
results = someList.stream().map(buff -> new String(buff)).collect(Collectors.toList());
results = someList.stream().<@NonNull String>map(String::new).collect(Collectors.toList());
}
}
"""
};
runner.classLibraries = this.LIBS;
runner.runConformTest();
}
public void testGH1760() {
Runner runner = new Runner();
runner.testFiles = new String[] {
"X.java",
"""
import org.eclipse.jdt.annotation.*;
import java.util.*;
import java.util.stream.*;
public class X {
List<@NonNull String> filter(List<@Nullable String> input) {
return input.stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}
"""
};
runner.classLibraries = this.LIBS;
runner.runConformTest();
}