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