1/*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/*
25 * @test
26 * @bug 4916607
27 * @summary Test casts (errors)
28 * @author gafter
29 *
30 * @compile/fail/ref=CastFail.out -XDrawDiagnostics CastFail.java
31 */
32
33import java.util.*;
34
35class CastFail {
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<Number>) null; // <<fail 1>>
52        o = (AC<String>) (AA<Vector<Number>>) null; // <<fail 2>>
53        o = (AC<String>) (AA<Stack<String>>) null; // <<fail 3>>
54        o = (AD<String>) (AA<Vector<? extends Number>>) null; // <<fail 4>>
55        o = (AE<Number>) (AA<Vector<? super String>>) null; // <<fail 5>>
56        o = (AF<String>) (AA<Number[]>) null; // <<fail 6>>
57        o = (AG<?>) (AA<Number>) null; // <<fail 7>>
58    }
59
60    // --- Inconsistent matches ---
61
62    private class BA<T> { }
63    private class BB<T, S> { }
64
65    private class BC<T> extends BA<Integer> { }
66    private class BD<T> extends BB<T, T> { }
67
68    private void inconsistentMatches() {
69        Object o;
70
71        o = (BC<?>) (BA<String>) null; // <<fail 8>>
72        o = (BD<String>) (BB<String, Number>) null; // <<fail 9>>
73        o = (BD<String>) (BB<Number, String>) null; // <<fail 10>>
74    }
75
76    // --- Transferring parameters via supertypes ---
77
78    private interface CA<T> { }
79    private interface CB<T> extends CA<T> { }
80    private interface CC<T> extends CA<T> { }
81
82    private class CD<T> implements CB<T> { }
83    private interface CE<T> extends CC<T> { }
84
85    private interface CF<S> { }
86    private interface CG<T> { }
87    private class CH<S, T> implements CF<S>, CG<T> { }
88    private interface CI<S> extends CF<S> { }
89    private interface CJ<T> extends CG<T> { }
90    private interface CK<S, T> extends CI<S>, CJ<T> { }
91
92    private void supertypeParameterTransfer() {
93        Object o;
94        CD<?> cd = (CE<?>) null; // <<fail 11>>
95        CE<?> ce = (CD<?>) null; // <<fail 12>>
96        o = (CE<Number>) (CD<String>) null; // <<fail 13>>
97
98        // 4916622: unnecessary warning with cast
99        // o = (CH<String, Integer>) (CK<String, Integer>) null; // <<pass>> <<todo: cast-infer>>
100    }
101
102    // --- Disjoint ---
103
104    private interface DA<T> { }
105    private interface DB<T> extends DA<T> { }
106    private interface DC<T> extends DA<Integer> { }
107
108    private <N extends Number, I extends Integer, R extends Runnable, S extends String> void disjointness() {
109        Object o;
110
111        // Classes
112        o = (DA<Number>) (DA<Integer>) null; // <<fail 14>>
113        o = (DA<? extends Integer>) (DA<Number>) null; // <<fail 15>>
114        o = (DA<? super Number>) (DA<Integer>) null; // <<fail 16>>
115        o = (DA<? extends Runnable>) (DA<? extends String>) null; // <<fail 17>>
116        o = (DA<? super Number>) (DA<? extends Integer>) null; // <<fail 18>>
117
118        // Typevars
119        o = (DA<? extends String>) (DA<I>) null; // <<fail 19>>
120        o = (DA<S>) (DA<R>) null; // <<fail 20>>
121
122        // Raw (asymmetrical!)
123        o = (DC<?>) (DA<? super String>) null; // <<fail 21>>
124    }
125}
126