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

Java example source code file (CantAnnotateScoping.java)

This example Java source code file (CantAnnotateScoping.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, arraylist, da2, dta, dta2, list, object, outer, sinner, ta2, target, test, util

The CantAnnotateScoping.java Java example source code

/*
 * @test /nodynamiccopyright/
 * @bug 8006733 8006775
 * @summary Ensure behavior for nested types is correct.
 * @author Werner Dietl
 * @compile/fail/ref=CantAnnotateScoping.out -XDrawDiagnostics CantAnnotateScoping.java
 */

import java.util.List;
import java.util.ArrayList;

import java.lang.annotation.*;

@Target({ElementType.TYPE_USE})
@interface TA {}
@Target({ElementType.TYPE_USE})
@interface TA2 {}

@Target({ElementType.FIELD})
@interface DA {}
@Target({ElementType.FIELD})
@interface DA2 {}

@Target({ElementType.TYPE_USE, ElementType.FIELD})
@interface DTA {}
@Target({ElementType.TYPE_USE, ElementType.FIELD})
@interface DTA2 {}

class Test {
    static class Outer {
        static class SInner {}
    }

    // Legal
    List<Outer. @TA SInner> li;

    // Illegal
    @TA Outer.SInner osi;
    // Illegal
    List<@TA Outer.SInner> aloi;
    // Illegal
    Object o1 = new @TA @DA @TA2 Outer.SInner();
    // Illegal
    Object o = new ArrayList<@TA @DA Outer.SInner>();

    // Illegal: @TA is only a type-use annotation
    @TA java.lang.Object f1;

    // Legal: @DA is only a declaration annotation
    @DA java.lang.Object f2;

    // Legal: @DTA is both a type-use and declaration annotation
    @DTA java.lang.Object f3;

    // Illegal: @TA and @TA2 are only type-use annotations
    @DTA @DA @TA @DA2 @TA2 java.lang.Object f4;

    // Illegal: Do we want one or two messages?
    // 1: @DA in invalid location
    // 2: Not finding class "lang"
    java. @DA lang.Object f5;

    // Illegal: Do we want one or two messages?
    // 1: @DA in invalid location
    // 2: Not finding class "XXX"
    java. @DA XXX.Object f6;

    // Illegal: Can't find class "lang".
    // Would a different error message be desirable?
    java. @TA lang.Object f7;
}

Other Java examples (source code examples)

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