author | hseigel |
Wed, 02 Mar 2016 23:48:41 +0000 | |
changeset 36397 | c487ced7231c |
parent 30724 | 0686b5ea7958 |
child 37636 | 6c6e6e25189d |
permissions | -rw-r--r-- |
10 | 1 |
/* |
30724
0686b5ea7958
8074514: Group 13d: golden files for tests in tools/javac/generics dir
sogoel
parents:
5520
diff
changeset
|
2 |
* @test /nodynamiccopyright/ |
10 | 3 |
* @bug 4607420 |
4 |
* @summary A bug in the original JSR14 generics specification |
|
5 |
* created a loophole in the type system. |
|
6 |
* |
|
30724
0686b5ea7958
8074514: Group 13d: golden files for tests in tools/javac/generics dir
sogoel
parents:
5520
diff
changeset
|
7 |
* @compile/fail/ref=Nonlinear.out -XDrawDiagnostics Nonlinear.java |
10 | 8 |
*/ |
9 |
||
10 |
||
11 |
public class Nonlinear { |
|
12 |
||
13 |
// This is an example of lack of type safety for |
|
14 |
// the version of javac from jsr14_adding_generics-1_0-ea |
|
15 |
||
16 |
// It is a variant of the "classic" problem with polymorphic |
|
17 |
// references in SML, which resulted in the usual array of |
|
18 |
// fixes: notably value polymorphism. |
|
19 |
||
20 |
// This code compiles, but produces a ClassCastException |
|
21 |
// when executed, even though there are no explicit casts in |
|
22 |
// the program. |
|
23 |
||
24 |
public static void main (String [] args) { |
|
25 |
Integer x = new Integer (5); |
|
26 |
String y = castit (x); |
|
27 |
System.out.println (y); |
|
28 |
} |
|
29 |
||
30 |
static <A,B> A castit (B x) { |
|
31 |
// This method casts any type to any other type. |
|
32 |
// Oh dear. This shouldn't type check, but does |
|
33 |
// because build () returns a type Ref<*> |
|
34 |
// which is a subtype of RWRef<A,B>. |
|
35 |
final RWRef<A,B> r = build (); |
|
36 |
r.set (x); |
|
37 |
return r.get (); |
|
38 |
} |
|
39 |
||
40 |
static <A> Ref<A> build () { |
|
41 |
return new Ref<A> (); |
|
42 |
} |
|
43 |
||
44 |
// Another way of doing this is a variant of the crackit |
|
45 |
// example discussed in the draft specification. |
|
46 |
// |
|
47 |
// The original duplicate was: |
|
48 |
// |
|
49 |
// static <A> Pair <A,A> duplicate (A x) { |
|
50 |
// return new Pair<A,A> (x,x); |
|
51 |
// } |
|
52 |
// |
|
53 |
// which breaks the requirement that a type variable |
|
54 |
// instantiated by * only occurs once in the result type. |
|
55 |
// |
|
56 |
// However, we can achieve the same result with a different |
|
57 |
// type for duplicate, which uses its type variables linearly |
|
58 |
// in the result: |
|
59 |
||
60 |
static <A,B extends Ref<A>> Pair<Ref<A>,B> duplicate (B x) { |
|
61 |
return new Pair<Ref<A>,B> (x,x); |
|
62 |
} |
|
63 |
||
64 |
// the cheat here is that A and B are used linearly in the result |
|
65 |
// type, but not in the polymorphic bounds. |
|
66 |
||
67 |
// We can use that to give an alternative implementation of |
|
68 |
// castit. |
|
69 |
||
70 |
static <A,B> A castit2 (B x) { |
|
71 |
Pair <Ref<A>, Ref<B>> p = duplicate (build ()); |
|
72 |
p.snd.set (x); |
|
73 |
return p.fst.get (); |
|
74 |
} |
|
75 |
||
76 |
||
77 |
} |
|
78 |
||
79 |
interface RWRef<A,B> { |
|
80 |
||
81 |
public A get (); |
|
82 |
public void set (B x); |
|
83 |
||
84 |
} |
|
85 |
||
86 |
class Ref<A> implements RWRef <A,A> { |
|
87 |
||
88 |
A contents; |
|
89 |
||
90 |
public void set (A x) { contents = x; } |
|
91 |
public A get () { return contents; } |
|
92 |
||
93 |
} |
|
94 |
||
95 |
class Pair<A,B> { |
|
96 |
||
97 |
final A fst; |
|
98 |
final B snd; |
|
99 |
||
100 |
Pair (A fst, B snd) { this.fst = fst; this.snd = snd; } |
|
101 |
||
102 |
} |