| 1 |
import org.checkerframework.checker.nullness.qual.*; |
| 2 |
|
| 3 |
// This is the example from manual section: |
| 4 |
// "Generics (parametric polymorphism or type polymorphism)" |
| 5 |
// whose source code is ../../../docs/manual/advanced-features.tex |
| 6 |
class GenericsExampleMin { |
| 7 |
|
| 8 |
class MyList1<@Nullable T> { |
| 9 |
T t; |
| 10 |
@Nullable T nble; |
| 11 |
@NonNull T nn; |
| 12 |
|
| 13 |
public MyList1(T t, @Nullable T nble, @NonNull T nn) { |
| 14 |
this.t = t; |
| 15 |
this.nble = nble; |
| 16 |
this.nn = nn; |
| 17 |
this.t = this.nble; |
| 18 |
} |
| 19 |
|
| 20 |
T get(int i) { |
| 21 |
return t; |
| 22 |
} |
| 23 |
|
| 24 |
// This method works. |
| 25 |
// Note that it fails to work if it is moved after m2() in the syntax tree. |
| 26 |
// TODO: the above comment seems out-of-date, as method |
| 27 |
// m3 below works. |
| 28 |
void m1() { |
| 29 |
t = this.get(0); |
| 30 |
nble = this.get(0); |
| 31 |
} |
| 32 |
|
| 33 |
// When the assignment to nn is added, the assignments to t and nble also fail, which is |
| 34 |
// unexpected. |
| 35 |
void m2() { |
| 36 |
// :: error: (assignment.type.incompatible) |
| 37 |
nn = null; |
| 38 |
t = this.get(0); |
| 39 |
nble = this.get(0); |
| 40 |
} |
| 41 |
|
| 42 |
void m3() { |
| 43 |
t = this.get(0); |
| 44 |
nble = this.get(0); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
class MyList2<@NonNull T> { |
| 49 |
T t; |
| 50 |
@Nullable T nble; |
| 51 |
|
| 52 |
public MyList2(T t, @Nullable T nble) { |
| 53 |
// :: error: (assignment.type.incompatible) |
| 54 |
this.t = this.nble; // error |
| 55 |
// :: error: (assignment.type.incompatible) |
| 56 |
this.t = nble; // error |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
class MyList3<T extends @Nullable Object> { |
| 61 |
T t; |
| 62 |
@Nullable T nble; |
| 63 |
@NonNull T nn; |
| 64 |
|
| 65 |
public MyList3(T t, @Nullable T nble, @NonNull T nn) { |
| 66 |
// :: error: (assignment.type.incompatible) |
| 67 |
this.t = nble; |
| 68 |
this.t = nn; |
| 69 |
// :: error: (assignment.type.incompatible) |
| 70 |
this.nn = t; |
| 71 |
// :: error: (assignment.type.incompatible) |
| 72 |
this.nn = nble; |
| 73 |
this.nn = nn; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
class MyList4<T extends @NonNull Object> { |
| 78 |
T t; |
| 79 |
@Nullable T nble; |
| 80 |
@NonNull T nn; |
| 81 |
|
| 82 |
public MyList4(T t, @Nullable T nble, @NonNull T nn) { |
| 83 |
// :: error: (assignment.type.incompatible) |
| 84 |
this.t = nble; |
| 85 |
this.t = nn; |
| 86 |
this.nn = t; |
| 87 |
// :: error: (assignment.type.incompatible) |
| 88 |
this.nn = nble; |
| 89 |
this.nn = nn; |
| 90 |
this.nn = t; |
| 91 |
this.nble = t; |
| 92 |
} |
| 93 |
} |
| 94 |
} |