private static int[] arrayDataToShape(final Object data, final int depth) {
        final int length = Array.getLength(data);
        final Class<?> klass = data.getClass().getComponentType();
        // If data is an array of arrays:
        if (length != 0 && klass.isArray()) {
            final int[] shape = PyMultiarray.arrayDataToShape(
                    Array.get(data, 0),
                    depth + 1);
            shape[depth] = length;
            // Verify that the array is well formed.
            for (int i = 0; i < length; i++) {
                final int[] shape2 = PyMultiarray.arrayDataToShape(
                        Array.get(data, i),
                        depth + 1);
                if (shape.length != shape2.length) { throw Py
                        .ValueError("malformed array"); }
                for (int j = depth + 1; j < shape.length; j++) {
                    if (shape[j] != shape2[j]) { throw Py
                            .ValueError("malformed array"); }
                }
            }
            return shape;
        }
        // If data is just an array of Primitives:
        final int[] shape = new int[depth + 1];
        shape[depth] = length;
        return shape;
    }