| 1 |
import org.checkerframework.checker.nullness.qual.*; |
| 2 |
import org.checkerframework.dataflow.qual.*; |
| 3 |
|
| 4 |
class AnnotatedGenerics { |
| 5 |
public static void testNullableTypeVariable() { |
| 6 |
// :: error: (initialization.fields.uninitialized) |
| 7 |
class Test<T extends @Nullable Object> { |
| 8 |
T f; |
| 9 |
|
| 10 |
@Nullable T get() { |
| 11 |
return f; |
| 12 |
} |
| 13 |
} |
| 14 |
Test<Iterable<String>> l = new Test<>(); |
| 15 |
// :: error: (iterating.over.nullable) |
| 16 |
for (String s : l.get()) ; |
| 17 |
} |
| 18 |
|
| 19 |
public static void testNonNullTypeVariable() { |
| 20 |
class Test<T extends @Nullable Object> { |
| 21 |
@NonNull T get() { |
| 22 |
throw new RuntimeException(); |
| 23 |
} |
| 24 |
} |
| 25 |
Test<@Nullable Iterable<String>> l = new Test<>(); |
| 26 |
for (String s : l.get()) ; |
| 27 |
Test<Iterable<String>> n = new Test<>(); |
| 28 |
for (String s : n.get()) ; |
| 29 |
} |
| 30 |
|
| 31 |
static class MyClass<T> implements MyIterator<@Nullable T> { |
| 32 |
public boolean hasNext() { |
| 33 |
return true; |
| 34 |
} |
| 35 |
|
| 36 |
public @Nullable T next() { |
| 37 |
return null; |
| 38 |
} |
| 39 |
|
| 40 |
public void remove() {} |
| 41 |
|
| 42 |
static void test() { |
| 43 |
MyClass<String> c = new MyClass<>(); |
| 44 |
String c1 = c.next(); |
| 45 |
@Nullable String c2 = c.next(); |
| 46 |
// :: error: (assignment.type.incompatible) |
| 47 |
@NonNull String c3 = c.next(); |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
public static final class MyComprator<T extends MyComparable<T>> { |
| 52 |
public void compare(T a1, T a2) { |
| 53 |
a1.compareTo(a2); |
| 54 |
} |
| 55 |
|
| 56 |
public void compare2(@NonNull T a1, @NonNull T a2) { |
| 57 |
a1.compareTo(a2); |
| 58 |
} |
| 59 |
|
| 60 |
public void compare3(T a1, @Nullable T a2) { |
| 61 |
// :: error: (argument.type.incompatible) |
| 62 |
a1.compareTo(a2); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
class MyComparable<T> { |
| 67 |
@Pure |
| 68 |
public int compareTo(@NonNull T a1) { |
| 69 |
return 0; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
<T> T test(java.util.List<? super Iterable<?>> l) { |
| 74 |
test(new java.util.ArrayList<Object>()); |
| 75 |
throw new Error(); |
| 76 |
} |
| 77 |
|
| 78 |
public interface MyIterator<E extends @Nullable Object> { |
| 79 |
boolean hasNext(); |
| 80 |
|
| 81 |
E next(); |
| 82 |
|
| 83 |
void remove(); |
| 84 |
} |
| 85 |
} |