Annotations on anonymous classes

Werner Dietl wdietl at gmail.com
Tue Feb 12 16:17:39 PST 2013


I have a question about anonymous classes and want to make sure we're
all on the same page on where type and declaration annotations should
go.

Let's take this code:

    interface Bound<X extends Object> {}

    void test() {
        new Bound<String>() { };
    }

This is equivalent to having:

    interface Bound<X extends Object> {}

    class Anon extends Object implements Bound<String> {}

    void test() {
        new Anon();
    }

Now, let's take a declaration annotation @DA and type annotations @TA
and @TB, and use them like this:

    interface Bound<X extends Object> {}

    void test() {
        new @DA @TA Bound<@TB String>() { };
    }

This should be translated to:

    interface Bound<X extends Object> {}

    @DA
    class Anon extends Object implements @TA Bound<@TB String> {}

    void test() {
        new @TA Anon();
    }

Note that I have @TA both on the implemented interface and on the
instantiation and @DA only on the class declaration.
Also note how @TB is not stored at the instantiation at all (this
might be surprising; if Bound were a class an instantiation "new @TA
Bound<@TB String>()" would obviously store both @TA and @TB with the
instantiation).

Is this the right way to interpret these annotations?
Should the specification explicitly state what the translation should be?

Thanks,
cu, WMD.

-- 
http://www.google.com/profiles/wdietl


More information about the type-annotations-spec-comments mailing list