alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (CastWarn14.java)

This example Java source code file (CastWarn14.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

integer, number, object, string, util

The CastWarn14.java Java example source code

/*
 * Copyright (c) 2003, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.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<T> { }

    private class AB<T> extends AA { }
    private class AC<T> extends AA> { }
    private class AD<T> extends AA> { }
    private class AE<T> extends AA> { }
    private class AF<T> extends AA { }
    private class AG<T> extends AA { }

    private void parameterTransfer() {
        Object o;

        o = (AB<String>) (AA) null; // <>
        o = (AB<String>) (AA) null; // <>
        o = (AC<String>) (AA>) null; // <>
        o = (AC<String>) (AA>) null; // <>
        o = (AC<String>) (AA>) null; // <>

        o = (AD<Number>) (AA>) null; // <>
        o = (AD<String>) (AA>) null; // <>
        o = (AD<?>) (AA>) null; // <>
        o = (AD<Object>) (AA>) null; // <>

        o = (AE<String>) (AA>) null; // <>
        o = (AE<Number>) (AA>) null; // <>

        o = (AF<String>) (AA) null; // <>
        o = (AF<String>) (AA) null; // <>

        o = (AG<?>) (AA) null; // <>
        o = (AG<?>) (AA) null; // <>
    }

    // --- Inconsistent matches ---

    private class BA<T> { }
    private class BB<T, S> { }

    private class BC<T> extends BA { }
    private class BD<T> extends BB { }

    private void inconsistentMatches() {
        Object o;

        o = (BC<?>) (BA) null; // <>
        o = (BC<?>) (BA) null; // <>
        o = (BD<String>) (BB) null; // <>
        o = (BD<String>) (BB) null; // <>
        o = (BD<String>) (BB) null; // <>
    }

    private void whyMustEverythingBeSo_______Complicated() {
        // This has to work...
        BD<Number> bd = new BD();
        BB<? extends Number, ? super Integer> bb = bd;
        // 4916620: wildcards: legal cast is rejected
        // bd = (BD<Number>) bb; // <> <>
    }

    // --- Transferring parameters via supertypes ---

    private interface CA<T> { }
    private interface CB<T> extends CA { }
    private interface CC<T> extends CA { }

    private class CD<T> implements CB { }
    private interface CE<T> extends CC { }

    private interface CF<S> { }
    private interface CG<T> { }
    private class CH<S, T> implements CF, CG { }
    private interface CI<S> extends CF { }
    private interface CJ<T> extends CG { }
    private interface CK<S, T> extends CI, CJ { }

    private void supertypeParameterTransfer() {
        Object o;
        CD<?> cd = (CE) null; // <>
        CE<?> ce = (CD) null; // <>
        o = (CE<String>) (CD) null; // <>
        o = (CE<Number>) (CD) null; // <>

        // 4916622: unnecessary warning with cast
        // o = (CH<String, Integer>) (CK) null; // <> <>
    }

    // --- Disjoint ---

    private interface DA<T> { }
    private interface DB<T> extends DA { }
    private interface DC<T> extends DA { }

    private <N extends Number, I extends Integer, R extends Runnable, S extends String> void disjointness() {
        Object o;

        // Classes
        o = (DA<Number>) (DA) null; // <>
        o = (DA<? extends Number>) (DA) null; // <>
        o = (DA<? extends Integer>) (DA) null; // <>
        o = (DA<? super Integer>) (DA) null; // <>
        o = (DA<? super Number>) (DA) null; // <>
        o = (DA<?>) (DA) null; // <>

        o = (DA<? extends Runnable>) (DA) null; // <>
        o = (DA<? extends Runnable>) (DA) null; // <>

        o = (DA<? super Integer>) (DA) null; // <>
        o = (DA<? super Number>) (DA) null; // <>
        o = (DA<?>) (DA) null; // <>

        o = (DA<? super String>) (DA) null; // <>
        o = (DA<?>) (DA) null; // <>

        o = (DA<?>) (DA) null; // <>

        // Typevars
        o = (DA<? extends Number>) (DA) null; // <>
        o = (DA<? extends Integer>) (DA) null; // <>
        o = (DA<? extends String>) (DA) null; // <>

        o = (DA<I>) (DA) null; // <>
        o = (DA<N>) (DA) null; // <>
        o = (DA<N>) (DA) null; // <>

        o = (DA<N>) (DA) null; // <>
        o = (DA<N>) (DA) null; // <>
        o = (DA<S>) (DA) null; // <>

        // Raw (asymmetrical!)
        o = (DA) (DB<Number>) null; // <>
        o = (DA<Number>) (DB) null; // <>
        o = (DA<?>) (DB) null; // <>
        o = (DA<? extends Object>) (DB) null; // <>
        o = (DA<? extends Number>) (DB) null; // <>

        o = (DB) (DA<Number>) null; // <>
        o = (DB<Number>) (DA) null; // <>
        o = (DB<?>) (DA) null; // <>
        o = (DB<? extends Object>) (DA) null; // <>
        o = (DB<? extends Number>) (DA) null; // <>

        o = (DC<?>) (DA) null; // <>
        o = (DC<?>) (DA) null; // <>
    }

}

Other Java examples (source code examples)

Here is a short list of links related to this Java CastWarn14.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.