Search code examples
javajunit4variadic-functionssuppress-warnings

@SuppressWarnings for the invocation of the varargs with un-cast null


What string should I use in @SuppressWarnings("....") to remove the warning I get in this unit test of a method with varargs:

/**
 * Calling {@link #readFiles(String, String...)} with just one arg and a null
 * works, but you have to do a null check on the varargs in case it is null.
 */
@Test
public void callMethodWithOneArgAndNull() {
   readFiles("C:/Temp", null);
}

Eclipse gives the following warning for this:

The argument of type null should explicitly be cast to String[] for the invocation of the varargs method readFiles(String, String...) from type TestVarArgs. It could alternatively be cast to String for a varargs invocation

This is good; it is expected. I am also unit testing what happens when I cast the null.

But for the purposes of this single test, I want to suppress that specific warning, i.e. I don't want to use @SuppressWarnings("all"). I have tried every string mentioned in this answer to What is the list of valid @SuppressWarnings warning names in Java? and none of them seem to catch this warning.


Solution

  • To my understanding, this is not a warning you can suppress with a specific annotation. You have to make an explicit cast to make the warning go away - or suppress all warnings.

    Also, consider testing both possible casts, i.e. (String[]) {null} and (String) null, to make sure your method doesn't choke on "no files" nor on a "null filename" input.