references to non-static types from annotations

Alex Buckley alex.buckley at oracle.com
Wed Apr 13 17:57:39 UTC 2016


On 4/13/2016 10:37 AM, Liam Miller-Cushon wrote:
> On Wed, Apr 13, 2016 at 10:15 AM, Alex Buckley <alex.buckley at oracle.com
> <mailto:alex.buckley at oracle.com>> wrote:
>
>
>     To clarify, JLS 9.7.1 does not prohibit an annotation element in a
>     static context from referring to a non-static class. I don't see why
>     such a prohibition would be necessary, yet it sounds like you want
>     javac to reject @A(I.class).
>
>     The emission of an invalid return descriptor for class_info_index is
>     "just" a javac issue, due to not erasing the fully qualified generic
>     type of I. If the class literal does the erasure itself --
>     @A(Test.I.class) interface C {} -- then the program compiles and
>     runs without error.
>
> Thanks for clarifying. I assumed it was supposed to be consistent with
> other expressions in static contexts.

I know what you mean, but the fact that I can write @A(Test.I.class) 
today means making it consistent would be a serious source incompatibility.

> Is it correct that the following is currently rejected?
>
> class Test<T> {
>    class I {}
>    static final Class<?> C = I.class;
> }
>
> error: non-static class Test.I cannot be referenced from a static context

Good question. This program should not be rejected. No JLS rule 
prohibits I.class in a static context. Static contexts pertain mostly to 
object creation and constructor invocation, but a class literal is a 
rather different kind of expression. Again, the program compiles and 
runs without error if the source code mentions Test.I.class rather than 
I.class.

Alex


More information about the compiler-dev mailing list