首页 > 其他 > 详细

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

时间:2020-06-30 20:53:16      阅读:49      评论:0      收藏:0      [点我收藏+]
    /**
     * 描述 对象空字段处理(仅对字符串操作)
     */
    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;
    }

 

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

原文:https://www.cnblogs.com/rumian/p/13215526.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!