my Question is, is it possible to print out specific information from a class that provides no toString() method in JAVA?
There’s the rub: We provide a logger for our application (using aspectJ) which prints out the specific arguments, that were given. For example:
public void addGroupMembers(Group group, String doneBy, DataListModel<User> users) {
doSomething()
}
And our Logger prints the following:
addGroupMembers called with given arguments:
Group = [id = ..... and so on]
username
DataListModel$1231 <-
We have to use the DataListModel-Class, because we’re working with JSF in the background. But, as you can see, this class doesn’t provide a toString method.
Our logger is written by ourself, so we can adapt that. Is it possible to simulate a toString method like: If the class doesn’t provide a toString, catch all their fields, and print them?
Or is there any other way?
Thanks in advance for your help.
Greetings, Thunderhook
You could make use of ReflectionToStringBuilder. Something like: