File: GenericsCasts.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 (53 lines) | stat: -rw-r--r-- 1,644 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
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

class GenericsCasts {
    // Cast from a raw type to a generic type
    // :: warning: [unchecked] unchecked cast
    List<Object>[] o = (List<Object>[]) new List[] {new ArrayList()};

    class Data<T> {}

    // Use our own dummy method as to avoid a complaint from the Signature Checker
    Data<?> forName(String p) {
        throw new Error("");
    }

    void m() {
        // Cast from a wildcard to a normal type argument.
        // Warning only with -AcheckCastElementType.
        // TODO:: warning: (cast.unsafe)
        // :: warning: [unchecked] unchecked cast
        Data<GenericsCasts> c = (Data<GenericsCasts>) forName("HaHa!");
    }

    // Casts from something with one type argument to two type arguments
    // are currently problematic.
    // TODO: try to find a problem with skipping this check.
    class Test<K extends Object, V extends Object> {
        class Entry<K extends Object, V extends Object> extends LinkedList<K> {}

        class Queue<T extends Object> {
            List<? extends T> poll() {
                throw new Error("");
            }
        }

        void trouble() {
            Queue<K> queue = new Queue<>();
            // Warning only with -AcheckCastElementType.
            // TODO:: warning: (cast.unsafe)
            // :: warning: [unchecked] unchecked cast
            Entry<K, V> e = (Entry<K, V>) queue.poll();
        }
    }

    public static <T extends Object> int indexOf(T[] a) {
        return indexOfEq(a);
    }

    public static int indexOfEq(Object[] a) {
        return 0;
    }
}