private int calcArraySizeForChild(Object fieldValue) { if (fieldValue == null) { return 1; } // In excel each element contains at least one cell even if it's empty. if (children == null) { return 1; } if (fieldValue.getClass().isArray()) { int size = 0; int count = Array.getLength(fieldValue); for (int i = 0; i < count; i++) { size += calcArraySizeForChild(Array.get(fieldValue, i)); } return size == 0 ? 1 : size; } else { int max = 1; for (FieldDescriptor child : children) { int childSize = child.getMaxArraySize(fieldValue); if (childSize > max) { max = childSize; } } return max; } }
/** * Returns maximum array elements of the the field value from a given parent object * * @param object parent object (contains this field) * @return max array size if exist or 1 if there are no arrays */ public int getMaxArraySize(Object object) { if (object == null) { return 1; } if (object.getClass().isArray()) { int count = Array.getLength(object); int height = 0; for (int i = 0; i < count; i++) { height += getMaxArraySize(Array.get(object, i)); } return height == 0 ? 1 : height; } Object fieldValue = ExportUtils.fieldValue(object, getField()); return calcArraySizeForChild(fieldValue); }