Java toString方法重寫工具之ToStringBuilder案例詳解
apache的commons-lang3的工具包里有一個ToStringBuilder類,這樣在打日志的時候可以方便的打印出類實例中的各屬性的值。
具體用法如下:
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
public class Message {
private String from;
private String to;
private String body;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
}
public static void main(String[] args) {
Message msg = new Message();
msg.setFrom("vince");
msg.setTo("mike");
msg.setBody("hello");
System.out.println(msg.toString());
}
}
而且支持多種打印格式
多行輸出的:
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。
里面部分源碼:
/**
* <p>Append to the <code>toString</code> the start of data indicator.</p>
* 拼裝結(jié)果的
* @param buffer the <code>StringBuffer</code> to populate
* @param object the <code>Object</code> to build a <code>toString</code> for
*/
public void appendStart(final StringBuffer buffer, final Object object) {
if (object != null) {
appendClassName(buffer, object);
appendIdentityHashCode(buffer, object);
appendContentStart(buffer);
if (fieldSeparatorAtStart) {
appendFieldSeparator(buffer);
}
}
}
/**
* <p>Append the {@link System#identityHashCode(java.lang.Object)}.</p>
* 拼裝對象hashcode
* @param buffer the <code>StringBuffer</code> to populate
* @param object the <code>Object</code> whose id to output
*/
protected void appendIdentityHashCode(final StringBuffer buffer, final Object object) {
if (this.isUseIdentityHashCode() && object!=null) {
register(object);
buffer.append('@');
buffer.append(Integer.toHexString(System.identityHashCode(object)));
}
}
需要注意的是:
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修飾的屬性不能打印出來,但是父類的是可以打印出來的,使用的時候一定要注意了。
到此這篇關(guān)于Java toString方法重寫工具之ToStringBuilder案例詳解的文章就介紹到這了,更多相關(guān)Java toString方法重寫工具之ToStringBuilder內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java實現(xiàn)級聯(lián)下拉結(jié)構(gòu)的示例代碼
在開發(fā)過程中,會遇到很多的實體需要將查出的數(shù)據(jù)處理為下拉或者級聯(lián)下拉的結(jié)構(gòu),提供給前端進行展示。本文為大家介紹了java封裝下拉和級聯(lián)下拉的通用工具類,需要的可以參考一下2022-06-06
VSCode搭建Java開發(fā)環(huán)境的超詳細步驟
VSCode是一款多平臺的源代碼編輯器,支持多種編程語言,它輕量級、功能強大,通過豐富的插件生態(tài)系統(tǒng)可以支持更多語言和運行時,如C++、C#、Java、Python等,這篇文章主要介紹了VSCode搭建Java開發(fā)環(huán)境的超詳細步驟,需要的朋友可以參考下2024-10-10
Spring實現(xiàn)HikariCP連接池的示例代碼
在SpringBoot 2.0中,我們使用默認連接池是HikariCP,本文講一下HikariCP的具體使用,具有一定的參考價值,感興趣的可以了解一下2021-08-08

