关于toString方法的重写工具ToStringBuilder

apache的commons-lang3的工具包里有一个ToStringBuilder类,这样在打日志的时候可以方便的打印出类实例中的各属性的值。

具体用法如下:

[java]  view plain  copy
  1. import org.apache.commons.lang3.builder.ToStringBuilder;  
  2. import org.apache.commons.lang3.builder.ToStringStyle;  
  3.   
  4. public class Message {  
  5.   
  6.     private String from;  
  7.   
  8.     private String to;  
  9.   
  10.     private String body;  
  11.   
  12.     public String getFrom() {  
  13.         return from;  
  14.     }  
  15.   
  16.     public void setFrom(String from) {  
  17.         this.from = from;  
  18.     }  
  19.   
  20.     public String getTo() {  
  21.         return to;  
  22.     }  
  23.   
  24.     public void setTo(String to) {  
  25.         this.to = to;  
  26.     }  
  27.   
  28.     public String getBody() {  
  29.         return body;  
  30.     }  
  31.   
  32.     public void setBody(String body) {  
  33.         this.body = body;  
  34.     }  
  35.   
  36.     @Override  
  37.     public String toString() {  
  38.         return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);  
  39.     }  
  40.       
  41.     public static void main(String[] args) {  
  42.         Message msg = new Message();  
  43.         msg.setFrom("vince");  
  44.         msg.setTo("mike");  
  45.         msg.setBody("hello");  
  46.         System.out.println(msg.toString());  
  47.     }  
  48. }  

而且支持多种打印格式

多行输出的:

com.vince.im.dto.Message@af72d8[
  from=vince
  to=mike
  body=hello
]

默认一行的:

com.vince.im.dto.Message@af72d8[from=vince,to=mike,body=hello]


NO_FIELD_NAMES_STYLE:

com.vince.im.dto.Message@af72d8[vince,mike,hello]


SHORT_PREFIX_STYLE:

Message[from=vince,to=mike,body=hello]


SIMPLE_STYLE:

vince,mike,hello

原理其实就是通过JAVA的reflect(反射)获取值,然后组成一个Buffer。

里面部分源码:

[java]  view plain  copy
  1.     /** 
  2.      * <p>Append to the <code>toString</code> the start of data indicator.</p> 
  3.      * 拼装结果的 
  4.      * @param buffer  the <code>StringBuffer</code> to populate 
  5.      * @param object  the <code>Object</code> to build a <code>toString</code> for 
  6.      */  
  7.     public void appendStart(final StringBuffer buffer, final Object object) {  
  8.         if (object != null) {  
  9.             appendClassName(buffer, object);  
  10.             appendIdentityHashCode(buffer, object);  
  11.             appendContentStart(buffer);  
  12.             if (fieldSeparatorAtStart) {  
  13.                 appendFieldSeparator(buffer);  
  14.             }  
  15.         }  
  16.     }  
  17.   
  18.     /** 
  19.      * <p>Append the {@link System#identityHashCode(java.lang.Object)}.</p> 
  20.      * 拼装对象hashcode 
  21.      * @param buffer  the <code>StringBuffer</code> to populate 
  22.      * @param object  the <code>Object</code> whose id to output 
  23.      */  
  24.     protected void appendIdentityHashCode(final StringBuffer buffer, final Object object) {  
  25.         if (this.isUseIdentityHashCode() && object!=null) {  
  26.             register(object);  
  27.             buffer.append('@');  
  28.             buffer.append(Integer.toHexString(System.identityHashCode(object)));  
  29.         }  
  30.     }  

需要注意的是:

Builds a toString value using the default ToStringStyle through reflection.

It uses AccessibleObject.setAccessible to gain access to private fields. This means that it will throw a security exception if run under a security manager, if the permissions are not set up correctly. It is also not as efficient as testing explicitly.

Transient members will be not be included, as they are likely derived. Static fields will not be included. Superclass fields will be appended.

也就是说transient和static修饰的属性不能打印出来,但是父类的是可以打印出来的,使用的时候一定要注意了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值