Java ToStringBuilder 不打印嵌套对象的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2413001/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ToStringBuilder doesn't print contents of nested objects
提问by goutham
I am cuurently using apache commons API (commmons-lang.jar) to print an object values as shown below:
我目前正在使用 apache commons API (commmons-lang.jar) 打印对象值,如下所示:
ToStringBuilder.reflectionToString(object,ToStringStyle.MULTI_LINE_STYLE))
But this code does not print value if the object has a getter method which returns another object.
但是,如果对象具有返回另一个对象的 getter 方法,则此代码不会打印值。
For example I have Personobject it has a getter method which returns Addressobject. Using the above code, it just prints Addressobject name. I am looking for printing Addressobject values also. If I a pass person.getAddress()to above code it prints that but I want to see everything when I pass person object.
例如,我有一个Person对象,它有一个返回Address对象的 getter 方法。使用上面的代码,它只是打印Address对象名称。我Address也在寻找打印对象值。如果我传递person.getAddress()给上面的代码,它会打印出来,但是当我传递 person 对象时,我想查看所有内容。
Any suggestions?
有什么建议?
回答by BalusC
Because it does not generate toStringrecursively. Either look for another tool, or just implement (or, better, IDE-autogenerate) the Object#toString()for all of them yourself.
因为它不会toString递归生成。要么寻找另一个工具,要么Object#toString()自己为所有这些工具实现(或者,更好的是,IDE 自动生成)。
For example:
例如:
public class Person {
private String name;
private Address address;
// Add/generate ctors, getters and setters.
public String toString() {
return String.format("Person[name: %s, %s]", name, address);
}
}
and
和
public class Address {
private String street;
private String city;
// Add/generate ctors, getters and setters.
public String toString() {
return String.format("Address[street: %s, city: %s]", street, city);
}
}
this way the
这样
String personString = person.toString();
and
和
System.out.println(person);
would produce something like
会产生类似的东西
Person[name: goutham, Address[street: Main Street 1, city: New York]]
回答by Jakub
You can as well define a base class for all your classes that will define toString method in the following way:
您也可以为所有类定义一个基类,这些类将通过以下方式定义 toString 方法:
public abstract class MyBaseClass{
public String toString(){
return ToStringBuilder.reflectionToString(object,ToStringStyle.MULTI_LINE_STYLE));
}
}
And then just make your classes to extend it. In such approach all your domain object will be able to generate a nice toString().
然后让你的类来扩展它。在这种方法中,您所有的域对象都将能够生成一个很好的 toString()。
回答by neo
You can use this method to dump each objects
您可以使用此方法转储每个对象
public static String dump(Object object) {
Field[] fields = object.getClass().getDeclaredFields();
StringBuilder sb = new StringBuilder();
sb.append(object.getClass().getSimpleName()).append('{');
boolean firstRound = true;
for (Field field : fields) {
if (!firstRound) {
sb.append(", ");
}
firstRound = false;
field.setAccessible(true);
try {
final Object fieldObj = field.get(object);
final String value;
if (null == fieldObj) {
value = "null";
} else {
value = fieldObj.toString();
}
sb.append(field.getName()).append('=').append('\'')
.append(value).append('\'');
} catch (IllegalAccessException ignore) {
//this should never happen
}
}
sb.append('}');
return sb.toString();
}
If there are objects inside the object, pass obj1.obj2 as the argument.
如果对象内部有对象,则将 obj1.obj2 作为参数传递。
回答by Asa
Since version 3.2of Apache Commons Lang, you can use a RecursiveToStringStyleto achieve exactly what you want.
从Apache Commons Lang 3.2 版开始,您可以使用 aRecursiveToStringStyle来实现您想要的。
Either:
任何一个:
ToStringBuilder.reflectionToString(object, new RecursiveToStringStyle());
or
或者
ToStringBuilder.reflectionToString(object, new MultilineRecursiveToStringStyle());
MultilineRecursiveToStringStyleis available since version 3.4
MultilineRecursiveToStringStyle自3.4 版起可用

