java类转字符串写法ToStringBuilder的应用
java开发中你可能需展示类的属性值,把对象记录为日志,调试输出等操作。ToStringBuilder提供简便类转字符串写法。
演示User类
User.java
package com.weizhixi.util; public class User { private Long id; private String username; private String password; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
没用ToStringBuilder前
你可能还在:
User user = new User(); System.out.println(user); //com.weizhixi.util.User@16d2633
真伤心,看不到对象里面有什么值。之后你会:
System.out.println("id:"+user.getId()+",username:"+user.getUsername()+",password:"+user.getPassword()); //id:1,username:weizhixi,password:123456
这写法真逆天,对象字段多就晕倒。
使用ToStringBuilder后
真心方便,只需在User对象中重写toString
@Override public String toString() { return ToStringBuilder.reflectionToString(this); }
这下好了:
System.out.println(user); //com.weizhixi.util.User@160877b[id=1,username=weizhixi,password=123456]
ToStringBuilder的风格
public abstract class ToStringStyle implements Serializable { private static final long serialVersionUID = -2587890625525655916L; public static final ToStringStyle DEFAULT_STYLE = new ToStringStyle.DefaultToStringStyle(); public static final ToStringStyle MULTI_LINE_STYLE = new ToStringStyle.MultiLineToStringStyle(); public static final ToStringStyle NO_FIELD_NAMES_STYLE = new ToStringStyle.NoFieldNameToStringStyle(); public static final ToStringStyle SHORT_PREFIX_STYLE = new ToStringStyle.ShortPrefixToStringStyle(); public static final ToStringStyle SIMPLE_STYLE = new ToStringStyle.SimpleToStringStyle(); ... }
使用方法:
@Override public String toString() { //return ToStringBuilder.reflectionToString(this); //return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE); //return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); //return ToStringBuilder.reflectionToString(this, ToStringStyle.NO_FIELD_NAMES_STYLE); //return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); return ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE); }
使用:DEFAULT_STYLE 默认风格
com.weizhixi.util.User@160877b[id=1,username=weizhixi,password=123456]
使用:MULTI_LINE_STYLE 多行风格
com.weizhixi.util.User@160877b[ id=1 username=weizhixi password=123456 ]
使用:NO_FIELD_NAMES_STYLE 不含字段名风格
com.weizhixi.util.User@160877b[1,weizhixi,123456]
使用:SHORT_PREFIX_STYLE 短类名前缀风格
User[id=1,username=weizhixi,password=123456]
使用:SIMPLE_STYLE 简单风格
1,weizhixi,123456
完整示例
package com.weizhixi.util; import org.apache.commons.lang3.builder.ToStringBuilder; import org.apache.commons.lang3.builder.ToStringStyle; public class User { private Long id; private String username; private String password; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE); } }
需要导入:
commons-lang3.jar
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.2.1</version> </dependency>
反射原理
看源码片段,ReflectionToStringBuilder类
... protected void appendFieldsIn(Class<?> clazz) { if(clazz.isArray()) { this.reflectionAppendArray(this.getObject()); } else { Field[] fields = clazz.getDeclaredFields(); AccessibleObject.setAccessible(fields, true); Field[] arr$ = fields; int len$ = fields.length; for(int i$ = 0; i$ < len$; ++i$) { Field field = arr$[i$]; String fieldName = field.getName(); if(this.accept(field)) { try { Object ex = this.getValue(field); this.append(fieldName, ex); } catch (IllegalAccessException var9) { throw new InternalError("Unexpected IllegalAccessException: " + var9.getMessage()); } } } } } ... public String toString() { if(this.getObject() == null) { return this.getStyle().getNullText(); } else { Class clazz = this.getObject().getClass(); this.appendFieldsIn(clazz); while(clazz.getSuperclass() != null && clazz != this.getUpToClass()) { clazz = clazz.getSuperclass(); this.appendFieldsIn(clazz); } return super.toString(); } } ...
原创文章,转载请注明出处:https://www.weizhixi.com/article/55.html