|
1 /* |
|
2 * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
|
20 * CA 95054 USA or visit www.sun.com if you need additional information or |
|
21 * have any questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @bug 4916607 |
|
27 * @summary Test casts (legal, warning, and errors) |
|
28 * @author gafter |
|
29 * |
|
30 * @compile/fail -source 1.5 -Werror -Xlint:unchecked CastWarn14.java |
|
31 */ |
|
32 |
|
33 import java.util.*; |
|
34 |
|
35 class CastTest { |
|
36 |
|
37 // --- Directly transferring parameters --- |
|
38 |
|
39 private class AA<T> { } |
|
40 |
|
41 private class AB<T> extends AA<T> { } |
|
42 private class AC<T> extends AA<Vector<T>> { } |
|
43 private class AD<T> extends AA<Vector<? extends T>> { } |
|
44 private class AE<T> extends AA<Vector<? super T>> { } |
|
45 private class AF<T> extends AA<T[]> { } |
|
46 private class AG<T> extends AA<String> { } |
|
47 |
|
48 private void parameterTransfer() { |
|
49 Object o; |
|
50 |
|
51 o = (AB<String>) (AA<String>) null; // <<pass>> |
|
52 o = (AB<String>) (AA<Number>) null; // <<fail 1>> |
|
53 o = (AC<String>) (AA<Vector<String>>) null; // <<pass>> |
|
54 o = (AC<String>) (AA<Vector<Number>>) null; // <<fail 2>> |
|
55 o = (AC<String>) (AA<Stack<String>>) null; // <<fail 3>> |
|
56 |
|
57 o = (AD<Number>) (AA<Vector<? extends Number>>) null; // <<pass>> |
|
58 o = (AD<String>) (AA<Vector<? extends Number>>) null; // <<fail 4>> |
|
59 o = (AD<?>) (AA<Vector<? extends Object>>) null; // <<pass>> |
|
60 o = (AD<Object>) (AA<Vector<?>>) null; // <<pass>> |
|
61 |
|
62 o = (AE<String>) (AA<Vector<? super String>>) null; // <<pass>> |
|
63 o = (AE<Number>) (AA<Vector<? super String>>) null; // <<fail 5>> |
|
64 |
|
65 o = (AF<String>) (AA<String[]>) null; // <<pass>> |
|
66 o = (AF<String>) (AA<Number[]>) null; // <<fail 6>> |
|
67 |
|
68 o = (AG<?>) (AA<String>) null; // <<pass>> |
|
69 o = (AG<?>) (AA<Number>) null; // <<fail 7>> |
|
70 } |
|
71 |
|
72 // --- Inconsistent matches --- |
|
73 |
|
74 private class BA<T> { } |
|
75 private class BB<T, S> { } |
|
76 |
|
77 private class BC<T> extends BA<Integer> { } |
|
78 private class BD<T> extends BB<T, T> { } |
|
79 |
|
80 private void inconsistentMatches() { |
|
81 Object o; |
|
82 |
|
83 o = (BC<?>) (BA<Integer>) null; // <<pass>> |
|
84 o = (BC<?>) (BA<String>) null; // <<fail 8>> |
|
85 o = (BD<String>) (BB<String, String>) null; // <<pass>> |
|
86 o = (BD<String>) (BB<String, Number>) null; // <<fail 9>> |
|
87 o = (BD<String>) (BB<Number, String>) null; // <<fail 10>> |
|
88 } |
|
89 |
|
90 private void whyMustEverythingBeSo_______Complicated() { |
|
91 // This has to work... |
|
92 BD<Number> bd = new BD<Number>(); |
|
93 BB<? extends Number, ? super Integer> bb = bd; |
|
94 // 4916620: wildcards: legal cast is rejected |
|
95 // bd = (BD<Number>) bb; // <<warn>> <<todo: cast-infer>> |
|
96 } |
|
97 |
|
98 // --- Transferring parameters via supertypes --- |
|
99 |
|
100 private interface CA<T> { } |
|
101 private interface CB<T> extends CA<T> { } |
|
102 private interface CC<T> extends CA<T> { } |
|
103 |
|
104 private class CD<T> implements CB<T> { } |
|
105 private interface CE<T> extends CC<T> { } |
|
106 |
|
107 private interface CF<S> { } |
|
108 private interface CG<T> { } |
|
109 private class CH<S, T> implements CF<S>, CG<T> { } |
|
110 private interface CI<S> extends CF<S> { } |
|
111 private interface CJ<T> extends CG<T> { } |
|
112 private interface CK<S, T> extends CI<S>, CJ<T> { } |
|
113 |
|
114 private void supertypeParameterTransfer() { |
|
115 Object o; |
|
116 CD<?> cd = (CE<?>) null; // <<fail 11>> |
|
117 CE<?> ce = (CD<?>) null; // <<fail 12>> |
|
118 o = (CE<String>) (CD<String>) null; // <<pass>> |
|
119 o = (CE<Number>) (CD<String>) null; // <<fail 13>> |
|
120 |
|
121 // 4916622: unnecessary warning with cast |
|
122 // o = (CH<String, Integer>) (CK<String, Integer>) null; // <<pass>> <<todo: cast-infer>> |
|
123 } |
|
124 |
|
125 // --- Disjoint --- |
|
126 |
|
127 private interface DA<T> { } |
|
128 private interface DB<T> extends DA<T> { } |
|
129 private interface DC<T> extends DA<Integer> { } |
|
130 |
|
131 private <N extends Number, I extends Integer, R extends Runnable, S extends String> void disjointness() { |
|
132 Object o; |
|
133 |
|
134 // Classes |
|
135 o = (DA<Number>) (DA<Integer>) null; // <<fail 14>> |
|
136 o = (DA<? extends Number>) (DA<Integer>) null; // <<pass>> |
|
137 o = (DA<? extends Integer>) (DA<Number>) null; // <<fail 15>> |
|
138 o = (DA<? super Integer>) (DA<Number>) null; // <<pass>> |
|
139 o = (DA<? super Number>) (DA<Integer>) null; // <<fail 16>> |
|
140 o = (DA<?>) (DA<Integer>) null; // <<pass>> |
|
141 |
|
142 o = (DA<? extends Runnable>) (DA<? extends Number>) null; // <<warn 2>> |
|
143 o = (DA<? extends Runnable>) (DA<? extends String>) null; // <<fail 17>> |
|
144 |
|
145 o = (DA<? super Integer>) (DA<? extends Number>) null; // <<warn 3>> |
|
146 o = (DA<? super Number>) (DA<? extends Integer>) null; // <<fail 18>> |
|
147 o = (DA<?>) (DA<? extends Integer>) null; // <<pass>> |
|
148 |
|
149 o = (DA<? super String>) (DA<? super Number>) null; // <<warn 4>> |
|
150 o = (DA<?>) (DA<? super String>) null; // <<pass>> |
|
151 |
|
152 o = (DA<?>) (DA<?>) null; // <<pass>> |
|
153 |
|
154 // Typevars |
|
155 o = (DA<? extends Number>) (DA<I>) null; // <<pass>> |
|
156 o = (DA<? extends Integer>) (DA<N>) null; // <<warn 5>> |
|
157 o = (DA<? extends String>) (DA<I>) null; // <<fail 19>> |
|
158 |
|
159 o = (DA<I>) (DA<? extends Number>) null; // <<warn 6>> |
|
160 o = (DA<N>) (DA<? extends Integer>) null; // <<warn 7>> |
|
161 o = (DA<N>) (DA<? extends Runnable>) null; // <<warn 8>> |
|
162 |
|
163 o = (DA<N>) (DA<I>) null; // <<warn 9>> |
|
164 o = (DA<N>) (DA<R>) null; // <<warn 10>> |
|
165 o = (DA<S>) (DA<R>) null; // <<fail 20>> |
|
166 |
|
167 // Raw (asymmetrical!) |
|
168 o = (DA) (DB<Number>) null; // <<pass>> |
|
169 o = (DA<Number>) (DB) null; // <<warn 11>> |
|
170 o = (DA<?>) (DB) null; // <<pass>> |
|
171 o = (DA<? extends Object>) (DB) null; // <<pass>> |
|
172 o = (DA<? extends Number>) (DB) null; // <<warn 12>> |
|
173 |
|
174 o = (DB) (DA<Number>) null; // <<pass>> |
|
175 o = (DB<Number>) (DA) null; // <<warn 13>> |
|
176 o = (DB<?>) (DA) null; // <<pass>> |
|
177 o = (DB<? extends Object>) (DA) null; // <<pass>> |
|
178 o = (DB<? extends Number>) (DA) null; // <<warn 14>> |
|
179 |
|
180 o = (DC<?>) (DA<?>) null; // <<pass>> |
|
181 o = (DC<?>) (DA<? super String>) null; // <<fail 21>> |
|
182 } |
|
183 |
|
184 } |