Mark individual array sizes with annotations

In Java 8, we can mark individual array sizes with annotations (see section 10.2 in JLS 8). For example,

int @a[] a; int @a[] @b[] a; void someMethod(int @a[] @b... y) {} 

We can then analyze such declarations using Java Reflection to implement certain logic.

Do you know any practical applications of this function in real Java infrastructures or Java libraries?

+7
java java-8 annotations jls
source share
1 answer

An example of such annotation placement is the Checker Framework .

It can get used to creating mutable / immutable or (non) null strings - basically, whatever you want, to annotate the whole array, but only for one row.

 Object @NonNull [] @Nullable [] a; 

In addition, it can be used for documentation, for example annotations explaining the purpose of each dimension.

+1
source share

All Articles