langtools/test/tools/javac/generics/wildcards/neg/CastWarn14.java
author darcy
Wed, 27 May 2009 22:34:43 -0700
changeset 2985 f43e1241e7fb
parent 10 06bc494ca11e
child 5520 86e4b9a9da40
permissions -rw-r--r--
6843761: Update langtools tests to remove unncessary -source and -target options Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003 Sun Microsystems, Inc.  All Rights Reserved.
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @bug 4916607
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary Test casts (legal, warning, and errors)
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @author gafter
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 *
2985
f43e1241e7fb 6843761: Update langtools tests to remove unncessary -source and -target options
darcy
parents: 10
diff changeset
    30
 * @compile/fail  -Werror -Xlint:unchecked CastWarn14.java
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
class CastTest {
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
    // --- Directly transferring parameters ---
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
    private class AA<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
    private class AB<T> extends AA<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    private class AC<T> extends AA<Vector<T>> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    private class AD<T> extends AA<Vector<? extends T>> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
    private class AE<T> extends AA<Vector<? super T>> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    private class AF<T> extends AA<T[]> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    private class AG<T> extends AA<String> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
    private void parameterTransfer() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        Object o;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
        o = (AB<String>) (AA<String>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        o = (AB<String>) (AA<Number>) null; // <<fail 1>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
        o = (AC<String>) (AA<Vector<String>>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        o = (AC<String>) (AA<Vector<Number>>) null; // <<fail 2>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
        o = (AC<String>) (AA<Stack<String>>) null; // <<fail 3>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        o = (AD<Number>) (AA<Vector<? extends Number>>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
        o = (AD<String>) (AA<Vector<? extends Number>>) null; // <<fail 4>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        o = (AD<?>) (AA<Vector<? extends Object>>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        o = (AD<Object>) (AA<Vector<?>>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        o = (AE<String>) (AA<Vector<? super String>>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        o = (AE<Number>) (AA<Vector<? super String>>) null; // <<fail 5>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        o = (AF<String>) (AA<String[]>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        o = (AF<String>) (AA<Number[]>) null; // <<fail 6>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        o = (AG<?>) (AA<String>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        o = (AG<?>) (AA<Number>) null; // <<fail 7>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    // --- Inconsistent matches ---
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    private class BA<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    private class BB<T, S> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    private class BC<T> extends BA<Integer> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    private class BD<T> extends BB<T, T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    private void inconsistentMatches() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        Object o;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        o = (BC<?>) (BA<Integer>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        o = (BC<?>) (BA<String>) null; // <<fail 8>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        o = (BD<String>) (BB<String, String>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        o = (BD<String>) (BB<String, Number>) null; // <<fail 9>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        o = (BD<String>) (BB<Number, String>) null; // <<fail 10>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
    private void whyMustEverythingBeSo_______Complicated() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        // This has to work...
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
        BD<Number> bd = new BD<Number>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        BB<? extends Number, ? super Integer> bb = bd;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        // 4916620: wildcards: legal cast is rejected
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        // bd = (BD<Number>) bb; // <<warn>> <<todo: cast-infer>>
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    // --- Transferring parameters via supertypes ---
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    private interface CA<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
    private interface CB<T> extends CA<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    private interface CC<T> extends CA<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    private class CD<T> implements CB<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    private interface CE<T> extends CC<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    private interface CF<S> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    private interface CG<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    private class CH<S, T> implements CF<S>, CG<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    private interface CI<S> extends CF<S> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    private interface CJ<T> extends CG<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    private interface CK<S, T> extends CI<S>, CJ<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    private void supertypeParameterTransfer() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        Object o;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        CD<?> cd = (CE<?>) null; // <<fail 11>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        CE<?> ce = (CD<?>) null; // <<fail 12>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        o = (CE<String>) (CD<String>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        o = (CE<Number>) (CD<String>) null; // <<fail 13>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        // 4916622: unnecessary warning with cast
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        // o = (CH<String, Integer>) (CK<String, Integer>) null; // <<pass>> <<todo: cast-infer>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    // --- Disjoint ---
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    private interface DA<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    private interface DB<T> extends DA<T> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    private interface DC<T> extends DA<Integer> { }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    private <N extends Number, I extends Integer, R extends Runnable, S extends String> void disjointness() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        Object o;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        // Classes
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        o = (DA<Number>) (DA<Integer>) null; // <<fail 14>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        o = (DA<? extends Number>) (DA<Integer>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        o = (DA<? extends Integer>) (DA<Number>) null; // <<fail 15>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        o = (DA<? super Integer>) (DA<Number>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
        o = (DA<? super Number>) (DA<Integer>) null; // <<fail 16>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        o = (DA<?>) (DA<Integer>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        o = (DA<? extends Runnable>) (DA<? extends Number>) null; // <<warn 2>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        o = (DA<? extends Runnable>) (DA<? extends String>) null; // <<fail 17>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        o = (DA<? super Integer>) (DA<? extends Number>) null; // <<warn 3>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        o = (DA<? super Number>) (DA<? extends Integer>) null; // <<fail 18>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
        o = (DA<?>) (DA<? extends Integer>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        o = (DA<? super String>) (DA<? super Number>) null; // <<warn 4>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        o = (DA<?>) (DA<? super String>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        o = (DA<?>) (DA<?>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        // Typevars
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        o = (DA<? extends Number>) (DA<I>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        o = (DA<? extends Integer>) (DA<N>) null; // <<warn 5>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        o = (DA<? extends String>) (DA<I>) null; // <<fail 19>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        o = (DA<I>) (DA<? extends Number>) null; // <<warn 6>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        o = (DA<N>) (DA<? extends Integer>) null; // <<warn 7>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        o = (DA<N>) (DA<? extends Runnable>) null; // <<warn 8>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        o = (DA<N>) (DA<I>) null; // <<warn 9>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        o = (DA<N>) (DA<R>) null; // <<warn 10>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        o = (DA<S>) (DA<R>) null; // <<fail 20>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
        // Raw (asymmetrical!)
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
        o = (DA) (DB<Number>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        o = (DA<Number>) (DB) null; // <<warn 11>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        o = (DA<?>) (DB) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        o = (DA<? extends Object>) (DB) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        o = (DA<? extends Number>) (DB) null; // <<warn 12>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        o = (DB) (DA<Number>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        o = (DB<Number>) (DA) null; // <<warn 13>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        o = (DB<?>) (DA) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
        o = (DB<? extends Object>) (DA) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
        o = (DB<? extends Number>) (DA) null; // <<warn 14>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        o = (DC<?>) (DA<?>) null; // <<pass>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        o = (DC<?>) (DA<? super String>) null; // <<fail 21>>
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
}