Guaranteed order of annotations?

Joseph D. Darcy joe.darcy at oracle.com
Wed Jan 18 02:13:00 UTC 2017


Hello,

Following up on Joel's message, IIRC this ordering issue did come up 
during the design and testing of repeating annotations.

As noted previously in the thread, while the ordering of the annotations 
in a container annotation is guaranteed, there is *not* a general 
ordering constraint on the annotations returned by the methods of the 
AnnotatedElement interface.

HTH,

-Joe

On 1/13/2017 1:16 PM, Gunnar Morling wrote:
> Hi Yuri,
>
> Thanks for the pointer.
>
> But do you see any clear description of an order mandated for
> getDeclaredAnnotations()? The only references to an order I can find
> there are for repeatable annotations (where source code order is
> maintained when retrieving them via the container annotation type).
> But I cannot find any guaranteed order when obtaining all declared
> annotations from an element.
>
> The reason why I'm asking is Bean Validation, where we've seen
> requests of people that wish to validate the constraints of an element
> in a fixed order, aborting after the first failed constraint:
>
>      @NotNull
>      @Email
>      String email;
>
> Here one may want to first validate @NotNull and don't proceed with
> validating @Email if the field is null.
>
> Relying on source order would be a very natural way to express the
> order of constraints. Without a guaranteed ordering of annotations
> we'd have to add some other means of ordering, e.g. an attribute with
> the index (@NotNull(order=0) @Email(order=1)) which is more verbose of
> course.
>
> --Gunnar
>
>
>
>
>
> 2017-01-13 21:15 GMT+01:00 Yuri Gaevsky <ygaevsky at azulsystems.com>:
>> Hi Gunnar,
>>
>> Please take a look at JDK-8010679 'Clarify "present" and annotation ordering in Core Reflection for Annotations' [*].
>>
>> Best regards,
>> -Yuri
>>
>> [*] https://bugs.openjdk.java.net/browse/JDK-8010679
>>
>>
>> -----Original Message-----
>> From: core-libs-dev [mailto:core-libs-dev-bounces at openjdk.java.net] On Behalf Of Gunnar Morling
>> Sent: Friday, January 13, 2017 08:14 PM
>> To: core-libs-dev at openjdk.java.net
>> Subject: Guaranteed order of annotations?
>>
>> Hi,
>>
>> Is there any order guaranteed in which an element's annotations are
>> returned by AnnotatedElement#getDeclaredAnnotations()? Specifically,
>> is this the order in which the annotations are given in the source
>> code?
>>
>> Section 9.7.5. of the JLS ("Multiple Annotations of the Same Type")
>> makes a statement of the source order being considered by the array of
>> the implicit container annotation created for a repeatable annotation
>> ("[...] all the base annotations in the left-to-right order in which
>> they appeared in the context").
>>
>> But I couldn't find any authoritative description on the ordering
>> behaviour of getDeclaredAnnotations(). Should we assume that we cannot
>> rely on the order in the source?
>>
>> Thanks for any pointers,
>>
>> --Gunnar



More information about the core-libs-dev mailing list