File: GenericArgs.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 (60 lines) | stat: -rw-r--r-- 1,742 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
import java.io.*;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.*;
import org.checkerframework.dataflow.qual.*;

@org.checkerframework.framework.qual.DefaultQualifier(Nullable.class)
public class GenericArgs {

    public @NonNull Set<@NonNull String> strings = new HashSet<>();

    void test() {
        @NonNull HashSet<@NonNull String> s = new HashSet<>();

        strings.addAll(s);
        strings.add("foo");
    }

    static class X<@NonNull T extends @NonNull Object> {
        T value() {
            // :: error: (return.type.incompatible)
            return null;
        }
    }

    public static void test2() {
        // :: error: (type.argument.type.incompatible)
        Object o = new X<Object>().value();
    }

    static <@NonNull Z extends @NonNull Object> void test3(Z z) {}

    void test4() {
        // :: error: (type.argument.type.incompatible)
        GenericArgs.<@Nullable Object>test3(null);
        // :: error: (argument.type.incompatible)
        GenericArgs.<@NonNull Object>test3(null);
    }

    static class GenericConstructor {
        <@NonNull T extends @NonNull Object> GenericConstructor(T t) {}
    }

    void test5() {
        // :: error: (argument.type.incompatible)
        new <@NonNull String>GenericConstructor(null);
    }

    void testRecursiveDeclarations() {
        class MyComparator<@NonNull T extends @NonNull Comparable<T>>
                implements Comparator<T @NonNull []> {
            @Pure
            public int compare(T[] a, T[] b) {
                return 0;
            }
        }
        Comparator<@NonNull String @NonNull []> temp = new MyComparator<@NonNull String>();
    }
}