Apache common ToStringBuilder class for Java toString() implementation

1436
Mar 20, 2016

Object.toString() method is one of the most useful method for debugging purpose. Here is how to override this method easily and effectively.
This is how we generally override toString method

public class ToStringTest {
public static void main(String[] args) {
Person person = new Person();
person.setFirstName("Manoj");
person.setLastName("Tripathi");

System.out.println(person);
}
}

class Person {
private String firstName;
private String lastName;
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}

@Override
public String toString() {
return new StringBuffer()
.append("firstName:").append(getFirstName()).append(",")
.append("lastName:").append(getLastName())
.toString();
}
}

Prints: firstName:Manoj,lastName:Tripathi


In toString() we concatenate all the properties name and its values, and returns the String. Here is now we can utilize the org.apache.commons.lang3.builder.ToStringBuilder.reflectionToString() utility class of Apache common library to help in implementing the toString very easily.




import org.apache.commons.lang.builder.ToStringBuilder;

public class ToStringTest {
public static void main(String[] args) {
Person person = new Person();
person.setFirstName("Manoj");
person.setLastName("Tripathi");

System.out.println(person);
}
}

class Person {
private String firstName;
private String lastName;
/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}
/**
* @param firstName the firstName to set
*/
public void setFirstName(String firstName) {
this.firstName = firstName;
}
/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}
/**
* @param lastName the lastName to set
*/
public void setLastName(String lastName) {
this.lastName = lastName;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}

Prints: Person@60607135[firstName=Manoj,lastName=Tripathi]






There are some default formatters provided by this class to format the result










ExampleOutput
ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
firstName:Manoj,lastName:Tripathi
ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
Person@22893e69[
firstName=Manoj
lastName=Tripathi
]
ToStringBuilder.reflectionToString(this, ToStringStyle.NO_FIELD_NAMES_STYLE);
Person@5ba88001[Manoj,Tripathi]
ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
Person[firstName=Manoj,lastName=Tripathi]
ToStringBuilder.reflectionToString(this, ToStringStyle.SIMPLE_STYLE);
Manoj,Tripathi





In some cases you may need string representation of a third party Class's object, in this case also you can use this utility class.

Suppose you have an object anObject which is an instance of some third party class ThirdPartyClass which you don't own but want to print the all the properties of this object for debugging purpose. here is how you can use ToStringBuilder class for this purpose as well.



System.out.println(ToStringBuilder.reflectionToString(anObject));





Maven dependency



<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>



comments powered by Disqus

© Copyright 2017