Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does a @Nullable annotation refer to in case of a varargs parameter?

For the following signature:

public static String format(final String pattern, final @Nullable Object... arguments)

Where does the @Nullable annotation refer to? To the array or to its elements? Is there a possibility to annotate both options (array/elements)?

Are there differences between Java 7 and Java 8 (type annotations)?

like image 993
Gustave Avatar asked Dec 14 '22 12:12

Gustave


1 Answers

Since there still is no standard @Nullable annotation, you are using an annotation of a 3rd party library, thus it depends on how it is declared. Pre-Java 8 annotations aren’t type annotations as that feature doesn’t exist in older versions. Hence, with these annotations you are always annotating the parameter rather than its type. This is usually interpreted by frameworks as “the parameter can be null” which would be the array reference, though the frameworks may treat varargs specially, but there is no standard mandating a particular behavior.

When you are using a real Java 8 type annotation, i.e. an annotation with @Target(ElementType.TYPE_USE), the outcome in best explained by using the ordinary array declaration:

@Nullable ElementType  []

declares an array of @Nullable ElementType, read nullable elements whereas

ElementType  @Nullable []

declare a nullable array type.

thus, the declaration

public static String format(final String pattern, final @Nullable Object... arguments)

implies that the elements can be null whereas

public static String format(final String pattern, final Object @Nullable... arguments)

declares that the array reference may be null. Of course, you may combine them to declare that both, the elements or the array itself, can be null

public static String format(String pattern, @Nullable Object @Nullable ... arguments)

As said, to use these options, you need an annotation type which is really declared as a Java 8 type annotation. Then, to enforce the described semantics you need an up-to-date checker framework implementation compatible with Java 8 type annotations.

like image 110
Holger Avatar answered Jan 22 '23 00:01

Holger