公用的对象空字段处理方法

    /**
     * 描述 对象空字段处理(仅对字符串操作)
     */
    public static <T> T nullHandle(T t) {
        return nullHandle(t, "");
    }

    /**
     * 描述 对象空字段处理(仅对字符串操作)
     */
    public static <T> T nullHandle(T t, String replace) {
        Class clazz = null;
        try {
            clazz = Class.forName(t.getClass().getName());
        } catch (ClassNotFoundException e) {
            LOGGER.error("找不到对应的类:【{}】", WarnInfo.class.getName());
        }
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field field : declaredFields) {
            // 私有属性必须设置访问权限
            field.setAccessible(true);
            try {
                Object o = field.get(t);
                if (o == null && field.getGenericType().toString().equals(STRING_TYPE)) {
                    field.set(t, replace);
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return t;
    }

 

posted @ 2020-06-30 19:25  一如当初ゝ  阅读(130)  评论(0编辑  收藏  举报