This answer is quite late, but hopefully it helps future visitors to this questions.
My take on a reflective solution can be improved but the basic idea is this:
@SuppressWarnings("unchecked")
private static <TArray, TElement> TArray toArray(final Object elements, final Class<TElement> elementType)
{
final int length = java.lang.reflect.Array.getLength(elements);
final Object resultArray = java.lang.reflect.Array.newInstance(elementType, length);
for (int i = 0; i < length; ++i)
{
final Object value = java.lang.reflect.Array.get(elements, i);
java.lang.reflect.Array.set(resultArray, i, value);
}
return (TArray)resultArray;
}
Then use as following:
final Boolean[] booleans = new Boolean[] { true, false, true, true, false };
final boolean[] primitives = toArray(booleans, boolean.class);
System.out.println(Arrays.asList(booleans));
for (boolean b : primitives)
{
System.out.print(b);
System.out.print(", ");
}
This prints out:
[true, false, true, true, false]
true, false, true, true, false,
This utility helps us pull arrays out of java.sql.Array
, because java.sql.Array
only gives us an Object
for the array.