How to print Java Bean completely using reflection or any other utility
You could use ToStringBuilder from Apache Commons.
From documentation:
A typical invocation for this method would look like:
public String toString() { return ToStringBuilder.reflectionToString(this); }
More details:
This class enables a good and consistent toString() to be built for any class or object. This class aims to simplify the process by:
allowing field names handling all types consistently handling nulls consistently outputting arrays and multi-dimensional arrays enabling the detail level to be controlled for Objects and Collections handling class hierarchies To use this class write code as follows:
public class Person { String name; int age; boolean smoker; ... public String toString() { return new ToStringBuilder(this). append("name", name). append("age", age). append("smoker", smoker). toString(); } }
Alternatively, there is a method that uses reflection to determine the fields to test. Because these fields are usually private, the method, reflectionToString, uses AccessibleObject.setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set up correctly. It is also slower than testing explicitly. A typical invocation for this method would look like:
public String toString() { return ToStringBuilder.reflectionToString(this); }
You're probably looking for Apache Commons ToStringBuilder#reflectionToString(Object)
.