Type annotations and arrays

Gunnar Morling gunnar at hibernate.org
Thu Apr 20 08:10:22 UTC 2017


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


More information about the compiler-dev mailing list