File: GenericsExampleMin.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 (94 lines) | stat: -rw-r--r-- 2,580 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import org.checkerframework.checker.nullness.qual.*;

// This is the example from manual section:
// "Generics (parametric polymorphism or type polymorphism)"
// whose source code is ../../../docs/manual/advanced-features.tex
class GenericsExampleMin {

    class MyList1<@Nullable T> {
        T t;
        @Nullable T nble;
        @NonNull T nn;

        public MyList1(T t, @Nullable T nble, @NonNull T nn) {
            this.t = t;
            this.nble = nble;
            this.nn = nn;
            this.t = this.nble;
        }

        T get(int i) {
            return t;
        }

        // This method works.
        // Note that it fails to work if it is moved after m2() in the syntax tree.
        // TODO: the above comment seems out-of-date, as method
        // m3 below works.
        void m1() {
            t = this.get(0);
            nble = this.get(0);
        }

        // When the assignment to nn is added, the assignments to t and nble also fail, which is
        // unexpected.
        void m2() {
            // :: error: (assignment.type.incompatible)
            nn = null;
            t = this.get(0);
            nble = this.get(0);
        }

        void m3() {
            t = this.get(0);
            nble = this.get(0);
        }
    }

    class MyList2<@NonNull T> {
        T t;
        @Nullable T nble;

        public MyList2(T t, @Nullable T nble) {
            // :: error: (assignment.type.incompatible)
            this.t = this.nble; // error
            // :: error: (assignment.type.incompatible)
            this.t = nble; // error
        }
    }

    class MyList3<T extends @Nullable Object> {
        T t;
        @Nullable T nble;
        @NonNull T nn;

        public MyList3(T t, @Nullable T nble, @NonNull T nn) {
            // :: error: (assignment.type.incompatible)
            this.t = nble;
            this.t = nn;
            // :: error: (assignment.type.incompatible)
            this.nn = t;
            // :: error: (assignment.type.incompatible)
            this.nn = nble;
            this.nn = nn;
        }
    }

    class MyList4<T extends @NonNull Object> {
        T t;
        @Nullable T nble;
        @NonNull T nn;

        public MyList4(T t, @Nullable T nble, @NonNull T nn) {
            // :: error: (assignment.type.incompatible)
            this.t = nble;
            this.t = nn;
            this.nn = t;
            // :: error: (assignment.type.incompatible)
            this.nn = nble;
            this.nn = nn;
            this.nn = t;
            this.nble = t;
        }
    }
}