| 1 |
import java.util.ArrayList; |
| 2 |
import java.util.LinkedList; |
| 3 |
import java.util.List; |
| 4 |
|
| 5 |
class GenericsCasts { |
| 6 |
// Cast from a raw type to a generic type |
| 7 |
// :: warning: [unchecked] unchecked cast |
| 8 |
List<Object>[] o = (List<Object>[]) new List[] {new ArrayList()}; |
| 9 |
|
| 10 |
class Data<T> {} |
| 11 |
|
| 12 |
// Use our own dummy method as to avoid a complaint from the Signature Checker |
| 13 |
Data<?> forName(String p) { |
| 14 |
throw new Error(""); |
| 15 |
} |
| 16 |
|
| 17 |
void m() { |
| 18 |
// Cast from a wildcard to a normal type argument. |
| 19 |
// Warning only with -AcheckCastElementType. |
| 20 |
// TODO:: warning: (cast.unsafe) |
| 21 |
// :: warning: [unchecked] unchecked cast |
| 22 |
Data<GenericsCasts> c = (Data<GenericsCasts>) forName("HaHa!"); |
| 23 |
} |
| 24 |
|
| 25 |
// Casts from something with one type argument to two type arguments |
| 26 |
// are currently problematic. |
| 27 |
// TODO: try to find a problem with skipping this check. |
| 28 |
class Test<K extends Object, V extends Object> { |
| 29 |
class Entry<K extends Object, V extends Object> extends LinkedList<K> {} |
| 30 |
|
| 31 |
class Queue<T extends Object> { |
| 32 |
List<? extends T> poll() { |
| 33 |
throw new Error(""); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
void trouble() { |
| 38 |
Queue<K> queue = new Queue<>(); |
| 39 |
// Warning only with -AcheckCastElementType. |
| 40 |
// TODO:: warning: (cast.unsafe) |
| 41 |
// :: warning: [unchecked] unchecked cast |
| 42 |
Entry<K, V> e = (Entry<K, V>) queue.poll(); |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
public static <T extends Object> int indexOf(T[] a) { |
| 47 |
return indexOfEq(a); |
| 48 |
} |
| 49 |
|
| 50 |
public static int indexOfEq(Object[] a) { |
| 51 |
return 0; |
| 52 |
} |
| 53 |
} |