Type annotations and arrays

Alex Buckley alex.buckley at oracle.com
Fri Apr 21 20:19:35 UTC 2017


On 4/20/2017 1:10 AM, Gunnar Morling wrote:
> For arrays the annotation on the declaration is ambiguous, though:
>
>      @Size(max=10) String[] arr;
>
> Before the advent of type annotations it was clear that the annotation
> applies to the array.
>
> Now, as @Size supports both element types, FIELD and TYPE_USE, it
> appears here as both, as a declaration annotation and as a type
> annotation. I.e. for Bean Validation it's ambiguous whether it should
> apply to the array or the array elements.
>
> We could mandate that the usage above only is considered as type
> annotation, as the annotation can be applied to the array like so:
>
>      String @Size(max=10) [] arr;
>
> But that'd break existing clients of Bean Validation so it's not an
> option we really have (as the semantics of giving the annotation on
> the declaration would change from "apply to the array" to "apply to
> the array elements").
>
> So it seems the introduction of type annotations caused a migration
> concern for existing annotation-based APIs when it comes to arrays.

The unfortunate syntax for array types in Java, and the difficulty of a 
"best" order for the type annotations thereon, were discussed at length 
in the lifetime of JSR 308.

> The only backwards-compatible way out I can see is to add a member to
> @Size stating its target, allowing to disambiguate the issue:
>
>      @Size(max=10, appliesTo=ARRAY_ELEMENTS) String[] arr;
>
> Are there other options we've missed?

Given that you want:

   @Size(max=10) String[] arr;

to constrain the size of the array, and not the size of each String in 
the array, I think I'd recommend a different TYPE_USE annotation type 
altogether for the new functionality of constraining the size of each 
String in the array. And I'd enforce that annotations of this new type 
are written in the "proper" location, on the element type only:

   @Size(max=10) @ElementSize(max=10) String[] arr;

Alex


More information about the compiler-dev mailing list