Debian Codegraph @

checker/tests/nullness/generics/MethodTypeVars5.java

raw
1 import org.checkerframework.checker.nullness.qual.*;
2
3 class MethodTypeVars5 {
4 class B<S extends @Nullable Object> {
5 S t;
6
7 B(S t) {
8 this.t = t;
9 }
10
11 S get() {
12 return t;
13 }
14 }
15
16 B<String> b = new B<>("Hello World");
17
18 String doit1() {
19 return doit1(b);
20 }
21
22 <U extends @Nullable Object> U doit1(B<U> x) {
23 return x.get();
24 }
25
26 String doit2() {
27 // Passing the null argument has no effect on the inferred type argument:
28 // the second parameter type doesn't contain the type variable at all.
29 return doit2(b, null);
30 }
31
32 <T extends @Nullable Object> T doit2(B<T> x, @Nullable String y) {
33 return x.get();
34 }
35
36 String doit3() {
37 // Passing the null argument has no effect on the inferred type argument:
38 // the type variable only appears as nested type.
39 return doit3(null);
40 }
41
42 String doit3b() {
43 return doit3(new B<String>("Hi"));
44 }
45
46 String doit3b2() {
47 return doit3(new B<>("Hi"));
48 }
49
50 String doit3c() {
51 // :: error: (return.type.incompatible)
52 return doit3(new B<@Nullable String>("Hi"));
53 }
54
55 void doit3d() {
56 // :: error: (assignment.type.incompatible)
57 @NonNull String s = doit3(new B<@Nullable String>("Hi"));
58 }
59
60 void doit3e() {
61 String s = doit3(new B<String>("Hi"));
62 }
63
64 void doit3e2() {
65 String s = doit3(new B<>("Hi"));
66 }
67
68 <T extends @Nullable Object> T doit3(@Nullable B<T> x) {
69 if (x != null) {
70 return x.get();
71 } else {
72 // This won't work at runtime, but whatever.
73 @SuppressWarnings("unchecked")
74 T res = (T) new Object();
75 return res;
76 }
77 }
78
79 String doit4() {
80 // Passing the null argument has an impact on the inferred type argument:
81 // the type variable appears as the top-level type.
82 // :: error: (return.type.incompatible)
83 return doit4("Ha!", null);
84 }
85
86 <T extends @Nullable Object> T doit4(T x, T y) {
87 return x;
88 }
89 }