Convert the given array (which may be a primitive array) to an object array (if necessary of primitive wrapper objects). A null source value will be converted to an empty Object array. @param source the (potentially primitive) array @return the corresponding object array (never <cod
(Object source)
| 253 | * @throws IllegalArgumentException if the parameter is not an array |
| 254 | */ |
| 255 | public static Object[] toObjectArray(Object source) { |
| 256 | if (source instanceof Object[]) { |
| 257 | return (Object[]) source; |
| 258 | } |
| 259 | if (source == null) { |
| 260 | return new Object[0]; |
| 261 | } |
| 262 | if (!source.getClass().isArray()) { |
| 263 | throw new IllegalArgumentException("Source is not an array: " + source); |
| 264 | } |
| 265 | int length = Array.getLength(source); |
| 266 | if (length == 0) { |
| 267 | return new Object[0]; |
| 268 | } |
| 269 | Class wrapperType = Array.get(source, 0).getClass(); |
| 270 | Object[] newArray = (Object[]) Array.newInstance(wrapperType, length); |
| 271 | for (int i = 0; i < length; i++) { |
| 272 | newArray[i] = Array.get(source, i); |
| 273 | } |
| 274 | return newArray; |
| 275 | } |
| 276 | |
| 277 | |
| 278 | //--------------------------------------------------------------------- |
no test coverage detected