I need to get the values of all possible variables (even if the field is private) of any object. I've seen that some asked the same questions before and I've seen different ways to solve this.
For example I have some class:
public class Person{
private String name;
private int age;
private Date birthday;
private Person father;
}
It's a good idea also to use Apache Common ReflectionToStringBuilder:
public String getAllValues(Object o){
return ReflectionToStringBuilder.toString(o, new MultilineRecursiveToStringStyle());
}
The output looks like that
Main$Person@782b3a41[
name=John Jones,
age=32,
birthday=java.util.Date@1fb2eseb[
],
father=Main$Person@11adc27[
name=Michael Jones,
age=72,
birthday=<null>,
father=Main$Person@2e1fgc38[
name=Jamie Jones,
age=99,
birthday=<null>,
father=<null>
]
]
]
And I'd like to have something like that:
name: "John Jones"
age: 32
birthDate: '1986-05-03'
father:
name: "Michael Jones"
age: 72
birthday: null
father:
name: "Jamie Jones"
age: 98
birthday: null
father: null
As I understand I need to create my own MyStyle class that extends Apache Common's MultilineRecursiveToStringStyle, don't I? Could you please point me to what exactly I should override? Is there any easier solution maybe?
Aucun commentaire:
Enregistrer un commentaire