码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2020-06-30 20:15:38      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:nts   处理   inf   static   字段   null   access   object   str   

    /**
     * 描述 对象空字段处理(仅对字符串操作)
     */
    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;
    }

 

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

标签:nts   处理   inf   static   字段   null   access   object   str   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!