Debian Codegraph @

checker/tests/nullness/generics/WildcardSubtyping.java

raw
1 import java.lang.annotation.Annotation;
2 import java.util.HashMap;
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.Set;
7 import org.checkerframework.checker.nullness.qual.*;
8
9 class Utils {
10
11 <A extends @Nullable Object> void test(List<? super A> list, A object) {
12 list.add(object);
13 }
14
15 interface Consumer<A extends @Nullable Object> {
16 public void consume(A object);
17 }
18
19 public static <A extends @Nullable Object> Consumer<A> cast(
20 final Consumer<@Nullable ? super A> consumer) {
21 return new Consumer<A>() {
22 @Override
23 public void consume(A object) {
24 consumer.consume(object);
25 }
26 };
27 }
28
29 public static <A extends @Nullable Object> Consumer<A> getConsumer(
30 Consumer<@Nullable Object> nullConsumer) {
31 return Utils.<A>cast(nullConsumer);
32 }
33
34 Map<String, Set<?>> mss = new HashMap<>();
35
36 Set<Class<? extends Annotation>> foo() {
37 Set<Class<? extends Annotation>> l = new HashSet<>(this.foo());
38 return l;
39 }
40 }
41
42 class MyGeneric<@NonNull T extends @Nullable Number> {}
43
44 class UseMyGeneric {
45 MyGeneric<?> wildcardUnbounded = new MyGeneric<>();
46
47 // :: error: (assignment.type.incompatible)
48 MyGeneric<? extends @NonNull Object> wildcardOutsideUB = wildcardUnbounded;
49 MyGeneric<? extends @NonNull Number> wildcardInsideUB = wildcardOutsideUB;
50 // :: error: (assignment.type.incompatible)
51 MyGeneric<? extends @NonNull Number> wildcardInsideUB2 = wildcardUnbounded;
52
53 MyGeneric<? extends @Nullable Number> wildcardInsideUBNullable = wildcardOutsideUB;
54 }
55
56 class MyGenericExactBounds<@NonNull T extends @NonNull Number> {}
57
58 class UseMyGenericExactBounds {
59 // :: error: (type.argument.type.incompatible)
60 MyGenericExactBounds<? extends @Nullable Object> wildcardOutsideUBError =
61 new MyGenericExactBounds<>();
62 MyGenericExactBounds<? extends @NonNull Object> wildcardOutside = new MyGenericExactBounds<>();
63 MyGenericExactBounds<? extends @NonNull Number> wildcardInsideUB = wildcardOutside;
64
65 MyGenericExactBounds<?> wildcardOutsideUB = wildcardOutside;
66 }