@NotNull and varargs

Answered

Hello, why in this code :

String[] l = {"A", null, "B"};
f(l);
private void f(@NotNull String... args)
{
for (String i : args)
{
if (i != null) System.out.println(i);
else System.out.println("null");
}
}

i != null is highlighted by the 'Constants conditions & exceptions' inspection ('i != null' always true) ?

Because if i run this code it prints "A null B", thus the second line is reached, thus 'i != null' is not always true.

0
2 comments
Official comment

Because apparently you're using a TYPE_USE-compatible @NotNull annotation, and in JDK 8 this syntax means that the array's elements are required to be notnull. Unfortunately the call isn't currently checked, because "l" is declared separately from "f(l)". If it were a vararg call, IDEA 2017.1 preview would highlight the null.

See also https://youtrack.jetbrains.com/issue/IDEA-167067

Please sign in to leave a comment.