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 (legal)
28 * @author gafter
29 *
30 * @compile CastTest.java
31 */
32
33import java.util.*;
34
35class 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 = (AC<String>) (AA<Vector<String>>) null; // <<pass>>
53        o = (AD<Number>) (AA<Vector<? extends Number>>) null; // <<pass>>
54        o = (AD<?>) (AA<Vector<? extends Object>>) null; // <<pass>>
55        o = (AD<Object>) (AA<Vector<?>>) null; // <<pass>>
56        o = (AE<String>) (AA<Vector<? super String>>) null; // <<pass>>
57        o = (AF<String>) (AA<String[]>) null; // <<pass>>
58        o = (AG<?>) (AA<String>) null; // <<pass>>
59    }
60
61    // --- Inconsistent matches ---
62
63    private class BA<T> { }
64    private class BB<T, S> { }
65
66    private class BC<T> extends BA<Integer> { }
67    private class BD<T> extends BB<T, T> { }
68
69    private void inconsistentMatches() {
70        Object o;
71
72        o = (BC<?>) (BA<Integer>) null; // <<pass>>
73        o = (BD<String>) (BB<String, String>) null; // <<pass>>
74    }
75
76    private void whyMustEverythingBeSo_______Complicated() {
77        // This has to work...
78        BD<Number> bd = new BD<Number>();
79        BB<? extends Number, ? super Integer> bb = bd;
80        // 4916620: wildcards: legal cast is rejected
81        // bd = (BD<Number>) bb; // <<warn>> <<todo: cast-infer>>
82    }
83
84    // --- Transferring parameters via supertypes ---
85
86    private interface CA<T> { }
87    private interface CB<T> extends CA<T> { }
88    private interface CC<T> extends CA<T> { }
89
90    private class CD<T> implements CB<T> { }
91    private interface CE<T> extends CC<T> { }
92
93    private interface CF<S> { }
94    private interface CG<T> { }
95    private class CH<S, T> implements CF<S>, CG<T> { }
96    private interface CI<S> extends CF<S> { }
97    private interface CJ<T> extends CG<T> { }
98    private interface CK<S, T> extends CI<S>, CJ<T> { }
99
100    private void supertypeParameterTransfer() {
101        Object o;
102        o = (CE<String>) (CD<String>) null; // <<pass>>
103
104        // 4916622: unnecessary warning with cast
105        // o = (CH<String, Integer>) (CK<String, Integer>) null; // <<pass>> <<todo: cast-infer>>
106    }
107
108    // --- Disjoint ---
109
110    private interface DA<T> { }
111    private interface DB<T> extends DA<T> { }
112    private interface DC<T> extends DA<Integer> { }
113
114    private <N extends Number, I extends Integer, R extends Runnable, S extends String> void disjointness() {
115        Object o;
116
117        // Classes
118        o = (DA<? extends Number>) (DA<Integer>) null; // <<pass>>
119        o = (DA<? super Integer>) (DA<Number>) null; // <<pass>>
120        o = (DA<?>) (DA<Integer>) null; // <<pass>>
121        o = (DA<?>) (DA<? extends Integer>) null; // <<pass>>
122        o = (DA<?>) (DA<? super String>) null; // <<pass>>
123        o = (DA<?>) (DA<?>) null; // <<pass>>
124
125        // Typevars
126        o = (DA<? extends Number>) (DA<I>) null; // <<pass>>
127
128        // Raw (asymmetrical!)
129        o = (DA) (DB<Number>) null; // <<pass>>
130        o = (DA<?>) (DB) null; // <<pass>>
131        o = (DA<? extends Object>) (DB) null; // <<pass>>
132        o = (DB) (DA<Number>) null; // <<pass>>
133        o = (DB<?>) (DA) null; // <<pass>>
134        o = (DB<? extends Object>) (DA) null; // <<pass>>
135        o = (DC<?>) (DA<?>) null; // <<pass>>
136    }
137
138}
139