ToStringBuilder如何忽略null和空字符串

日常开发中,经常用org.apache.commons.lang3.builder.ToStringBuilder工具类来记录日志,比如:

package com.cnblogs.yjmyzz;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.util.Date;

/**
 * @author 菩提树下的杨过
 * @className ToStringTest
 * @description TODO
 * @date 2022/5/8 07:24
 */
public class ToStringTest {


    static class DemoObject {
        private String a;
        private String b = "";
        private String c = "abc";
        private Date d = new Date();
        private Integer e;
    }



    public static void main(String[] args) {
        DemoObject obj = new DemoObject();
        //原始toString()
        System.out.println(obj);
        
        //ToStringBuilder的输出(默认风格)
        System.out.println(ToStringBuilder.reflectionToString(obj));
        
        //ToStringBuilder的输出(短前缀风格)
        System.out.println(ToStringBuilder.reflectionToString(obj, ToStringStyle.SHORT_PREFIX_STYLE));
    }
}

输出如下:

com.cnblogs.yjmyzz.ToStringTest$DemoObject@1786dec2
com.cnblogs.yjmyzz.ToStringTest$DemoObject@1786dec2[a=<null>,b=,c=abc,d=Sun May 08 07:35:50 CST 2022,e=<null>]
ToStringTest.DemoObject[a=<null>,b=,c=abc,d=Sun May 08 07:35:50 CST 2022,e=<null>]

从结果上看:

行1:也就是Object默认的toString()输出,只有类名以及对象实例的hashCode,内部成员信息完全看不出来

行2:使用ToStringBuilder工具类输出对象信息,包含了几乎所有完整信息

行3:如果觉得行2的输出,类名太长,可以指定短前缀风格

 

但是不管是行2还是行3的风格,当field值为null或字符串时,仍然会输出,如果想把这些空值给过滤掉,可以参考下面的方法,加一个类:

package com.cnblogs.yjmyzz;

import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.lang.reflect.Field;
import java.util.Date;

/**
 * @author 菩提树下的杨过
 * @className ToStringTest
 * @description TODO
 * @date 2022/5/8 07:24
 */
public class ToStringTest {


    static class DemoObject {
        private String a;
        private String b = "";
        private String c = "abc";
        private Date d = new Date();
        private Integer e;
    }

    /**
     * 新增的工具类
     */
    static class ToStringUtil {
        public static String toStringWithAttributes(Object ofInterest, ToStringStyle style) {
            ReflectionToStringBuilder builder = new ReflectionToStringBuilder(ofInterest, style) {
                @Override
                protected boolean accept(Field field) {
                    try {
                        Object value = field.get(ofInterest);
                        return super.accept(field) && value != null && !"".equalsIgnoreCase(value.toString());
                    } catch (IllegalAccessException e) {
                        return super.accept(field);
                    }
                }
            };
            return builder.toString();
        }
    }

    public static void main(String[] args) {
        DemoObject obj = new DemoObject();
        //原始toString()
        System.out.println(obj);

        //去掉null和空字符串的输出
        System.out.println(ToStringUtil.toStringWithAttributes(obj, ToStringStyle.SHORT_PREFIX_STYLE));

    }
}

再次运行:

com.cnblogs.yjmyzz.ToStringTest$DemoObject@1786dec2
ToStringTest.DemoObject[c=abc,d=Sun May 08 07:43:01 CST 2022]

从输出上看,null值以及空字符串都被成功过滤掉了

posted @ 2022-05-08 07:46  菩提树下的杨过  阅读(676)  评论(0编辑  收藏  举报