Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Object[] toObjectArray(Object obj) {
- if (obj == null) {
- return null;
- }
- // get class type of argument
- Class<?> clazz = obj.getClass();
- Preconditions.checkArgument(clazz.isArray(), "Given object is not array!");
- // get length of array
- int length = Array.getLength(obj);
- // prepare class type of
- Class<?> componentType = clazz.getComponentType();
- // check whatever the array is array of primitives
- if (!componentType.isPrimitive()) {
- // return original argument
- return (Object[])obj;
- }
- // prepare Object variant of component type
- Class<?> componentObjectType = PrimitiveUtils.wrap(componentType);
- // prepare array
- Object[] array = (Object[])Array.newInstance(componentObjectType, length);
- for (int i = 0; i < length; i++) {
- // boxing
- try {
- Constructor<?> construct = componentObjectType.getConstructor(componentType);
- array[i] = construct.newInstance(Array.get(obj, i));
- }
- catch (Exception exc) {
- // this should not happen
- exc.printStackTrace();
- }
- }
- return array;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement