langtools/test/tools/javac/CheckNoClassCastException.java
author jlahoda
Mon, 02 Feb 2015 18:52:16 +0530
changeset 28800 35f9042d53b3
permissions -rw-r--r--
8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK. Summary: Alternate fix for 8015499 to eliminate extra checkcast (by working around a limitation in Types#asSuper) without the side effect that results in 8069265 Reviewed-by: mcimadamore, jjg Contributed-by: srikanth.adayapalam@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28800
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     1
/*
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     4
 *
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     8
 *
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    13
 * accompanied this code).
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    14
 *
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    18
 *
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    21
 * questions.
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    22
 */
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    23
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    24
/*
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    25
 * @test
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    26
 * @bug 8069265
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    27
 * @summary ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    28
 * @run main CheckNoClassCastException
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    29
 */
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    30
import java.util.*;
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    31
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    32
public class CheckNoClassCastException {
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    33
    static String result = "";
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    34
    public static void main(String[] args) {
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    35
        ListFail.main(null);
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    36
        MapFail.main(null);
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    37
        if (!result.equals("ListFailDoneMapFailDone"))
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    38
            throw new AssertionError("Incorrect result");
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    39
    }
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    40
}
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    41
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    42
class ListFail {
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    43
    static interface Foo {
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    44
    }
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    45
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    46
    public static void main(String[] args) {
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    47
        List<Date> list = new ArrayList<>();
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    48
        list.add(new Date());
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    49
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    50
        List<Foo> cList = (List<Foo>) (List<?>) list;
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    51
        Date date = (Date) cList.get(0);
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    52
        CheckNoClassCastException.result += "ListFailDone";
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    53
    }
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    54
}
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    55
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    56
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    57
class MapFail {
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    58
    static interface Foo {
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    59
    }
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    60
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    61
    public static void main(String[] args) {
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    62
        Map<String,Date> aMap = new HashMap<>();
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    63
        aMap.put("test",new Date());
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    64
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    65
        Map<String,Foo> m = (Map<String,Foo>) (Map<?,?>) aMap;
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    66
        Date q = (Date) m.get("test");
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    67
        CheckNoClassCastException.result += "MapFailDone";
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    68
    }
35f9042d53b3 8069265: ClassCastException when compiled with JDK 9b08+, JDK8 compiles OK.
jlahoda
parents:
diff changeset
    69
}