Type annotations and arrays

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Type annotations and arrays

Gunnar Morling
Hi,

Working on the Bean Validation 2 specification we came across an
ambiguity when using type annotations with arrays.

Bean Validation 2 uses type annotations to put constraints to the
elements of container types. This works great for generic types:

    // the list must contain 10 elements at most
    @Size(max=10)
    List<String> list;

    // each String in the list must have 10 chars at most
    List<@Size(max=10) String> list;

If the @Size annotation is given on the declaration, it applies to the
list, if is given on the type argument, it applies to the elements in
the list.

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

Thanks,

--Gunnar
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Type annotations and arrays

Alex Buckley-3
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
Loading...