我必须在日志文件中打印对象值。我用了:
ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE, true, true);
但它也会打印我不想包含的空值,例如:
Pojo@117d9a3[id=1, name=null ,description=曼联,KEY=APP-KEY,secretKey=Alex ]
如何禁止包含空值?
我必须在日志文件中打印对象值。我用了:
ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE, true, true);
但它也会打印我不想包含的空值,例如:
Pojo@117d9a3[id=1, name=null ,description=曼联,KEY=APP-KEY,secretKey=Alex ]
如何禁止包含空值?
没有子类化的更简单的解决方案是覆盖 accept 方法:
public String toStringWithAttributes() {
Object myself = this;
ReflectionToStringBuilder builder = new ReflectionToStringBuilder(
this, ToStringStyle.SHORT_PREFIX_STYLE) {
@Override
protected boolean accept(Field field) {
try {
return super.accept(field) && field.get(myself) != null;
} catch (IllegalAccessException e) {
return super.accept(field);
}
}
};
return builder.toString();
}
这有一个额外的好处,你可以使用任何ToStringStyle
你想要的东西,并且格式是完美的。
您必须提供自己的 ToStringStyle 实现。像这样的东西(未经测试!):
import org.apache.commons.lang.SystemUtils;
import org.apache.commons.lang.builder.ToStringStyle;
public final class NotNullToStringStyle extends ToStringStyle {
public static final ToStringStyle NOT_NULL_STYLE = new NotNullToStringStyle();
private static final long serialVersionUID = 1L;
/**
* <p>Constructor.</p>
*
* <p>Use the static constant rather than instantiating.</p>
*/
NotNullToStringStyle() {
super();
this.setContentStart("[");
this.setFieldSeparator(SystemUtils.LINE_SEPARATOR + " ");
this.setFieldSeparatorAtStart(true);
this.setContentEnd(SystemUtils.LINE_SEPARATOR + "]");
}
/**
* <p>Ensure <code>Singleton</code> after serialization.</p>
*
* @return the singleton
*/
private Object readResolve() {
return NOT_NULL_STYLE;
}
@Override
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
if (value != null) {
appendFieldStart(buffer, fieldName);
appendInternal(buffer, fieldName, value, isFullDetail(fullDetail));
appendFieldEnd(buffer, fieldName);
}
}
}
大部分代码都是从 复制的MultiLineToStringStyle
,因为它是private
,final
所以我们不能扩展它。真正的事情发生在append
方法中。以下是原文供参考:
public void append(StringBuffer buffer, String fieldName, Object value, Boolean fullDetail) {
appendFieldStart(buffer, fieldName);
if (value == null) {
appendNullText(buffer, fieldName);
} else {
appendInternal(buffer, fieldName, value, isFullDetail(fullDetail));
}
appendFieldEnd(buffer, fieldName);
}
@Christian Sporer 的回答(给他投票),但修改为可重用的 util 方法:
public class ToStringUtil {
public static String toStringWithAttributes(Object ofInterest, ToStringStyle style) {
ReflectionToStringBuilder builder = new ReflectionToStringBuilder(ofInterest, style) {
@Override
protected boolean accept(Field field) {
try { return super.accept(field) && field.get(ofInterest) != null; }
catch (IllegalAccessException e) { return super.accept(field); }
}
};
return builder.toString();
}
}
有一种方法可以setNullText(String nullText)
使用您自己的文本在此对象上创建新的 ToStringStyle 对象调用,然后将此 ToStringStyle 对象传递给ReflectionToStringBuilder
类的构造函数。
从 3.6 开始,有一个内置方法
ReflectionToStringBuilder.toString(this, ToStringStyle.DEFAULT_STYLE,false,false,true,null);
方法签名是:
ReflectionToStringBuilder.toString(
Object object,
ToStringStyle style,
boolean outputTransients,
boolean outputStatics,
boolean excludeNullValues,
Class<? super Object> reflectUpToClass)
Apache commons-lang3 中存在开箱即用的实现:
ReflectionToStringBuilder.toString(new Pojo(), ToStringStyle.DEFAULT_STYLE, true, false, true , null)
输出按要求:
general.Pojo@3532ec19[description=Manchester,id=1]
使用:
public static String toString( final T object, final ToStringStyle style, final boolean outputTransients, final boolean outputStatics, final boolean excludeNullValues , final Class<? super T> reflectUpToClass)