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

Java example source code file (RepeatingTypeAnnotations.java)

This example Java source code file (RepeatingTypeAnnotations.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, mylist, object, repeatingtypeannotations, rta, rtas, string, target, tas

The RepeatingTypeAnnotations.java Java example source code

/*
 * Copyright (c) 2012, 2013, 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.
 */

import java.lang.annotation.*;

/*
 * @test
 * @bug 8006775
 * @summary repeating type annotations are possible
 * @author Werner Dietl
 * @compile/fail/ref=RepeatingTypeAnnotations.out -XDrawDiagnostics RepeatingTypeAnnotations.java
 */

class RepeatingTypeAnnotations {
    // Fields
    @RTA @RTA Object fr1 = null;
    Object fr2 = new @RTA @RTA Object();
    // error
    Object fs = new @TA @TA Object();
    // error
    Object ft = new @TA @TA Object();
    Object fe = new @TA @TA Object();

    // Local variables
    Object foo() {
        Object o = new @RTA @RTA Object();
        o = new @TA @RTA @RTA Object();
        o = new @RTA @TA @RTA Object();
        // error
        o = new @RTA @TA @RTA @TA Object();
        // error
        return new @TA @TA Object();
    }

    // Instance creation
    Object bar() {
        Object o = new @RTA @RTA MyList<@RTA @RTA Object>();
        o = new @TA @RTA MyList<@TA @RTA Object>();
        o = new @TA @RTA @RTA MyList<@RTA @TA @RTA Object>();
        // error
        o = new @TA @TA MyList<@RTA @RTA Object>();
        // error
        o = new @RTA @RTA MyList<@TA @TA Object>();
        // error
        return new @TA @TA MyList<@RTA @RTA Object>();
    }

    // More tests
    void oneArg() {
        Object o = new @RTA @RTA Object();
        // error
        o = new @TA @TA Object();
        o = new @RTA @TA @RTA Object();

        o = new MyList<@RTA @RTA Object>();
        // error
        o = new MyList<@TA @TA Object>();
        // error
        o = new @TA @TA MyList<@TA @TA Object>();
        // error
        this.<@TA @TA String>newList();

        this.<@RTA @RTA MyList<@RTA @RTA String>>newList();
        // error
        this.<@TA @TA MyList<@TA @TA String>>newList();

        o = (@RTA @RTA MyList<@RTA @RTA Object>) o;
        // error
        o = (@TA @TA MyList<@TA @TA Object>) o;

        this.<@RTA @RTA String, @RTA @RTA Object>newMap();
        // error
        this.<@TA @TA String, @TA @TA Object>newMap();

        this.<@RTA @RTA String @RTA @RTA []>newList();
        // error
        this.<@TA @TA String @TA @TA []>newList();

        this.<@RTA @RTA String @RTA @RTA [] @RTA @RTA [], MyList<@RTA @RTA String> @RTA @RTA []>newMap();
        // error
        this.<String @TA @TA [] @TA @TA [], MyList<@TA @TA String> @TA @TA []>newMap();
    }

    static <E> void newList() { }
    static <K, V> void newMap() { }
}

class MyList<E> { }


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

@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface TAs {
    TA[] value();
}

@Repeatable(RTAs.class)
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface RTA { }

@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
@interface RTAs {
    RTA[] value();
}

Other Java examples (source code examples)

Here is a short list of links related to this Java RepeatingTypeAnnotations.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.