test/tools/javac/generics/wildcards/pos/CastTest.java

Wed, 13 Aug 2014 14:50:00 -0700

author
katleman
date
Wed, 13 Aug 2014 14:50:00 -0700
changeset 2549
0b6cc4ea670f
parent 554
9d9f26857129
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Added tag jdk8u40-b01 for changeset bf89a471779d

     1 /*
     2  * Copyright (c) 2003, 2004, 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  */
    24 /*
    25  * @test
    26  * @bug 4916607
    27  * @summary Test casts (legal, warning, and errors)
    28  * @author gafter
    29  *
    30  * @compile  -Werror CastTest.java
    31  */
    33 import java.util.*;
    35 class CastTest {
    37     // --- Directly transferring parameters ---
    39     private class AA<T> { }
    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> { }
    48     private void parameterTransfer() {
    49         Object o;
    51         o = (AB<String>) (AA<String>) null; // <<pass>>
    52         o = (AC<String>) (AA<Vector<String>>) null; // <<pass>>
    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>>
    58         o = (AE<String>) (AA<Vector<? super String>>) null; // <<pass>>
    60         o = (AF<String>) (AA<String[]>) null; // <<pass>>
    62         o = (AG<?>) (AA<String>) null; // <<pass>>
    63     }
    65     // --- Inconsistent matches ---
    67     private class BA<T> { }
    68     private class BB<T, S> { }
    70     private class BC<T> extends BA<Integer> { }
    71     private class BD<T> extends BB<T, T> { }
    73     private void inconsistentMatches() {
    74         Object o;
    76         o = (BC<?>) (BA<Integer>) null; // <<pass>>
    77         o = (BD<String>) (BB<String, String>) null; // <<pass>>
    78     }
    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;
    85         // 4916620: wildcards: legal cast is rejected
    86         // bd = (BD<Number>) bb; // <<warn>> <<todo: cast-infer>>
    87     }
    89     // --- Transferring parameters via supertypes ---
    91     private interface CA<T> { }
    92     private interface CB<T> extends CA<T> { }
    93     private interface CC<T> extends CA<T> { }
    95     private class CD<T> implements CB<T> { }
    96     private interface CE<T> extends CC<T> { }
    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> { }
   105     private void supertypeParameterTransfer() {
   106         Object o;
   107         o = (CE<String>) (CD<String>) null; // <<pass>>
   109         // 4916622: unnecessary warning with cast
   110         // o = (CH<String, Integer>) (CK<String, Integer>) null; // <<pass>> <<todo: cast-infer>>
   111     }
   113     // --- Disjoint ---
   115     private interface DA<T> { }
   116     private interface DB<T> extends DA<T> { }
   117     private interface DC<T> extends DA<Integer> { }
   119     private <N extends Number, I extends Integer, R extends Runnable, S extends String> void disjointness() {
   120         Object o;
   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>>
   127         o = (DA<?>) (DA<? extends Integer>) null; // <<pass>>
   129         o = (DA<?>) (DA<? super String>) null; // <<pass>>
   131         o = (DA<?>) (DA<?>) null; // <<pass>>
   133         // Typevars
   134         o = (DA<? extends Number>) (DA<I>) null; // <<pass>>
   136         // Raw (asymmetrical!)
   137         o = (DA) (DB<Number>) null; // <<pass>>
   138         o = (DA<?>) (DB) null; // <<pass>>
   139         o = (DA<? extends Object>) (DB) null; // <<pass>>
   141         o = (DB) (DA<Number>) null; // <<pass>>
   142         o = (DB<?>) (DA) null; // <<pass>>
   143         o = (DB<? extends Object>) (DA) null; // <<pass>>
   145         // reported broken (5034609)
   146         // o = (DC<?>) (DA<?>) null; // <<pass>>
   147     }
   149 }

mercurial