[ovirt-devel] Introducing ToStringBuilder

Martin Perina mperina at redhat.com
Thu Apr 9 12:58:47 UTC 2015


Hi,

patch [1] introducing ToStringBuilder was merged to master a few days ago
and hopefully additional fixes [2] for ToStringBuilder will be merged soon.

ToStringBuilder is a helper class which eases implementation of toString()
method, which usually contains a lot of class info for logging purposes.

For example take a look at following class:


 public class Test {
     private boolean flag;
     private int number;
     private List<String> messages;
     private String password;

     ...

 }


Using ToStringBuilder we can easily create toString() method:


     @Override
     public String toString() {
         return ToStringBuilder.forInstance(this)
                 .append("flag", flag)
                 .append("number", getNumber())
                 .append("message", getMessages)
                 .appendFiltered("password", getPassword())
                 .build();
     }


Using this toString() method we will receive following output (assuming
that attributes as filled in with values):


  Test:{flag='false', number='0', message='[msg1, msg2]', password='***'}


For more information about ToStringBuilder please take a look at source code
which is documented by JavaDoc.

It would be great if we start to use this builder in engine code, so I created
patch [3] which refactors existing relevant toString() methods to use
ToStringBuilder.


Thanks

Martin Perina


[1] https://gerrit.ovirt.org/39240
[2] https://gerrit.ovirt.org/39720
[3] https://gerrit.ovirt.org/39721



More information about the Devel mailing list