Somewhere in my code I have following function:
private static Object formatArg(Object arg)
{
Object formatterArg;
if (arg instanceof Collection)
formatterArg = getFormatter().format((Collection) arg, mFormatters);
else if (arg instanceof Object[])
formatterArg = getFormatter().format((Object[]) arg, mFormatters);
else
formatterArg = getFormatter().format(arg, mFormatters, false);
return formatterArg;
}
The problem is this arg instanceof Object[]
. The goal of this is that I want to handle int[]
, long[]
and any primitives arrays as well.
Question
Is it possible to find out of an Object
is an array, no matter of which type and pass it to another function? This should work with arrays of primitive types as well as with an array of Objects
... I can use arg != null && arg.getClass().isArray()
to check it the Object
is an array, but then I don't know how to pass it on (as I can't just cast and int[]
to an Object[]
...). Any tricks to achieve what I want?
Edit
A beautiful solution would automatically convert int[]
to Integer[]
, long[]
to Long[]
and so on. Those converted arrays would work with my instanceof Object[]
, but this means I have to manually check for these primitive types, which I can do, but this is ugly. Any better ideas?