Java – how to exclude a single variable using tostrimbuilder
•
Java
I have an object that contains many variables, but one of them is bytearray, for example
public class DataWithFields { private String string1; private String string2; .... private byte[] data public String toString() { return ToStringBuilder.reflectionToString(this,ToStringStyle.SHORT_PREFIX_STYLE); } }
For the above code, I want to exclude data variables from toString without explicitly defining each value What should I do?
Solution
Simpler solution:
@Override public String toString() { return ReflectionToStringBuilder.toStringExclude(this,"data"); }
If defaulttostringstyle is not suitable for your application needs, be sure to call
ReflectionToStringBuilder.setDefaultStyle(style);
At the beginning of the application
If you want to exclude more fields, just add them after or before data
@Override public String toString() { return ReflectionToStringBuilder.toStringExclude(this,"fieldX","data","fieldY"); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码