references to non-static types from annotations

Liam Miller-Cushon cushon at
Wed Apr 13 18:12:37 UTC 2016

On Wed, Apr 13, 2016 at 10:57 AM, Alex Buckley <alex.buckley at>

> 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.

I had wondered if that was deliberate, because `Test.I` makes the erasure
explicit and `I` does not. But I suppose the class literal makes that
explicit anyways.

And I don't think it would be a significant incompatibility, for what it's
worth. I only found a single example of this in our codebase, and ecj
already rejects @A(I.class).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the compiler-dev mailing list