f-angi
5/31/2016 - 9:56 AM

Checks if an object is empty

Checks if an object is empty

import org.apache.commons.beanutils.PropertyUtils;

public boolean isNotEmptyObject(Object object) {

    if (object == null) {
        return false;
    }

    try {
        for (Object obj : PropertyUtils.describe(object).values()) {
            if (obj == null || obj instanceof Class) {
                continue;
            }
            if (obj instanceof String) {
                if (!((String) obj).trim().isEmpty()) {
                    return true;
                }
            }
            else if (obj instanceof Collection) {
                if (!((Collection<?>) obj).isEmpty()) {
                    return true;
                }
            }
            else {
                return isNotEmptyObject(obj);
            }
        }
    }
    catch (Exception e) {
        LOGGER.error("", e);
        return true;
    }
    return false;
}