getAnnotationsByType() in the presence of both repeating and container
Joel Borggrén-Franck
joel.franck at oracle.com
Wed Mar 20 07:02:54 PDT 2013
Hi Experts,
Consider the following declaration in some existing code:
@Foo(1) @FooContainer({@Foo(2), @Foo(3)})
class SomeClass {}
If the annotation type Foo is converted to be repeatable and FooContainer is the containing type for Foo, we believe that the call
SomeClass.class.getAnnotationsByType(Foo.class)
is most useful if it returns the array of the merged annotations:
[ @Foo(1), @Foo(2), @Foo(3) ]
in their natural order (@Foo(1) before @Foo(2) because @Foo(1) occurs before @FooContainer(…) on the declaration).
(Of course this should be the case for non-legacy code as well, but we suspect it will be much more common in code dating to before repeating annotations.)
Alex pointed out to me that this is currently not according to spec, because:
> getAnnotationsByType(Class) relies on the definition of "present", which in turn relies on "directly present", which is:
>
> "An annotation A is directly present on an element E if ... for an invocation of get[Declared]AnnotationsByType(Class<T>), the attribute either contains A or, if the type of A is repeatable, contains exactly one annotation whose value element contains A and whose type is the containing annotation type of A's type (§9.6)."
>
> The either-or in this text does not really admit the merging of naked and contained @Foo's.
So we propose to change the spec to require this behaviour. My interpretation of the current wording is that the array [ @Foo(1) ] should be returned, but we think this is less useful than the merged array. Due to backward compatibility it is not an option to make the combination an error at reflect time.
What do you think?
cheers
/Joel
More information about the enhanced-metadata-spec-discuss
mailing list