forked from typetools/checker-framework
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Figure3NC.java
40 lines (31 loc) · 839 Bytes
/
Figure3NC.java
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
// Passes the Nullness Checker but fails with a ClassCastException!
import org.checkerframework.checker.nullness.qual.Nullable;
public class Figure3NC {
static class Type<A> {
class Constraint<B extends A> extends Type<B> {}
<B> @Nullable Constraint<? super B> bad() {
return null;
}
<B> A coerce(B b) {
return pair(this.<B>bad(), b).value;
}
}
static class Sum<T> {
@Nullable Type<T> type;
T value;
Sum(@Nullable Type<T> t, T v) {
type = t;
value = v;
}
}
static <T> Sum<T> pair(@Nullable Type<T> type, T value) {
return new Sum<T>(type, value);
}
static <T, U> U coerce(T t) {
Type<U> type = new Type<U>();
return type.<T>coerce(t);
}
public static void main(String[] args) {
String zero = Figure3NC.<Integer, String>coerce(0);
}
}