1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/generics/wildcards/pos/CastTest.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,149 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +/* 1.28 + * @test 1.29 + * @bug 4916607 1.30 + * @summary Test casts (legal, warning, and errors) 1.31 + * @author gafter 1.32 + * 1.33 + * @compile -Werror CastTest.java 1.34 + */ 1.35 + 1.36 +import java.util.*; 1.37 + 1.38 +class CastTest { 1.39 + 1.40 + // --- Directly transferring parameters --- 1.41 + 1.42 + private class AA<T> { } 1.43 + 1.44 + private class AB<T> extends AA<T> { } 1.45 + private class AC<T> extends AA<Vector<T>> { } 1.46 + private class AD<T> extends AA<Vector<? extends T>> { } 1.47 + private class AE<T> extends AA<Vector<? super T>> { } 1.48 + private class AF<T> extends AA<T[]> { } 1.49 + private class AG<T> extends AA<String> { } 1.50 + 1.51 + private void parameterTransfer() { 1.52 + Object o; 1.53 + 1.54 + o = (AB<String>) (AA<String>) null; // <<pass>> 1.55 + o = (AC<String>) (AA<Vector<String>>) null; // <<pass>> 1.56 + 1.57 + o = (AD<Number>) (AA<Vector<? extends Number>>) null; // <<pass>> 1.58 + o = (AD<?>) (AA<Vector<? extends Object>>) null; // <<pass>> 1.59 + o = (AD<Object>) (AA<Vector<?>>) null; // <<pass>> 1.60 + 1.61 + o = (AE<String>) (AA<Vector<? super String>>) null; // <<pass>> 1.62 + 1.63 + o = (AF<String>) (AA<String[]>) null; // <<pass>> 1.64 + 1.65 + o = (AG<?>) (AA<String>) null; // <<pass>> 1.66 + } 1.67 + 1.68 + // --- Inconsistent matches --- 1.69 + 1.70 + private class BA<T> { } 1.71 + private class BB<T, S> { } 1.72 + 1.73 + private class BC<T> extends BA<Integer> { } 1.74 + private class BD<T> extends BB<T, T> { } 1.75 + 1.76 + private void inconsistentMatches() { 1.77 + Object o; 1.78 + 1.79 + o = (BC<?>) (BA<Integer>) null; // <<pass>> 1.80 + o = (BD<String>) (BB<String, String>) null; // <<pass>> 1.81 + } 1.82 + 1.83 + private void whyMustEverythingBeSo_______Complicated() { 1.84 + // This has to work... 1.85 + BD<Number> bd = new BD<Number>(); 1.86 + BB<? extends Number, ? super Integer> bb = bd; 1.87 + 1.88 + // 4916620: wildcards: legal cast is rejected 1.89 + // bd = (BD<Number>) bb; // <<warn>> <<todo: cast-infer>> 1.90 + } 1.91 + 1.92 + // --- Transferring parameters via supertypes --- 1.93 + 1.94 + private interface CA<T> { } 1.95 + private interface CB<T> extends CA<T> { } 1.96 + private interface CC<T> extends CA<T> { } 1.97 + 1.98 + private class CD<T> implements CB<T> { } 1.99 + private interface CE<T> extends CC<T> { } 1.100 + 1.101 + private interface CF<S> { } 1.102 + private interface CG<T> { } 1.103 + private class CH<S, T> implements CF<S>, CG<T> { } 1.104 + private interface CI<S> extends CF<S> { } 1.105 + private interface CJ<T> extends CG<T> { } 1.106 + private interface CK<S, T> extends CI<S>, CJ<T> { } 1.107 + 1.108 + private void supertypeParameterTransfer() { 1.109 + Object o; 1.110 + o = (CE<String>) (CD<String>) null; // <<pass>> 1.111 + 1.112 + // 4916622: unnecessary warning with cast 1.113 + // o = (CH<String, Integer>) (CK<String, Integer>) null; // <<pass>> <<todo: cast-infer>> 1.114 + } 1.115 + 1.116 + // --- Disjoint --- 1.117 + 1.118 + private interface DA<T> { } 1.119 + private interface DB<T> extends DA<T> { } 1.120 + private interface DC<T> extends DA<Integer> { } 1.121 + 1.122 + private <N extends Number, I extends Integer, R extends Runnable, S extends String> void disjointness() { 1.123 + Object o; 1.124 + 1.125 + // Classes 1.126 + o = (DA<? extends Number>) (DA<Integer>) null; // <<pass>> 1.127 + o = (DA<? super Integer>) (DA<Number>) null; // <<pass>> 1.128 + o = (DA<?>) (DA<Integer>) null; // <<pass>> 1.129 + 1.130 + o = (DA<?>) (DA<? extends Integer>) null; // <<pass>> 1.131 + 1.132 + o = (DA<?>) (DA<? super String>) null; // <<pass>> 1.133 + 1.134 + o = (DA<?>) (DA<?>) null; // <<pass>> 1.135 + 1.136 + // Typevars 1.137 + o = (DA<? extends Number>) (DA<I>) null; // <<pass>> 1.138 + 1.139 + // Raw (asymmetrical!) 1.140 + o = (DA) (DB<Number>) null; // <<pass>> 1.141 + o = (DA<?>) (DB) null; // <<pass>> 1.142 + o = (DA<? extends Object>) (DB) null; // <<pass>> 1.143 + 1.144 + o = (DB) (DA<Number>) null; // <<pass>> 1.145 + o = (DB<?>) (DA) null; // <<pass>> 1.146 + o = (DB<? extends Object>) (DA) null; // <<pass>> 1.147 + 1.148 + // reported broken (5034609) 1.149 + // o = (DC<?>) (DA<?>) null; // <<pass>> 1.150 + } 1.151 + 1.152 +}