File: CollectionsAnnotationsMin.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,104 kB
  • sloc: java: 145,916; xml: 839; sh: 518; makefile: 404; perl: 26
file content (58 lines) | stat: -rw-r--r-- 1,862 bytes parent folder | download | duplicates (3)
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
import org.checkerframework.checker.nullness.qual.*;

class CollectionsAnnotationsMin {
    static class Collection1<E extends @Nullable Object> {
        public void add(E elt) {
            // :: error: (dereference.of.nullable)
            elt.hashCode();
        }
    }

    static class PriorityQueue1<E extends @NonNull Object> extends Collection1<E> {
        public void add(E elt) {
            // dereference allowed here
            elt.hashCode();
        }
    }

    // This is allowed, as "null" cannot be added to f1
    static Collection1<? extends @Nullable Object> f1 = new PriorityQueue1<@NonNull Object>();

    // :: error: (assignment.type.incompatible)
    static Collection1<@Nullable Object> f2 = new PriorityQueue1<@NonNull Object>();

    static void addNull1(Collection1<@Nullable Object> l) {
        l.add(null);
    }

    // The upper bound on E is implicitly from Collection1
    static <E extends @Nullable Object> void addNull2(Collection1<E> l) {
        // :: error: (argument.type.incompatible)
        l.add(null);
    }

    // The upper bound on E is implicitly from Collection1
    static <E extends @Nullable Object> E addNull2b(Collection1<E> l, E p) {
        // :: error: (argument.type.incompatible)
        l.add(null);
        return p;
    }

    static <@Nullable E extends @Nullable Object> void addNull3(Collection1<E> l) {
        l.add(null);
    }

    static void bad() {
        // :: error: (argument.type.incompatible)
        addNull1(new PriorityQueue1<@NonNull Object>());

        addNull2(new PriorityQueue1<@NonNull Object>());
        addNull2b(new PriorityQueue1<@NonNull Object>(), new Object());

        // :: error: (type.argument.type.incompatible)
        addNull3(new PriorityQueue1<@NonNull Object>());

        // :: error: (argument.type.incompatible)
        f1.add(null);
    }
}