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

Java example source code file (LintCast.java)

This example Java source code file (LintCast.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

annotation, inner, lintcast, list, object, outer, string, target, util

The LintCast.java Java example source code

import java.lang.annotation.*;
import java.util.List;

/*
 * @test /nodynamiccopyright/
 * @bug 6843077 8006775
 * @summary test that compiler doesn't warn about annotated redundant casts
 * @author Mahmood Ali
 * @author Werner Dietl
 * @compile/ref=LintCast.out -Xlint:cast -XDrawDiagnostics LintCast.java
 */
class LintCast {
    void unparameterized() {
        String s = "m";
        String s1 = (String)s;
        String s2 = (@A String)s;
    }

    void parameterized() {
        List<String> l = null;
        List<String> l1 = (List)l;
        List<String> l2 = (List<@A String>)l;
    }

    void array() {
        int @A [] a = null;
        int[] a1 = (int[])a;
        int[] a2 = (int @A [])a;
    }

    void sameAnnotations() {
        @A String annotated = null;
        String unannotated = null;

        // compiler ignore annotated casts even if redundant
        @A String anno1 = (@A String)annotated;

        // warn if redundant without an annotation
        String anno2 = (String)annotated;
        String unanno2 = (String)unannotated;
    }

    void more() {
        Object @A [] a = null;
        Object[] a1 = (Object[])a;
        Object[] a2 = (Object @A [])a;

        @A List<String> l3 = null;
        List<String> l4 = (List)l3;
        List<String> l5 = (@A List)l3;

        List<@A String> l6 = null;
        List<String> l7 = (List)l6;
        List<String> l8 = (List<@A String>)l6;

        @A Object o = null;
        Object o1 = (Object)o;
        Object o2 = (@A Object)o;

        Outer. @A Inner oi = null;
        Outer.Inner oi1 = (Outer.Inner)oi;
        Outer.Inner oi2 = (Outer. @A Inner)oi;
    }

    class Outer { class Inner {} }
}

@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface A { }

Other Java examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.