Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print Java Bean completely using reflection or any other utility

I have an Java bean class Person containing 3 variables:

  • name (String)
  • age (String)
  • address (Object).

Address contains 3 variables:

  • street
  • door_no
  • city

I would like to have a utility which should print all variables in Person.

By this I mean it should print Person & also the Address object contained in it.

I could create a hashmap and put the variable names & values using reflection and print key/value UI in JSP but the issue is I have to apply reflection for Address to add variable/value in the hashmap.

Is there a utility available to do this?

like image 495
sridhar Avatar asked Jul 26 '12 12:07

sridhar


4 Answers

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);
 }
like image 119
Francisco Spaeth Avatar answered Oct 26 '22 12:10

Francisco Spaeth


You're probably looking for Apache Commons ToStringBuilder#reflectionToString(Object).

like image 30
Romain Avatar answered Oct 26 '22 11:10

Romain


You may find the Jackson JSON serializer useful for this purpose. The Jackson library may already be part of your stack. If not, find the required dependencies below.

private static final ObjectMapper OBJECT_MAPPER_SINGLETON = new ObjectMapper();
public static String toStringUsingJackson(final Object object) {
    try {
        return OBJECT_MAPPER_SINGLETON.writeValueAsString(object);
    } catch (final JsonProcessingException e) {
        return String.valueOf(object);
    }
}

Sample output:

{"name":"John Doe","age":42}

Required maven/gradle dependencies:

  • jackson-core, groupId=com.fasterxml.jackson.core
  • jackson-databind, groupId=com.fasterxml.jackson.core
like image 34
Steve Oh Avatar answered Oct 26 '22 12:10

Steve Oh


You can implement the toString method to print out whatever you want, or you can use Apache Commons ToStringBuilder http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/builder/ToStringBuilder.html and its reflectionToString method. I don't believe this will recurse through the properties (like address for example), but if you want to see Address printed out with Street, Door No and City, use implement its toString method to print that information out.

like image 29
Jeff Storey Avatar answered Oct 26 '22 12:10

Jeff Storey