File: AnnotatedGenerics.java

package info (click to toggle)
checker-framework-java 3.2.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 22,840 kB
  • sloc: java: 145,910; xml: 839; sh: 518; makefile: 401; perl: 26
file content (85 lines) | stat: -rw-r--r-- 2,238 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
import org.checkerframework.checker.nullness.qual.*;
import org.checkerframework.dataflow.qual.*;

class AnnotatedGenerics {
    public static void testNullableTypeVariable() {
        // :: error: (initialization.fields.uninitialized)
        class Test<T extends @Nullable Object> {
            T f;

            @Nullable T get() {
                return f;
            }
        }
        Test<Iterable<String>> l = new Test<>();
        // :: error: (iterating.over.nullable)
        for (String s : l.get()) ;
    }

    public static void testNonNullTypeVariable() {
        class Test<T extends @Nullable Object> {
            @NonNull T get() {
                throw new RuntimeException();
            }
        }
        Test<@Nullable Iterable<String>> l = new Test<>();
        for (String s : l.get()) ;
        Test<Iterable<String>> n = new Test<>();
        for (String s : n.get()) ;
    }

    static class MyClass<T> implements MyIterator<@Nullable T> {
        public boolean hasNext() {
            return true;
        }

        public @Nullable T next() {
            return null;
        }

        public void remove() {}

        static void test() {
            MyClass<String> c = new MyClass<>();
            String c1 = c.next();
            @Nullable String c2 = c.next();
            // :: error: (assignment.type.incompatible)
            @NonNull String c3 = c.next();
        }
    }

    public static final class MyComprator<T extends MyComparable<T>> {
        public void compare(T a1, T a2) {
            a1.compareTo(a2);
        }

        public void compare2(@NonNull T a1, @NonNull T a2) {
            a1.compareTo(a2);
        }

        public void compare3(T a1, @Nullable T a2) {
            // :: error: (argument.type.incompatible)
            a1.compareTo(a2);
        }
    }

    class MyComparable<T> {
        @Pure
        public int compareTo(@NonNull T a1) {
            return 0;
        }
    }

    <T> T test(java.util.List<? super Iterable<?>> l) {
        test(new java.util.ArrayList<Object>());
        throw new Error();
    }

    public interface MyIterator<E extends @Nullable Object> {
        boolean hasNext();

        E next();

        void remove();
    }
}