I have declared the following method:
private void mockInvokeDBHandler(Map<String, Object>... rows) {
List<Map<String, Object>> allRows = Arrays.asList(rows));
// rest of method omitted
}
It is invoked by clients using something like
Map<String, Object> row1 = new HashMap<String, Object>();
Map<String, Object> row2 = new HashMap<String, Object>();
mockInvokeDBHandler(row1, row2);
However, the last line shown above generates a warning
Type safety : A generic array of Map is created for a varargs parameter
I don’t fully understand this, but I guess it’s because varargs params are converted to arrays, and it’s a bad idea to have an array whose type is a generic class (because generics are invariant, whereas arrays aren’t).
I could resolve this problem by redifining the method as
private void mockInvokeDBHandler(List<Map<String, Object>> rows) {
}
But this places the burden of putting the row objects into a List on the client, which I’d rather avoid. Is there a better solution?
To pass the arguments to a varargs method the compiler will place the arguments into an array.
The warning is to let you know that the compiler cannot guarantee that each of the elements in the array – each of the arguments to the varags method – is truly a
Map<String, Object>.This is a bit of an annoying warning because there is no way you can work around this, other than to redefine the method signature to not use varargs. IMO it is safe to ignore as long as you are pretty sure of the actual run-time types of these arguments (which in this case, you are).