转载

对象检查工具方法类

传入对象,检查对象
方法一:checkNullAndEmpty对象有值,或者对象里面的字段存在有值时返回false,没有值返回true;
方法二:checkAllHaveValue对象有值,或者对象里面的字段都有值时返回true;


/**
 * @Description: 对象检查工具类
 * @Param:
 * @Return:
 * @Author: yao
 * @Date: 2019/11/13
 * @Time: 9:38
 */
public class ObjectCheckUtil {
    private static final Logger log = LoggerFactory.getLogger(ObjectCheckUtil.class);
    /**
     * 传入对象,检查对象是否为null或者所有的属性为空/null,不校验boolean类型为false的情况
     * @return
     */
    public static boolean checkNullAndEmpty(Object obj){
        if(null == obj){
            return true;
        }else if (obj instanceof CharSequence){
            return ((CharSequence) obj).length() == 0;
        }
        else if (obj instanceof Collection){
            return ((Collection) obj).isEmpty();
        }
        else if (obj instanceof Map){
            return ((Map) obj).isEmpty();
        }
        else if (obj.getClass().isArray()) {
            return Array.getLength(obj) == 0;
        }
        Class<?> clzz = obj.getClass();
        Field[] allFields = getAllFields(clzz);
        for (Field field : allFields) {
            if(Modifier.isFinal(field.getModifiers()) || Modifier.isStatic(field.getModifiers()) || Modifier.isAbstract(field.getModifiers())){
                continue;
            }
            if(!field.isAccessible()){
                field.setAccessible(true);
            }
            try {
                Object o = field.get(obj);
                //处理String,排除空字符串
                if(o instanceof String){
                    if(StringUtils.isNotEmpty((String) o)){
                        return false;
                    }
                }
                if(o instanceof Boolean){
                    //boolean类型且默认为true
                    if((Boolean) o){
                        return false;
                    }
                    continue;
                }
                if(o != null){
                    return false;
                }
                if(o instanceof Collection){
                    if(!((Collection) obj).isEmpty()){
                        return false;
                    }
                }
                if(o instanceof Map){
                    if(!((Map) obj).isEmpty()){
                        return false;
                    }
                }
                if(o != null && o.getClass().isArray() ){
                    if(Array.getLength(obj) > 0){
                        return false;
                    }
                }
            } catch (IllegalAccessException e) {
                log.error("get obj by reflect error{}:",field.getName(),e);
            }
        }
        return true;
    }

    public static Field[] getAllFields(Class c){
        List<Field> fieldList = new ArrayList<>();
        while (c!= null){
            fieldList.addAll(new ArrayList<>(Arrays.asList(c.getDeclaredFields())));
            c= c.getSuperclass();
        }
        Field[] fields = new Field[fieldList.size()];
        fieldList.toArray(fields);
        return fields;
    }

    public static boolean checkAllHaveValue(Object obj){
        if(null == obj){
            return false;
        }else if (obj instanceof CharSequence){
            return ((CharSequence) obj).length() > 0;
        }
        else if (obj instanceof Collection){
            return !((Collection) obj).isEmpty();
        }
        else if (obj instanceof Map){
            return !((Map) obj).isEmpty();
        }
        else if (obj.getClass().isArray()) {
            return Array.getLength(obj) > 0;
        }
        Class<?> clzz = obj.getClass();
        Field[] allFields = getAllFields(clzz);
        for (Field field : allFields) {
            if(Modifier.isFinal(field.getModifiers()) || Modifier.isStatic(field.getModifiers()) || Modifier.isAbstract(field.getModifiers())){
                continue;
            }
            if(!field.isAccessible()){
                field.setAccessible(true);
            }
            try {
                Object o = field.get(obj);
                if(o == null){
                    return false;
                }
                //处理String,空字符串返回false
                if(o instanceof String){
                    if(StringUtils.isEmpty((String) o)){
                        return false;
                    }
                    continue;
                }
                if(o instanceof Boolean){
                    //boolean类型且默认为true
                    if((Boolean) o == null){
                        return false;
                    }
                    continue;
                }
                if(o instanceof Collection){
                    if(((Collection) obj).isEmpty()){
                        return false;
                    }
                    continue;
                }
                if(o instanceof Map){
                    if(((Map) obj).isEmpty()){
                        return false;
                    }
                    continue;
                }
                if(o != null && o.getClass().isArray() ){
                    if(Array.getLength(obj) == 0){
                        return false;
                    }
                }
            } catch (IllegalAccessException e) {
                log.error("get obj by reflect error{}:",field.getName(),e);
            }
        }
        return true;
    }

    public static void main(String[] args) {
        SysPushTask sysPushTask = new SysPushTask();
        sysPushTask.setId(1l);
        sysPushTask.setBusinessType(BusinessTypeEnum.TEACHING_TASK.getKey());
        sysPushTask.setBusinessId("123");
        sysPushTask.setBusinessName(BusinessTypeEnum.TEACHING_TASK.getDescript());
        sysPushTask.setState(SPTStateEnum.UN_SCHEDULED.getKey());
        sysPushTask.setTitle("【"+BusinessTypeEnum.TEACHING_TASK.getDescript()+"提醒】");
        sysPushTask.setCreateTime(DataUtil.getNowTimeDay());
        sysPushTask.setCoId(10001l);
        sysPushTask.setMsgType(MsgTypeEnum.WX.getKey());
        sysPushTask.setOpenId("123");
        sysPushTask.setContent("提醒内容");// 提醒内容
        sysPushTask.setDispatchTime("2019-01-01");// 提前一天
        boolean ret = ObjectCheckUtil.checkAllHaveValue(sysPushTask);
        System.out.println("========="+ret);
    }

}
正文到此结束
该篇文章的评论功能已被站长关闭