public class ReflectionToStringBuilder extends ToStringBuilder
Object.toString()方法
该类使用反射来确定要追加的字段。因为这些字段通常是私有的,所以该类使用
AccessibleObject.setAccessible(AccessibleObject[], boolean)
来更改字段的可见性。在安全管理器下,除非正确设置了适当的权限,否则此操作将失败
此方法的典型调用如下所示:
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
Builder.HashKey| Modifier and Type | Field and Description |
|---|---|
protected String[] |
excludeFieldNames
要从输出中排除哪些字段名。适用于
"password"等字段 |
| Constructor and Description |
|---|
ReflectionToStringBuilder(Object object) |
ReflectionToStringBuilder(Object object,
ToStringStyle style) |
ReflectionToStringBuilder(Object object,
ToStringStyle style,
StringBuffer buffer) |
ReflectionToStringBuilder(T object,
ToStringStyle style,
StringBuffer buffer,
Class<? super T> reflectUpToClass,
boolean outputTransients,
boolean outputStatics)
构造函数
|
ReflectionToStringBuilder(T object,
ToStringStyle style,
StringBuffer buffer,
Class<? super T> reflectUpToClass,
boolean outputTransients,
boolean outputStatics,
boolean excludeNullValues)
构造函数
|
| Modifier and Type | Method and Description |
|---|---|
protected boolean |
accept(Field field)
返回是否附加给定的字段
Transient fields are appended only if
isAppendTransients() returns true. |
protected void |
appendFieldsIn(Class<?> clazz)
附加由给定类的给定对象定义的字段和值.
|
String[] |
getExcludeFieldNames() |
Class<?> |
getUpToClass() |
protected Object |
getValue(Field field) |
boolean |
isAppendStatics() |
boolean |
isAppendTransients() |
boolean |
isExcludeNullValues() |
ReflectionToStringBuilder |
reflectionAppendArray(Object array) |
void |
setAppendStatics(boolean appendStatics) |
void |
setAppendTransients(boolean appendTransients) |
ReflectionToStringBuilder |
setExcludeFieldNames(String... excludeFieldNamesParam) |
void |
setExcludeNullValues(boolean excludeNullValues) |
void |
setUpToClass(Class<?> clazz) |
String |
toString()
Returns the built
toString. |
static String |
toString(Object object) |
static String |
toString(Object object,
ToStringStyle style) |
static String |
toString(Object object,
ToStringStyle style,
boolean outputTransients) |
static String |
toString(Object object,
ToStringStyle style,
boolean outputTransients,
boolean outputStatics) |
static <T> String |
toString(T object,
ToStringStyle style,
boolean outputTransients,
boolean outputStatics,
boolean excludeNullValues,
Class<? super T> reflectUpToClass) |
static <T> String |
toString(T object,
ToStringStyle style,
boolean outputTransients,
boolean outputStatics,
Class<? super T> reflectUpToClass) |
static String |
toStringExclude(Object object,
Collection<String> excludeFieldNames) |
static String |
toStringExclude(Object object,
String... excludeFieldNames) |
append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, append, appendAsObjectToString, appendSuper, appendToString, build, getDefaultStyle, getObject, getStringBuffer, getStyle, reflectionToString, reflectionToString, reflectionToString, reflectionToString, setDefaultStyleprotected String[] excludeFieldNames
"password"等字段public ReflectionToStringBuilder(Object object)
public ReflectionToStringBuilder(Object object, ToStringStyle style)
public ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
public ReflectionToStringBuilder(T object,
ToStringStyle style,
StringBuffer buffer,
Class<? super T> reflectUpToClass,
boolean outputTransients,
boolean outputStatics)
T - 对象的类型object - 对象来构建一个 toStringstyle - 创建的toString,可以是nullbuffer - 要填充的StringBuffer,可以是nullreflectUpToClass - 要反映到(包括)的超类可以是nulloutputTransients - 是否包含忽略字段outputStatics - 是否包含静态字段public ReflectionToStringBuilder(T object,
ToStringStyle style,
StringBuffer buffer,
Class<? super T> reflectUpToClass,
boolean outputTransients,
boolean outputStatics,
boolean excludeNullValues)
T - 对象的类型object - 对象来构建一个 toStringstyle - 创建的toString,可以是nullbuffer - 要填充的StringBuffer,可以是nullreflectUpToClass - 要反映到(包括)的超类可以是nulloutputTransients - 是否包含忽略字段outputStatics - 是否包含静态字段excludeNullValues - 是否排除值为空的字段public static String toString(Object object, ToStringStyle style)
public static String toString(Object object, ToStringStyle style, boolean outputTransients)
public static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)
public static <T> String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, Class<? super T> reflectUpToClass)
public static <T> String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, boolean excludeNullValues, Class<? super T> reflectUpToClass)
public static String toStringExclude(Object object, Collection<String> excludeFieldNames)
protected boolean accept(Field field)
isAppendTransients() returns true.
isAppendStatics() returns true.
field - 字段属性.protected void appendFieldsIn(Class<?> clazz)
clazz - 对象参数的类public String[] getExcludeFieldNames()
public ReflectionToStringBuilder setExcludeFieldNames(String... excludeFieldNamesParam)
public Class<?> getUpToClass()
public void setUpToClass(Class<?> clazz)
protected Object getValue(Field field) throws IllegalAccessException
IllegalAccessExceptionpublic boolean isAppendStatics()
public void setAppendStatics(boolean appendStatics)
public boolean isAppendTransients()
public void setAppendTransients(boolean appendTransients)
public boolean isExcludeNullValues()
public void setExcludeNullValues(boolean excludeNullValues)
public ReflectionToStringBuilder reflectionAppendArray(Object array)
public String toString()
ToStringBuilderReturns the built toString.
This method appends the end of data indicator, and can only be called once.
Use ToStringBuilder.getStringBuffer() to get the current string state.
If the object is null, return the style's nullText
toString in class ToStringBuildertoStringCopyright © 2020. All rights reserved.