TYPE_USE annotations on scoping constructs

Alex Buckley alex.buckley at oracle.com
Wed May 27 16:57:40 UTC 2020


On 5/27/2020 4:22 AM, Anna Kozlova wrote:
> javac accepts the following code:
> 
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Target;
> @Target(ElementType.TYPE_USE)
> @interface Ann { }
> class A<T> {}
> public class App extends A<@Ann App.Data> {//<--
>      public static class Data { }
> }
> 
> though fails to compile static List<@Ann App.Data> l;
> 
> Looks like A<@Ann App.Data> should also be a compile error?

Yes, because the type name `App` is playing the role of a "scoping 
construct" for the type `Data`.

(In the body of the Data class, the term `App.this` is illegal. Hence, 
the annotation on `App.Data` is not admissible, per JLS 9.7.4.)

Alex


More information about the compiler-dev mailing list