/* * Copyright 2003 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, * CA 95054 USA or visit www.sun.com if you need additional information or * have any questions. */ /* * @test * @bug 4916607 * @summary Test casts (legal, warning, and errors) * @author gafter * * @compile/fail -Werror -Xlint:unchecked CastWarn14.java */ import java.util.*; class CastTest { // --- Directly transferring parameters --- private class AA { } private class AB extends AA { } private class AC extends AA> { } private class AD extends AA> { } private class AE extends AA> { } private class AF extends AA { } private class AG extends AA { } private void parameterTransfer() { Object o; o = (AB) (AA) null; // <> o = (AB) (AA) null; // <> o = (AC) (AA>) null; // <> o = (AC) (AA>) null; // <> o = (AC) (AA>) null; // <> o = (AD) (AA>) null; // <> o = (AD) (AA>) null; // <> o = (AD) (AA>) null; // <> o = (AD) (AA>) null; // <> o = (AE) (AA>) null; // <> o = (AE) (AA>) null; // <> o = (AF) (AA) null; // <> o = (AF) (AA) null; // <> o = (AG) (AA) null; // <> o = (AG) (AA) null; // <> } // --- Inconsistent matches --- private class BA { } private class BB { } private class BC extends BA { } private class BD extends BB { } private void inconsistentMatches() { Object o; o = (BC) (BA) null; // <> o = (BC) (BA) null; // <> o = (BD) (BB) null; // <> o = (BD) (BB) null; // <> o = (BD) (BB) null; // <> } private void whyMustEverythingBeSo_______Complicated() { // This has to work... BD bd = new BD(); BB bb = bd; // 4916620: wildcards: legal cast is rejected // bd = (BD) bb; // <> <> } // --- Transferring parameters via supertypes --- private interface CA { } private interface CB extends CA { } private interface CC extends CA { } private class CD implements CB { } private interface CE extends CC { } private interface CF { } private interface CG { } private class CH implements CF, CG { } private interface CI extends CF { } private interface CJ extends CG { } private interface CK extends CI, CJ { } private void supertypeParameterTransfer() { Object o; CD cd = (CE) null; // <> CE ce = (CD) null; // <> o = (CE) (CD) null; // <> o = (CE) (CD) null; // <> // 4916622: unnecessary warning with cast // o = (CH) (CK) null; // <> <> } // --- Disjoint --- private interface DA { } private interface DB extends DA { } private interface DC extends DA { } private void disjointness() { Object o; // Classes o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> // Typevars o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> o = (DA) (DA) null; // <> // Raw (asymmetrical!) o = (DA) (DB) null; // <> o = (DA) (DB) null; // <> o = (DA) (DB) null; // <> o = (DA) (DB) null; // <> o = (DA) (DB) null; // <> o = (DB) (DA) null; // <> o = (DB) (DA) null; // <> o = (DB) (DA) null; // <> o = (DB) (DA) null; // <> o = (DB) (DA) null; // <> o = (DC) (DA) null; // <> o = (DC) (DA) null; // <> } }