Class RecursiveToStringStyle

java.lang.Object
org.apache.commons.lang3.builder.ToStringStyle
org.apache.commons.lang3.builder.RecursiveToStringStyle
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
MultilineRecursiveToStringStyle

Works with ToStringBuilder to create a "deep" toString.

To use this class write code as follows:

public class Job {
  String title;
  ...
}

public class Person {
  String name;
  int age;
  boolean smoker;
  Job job;

  ...

  public String toString() {
    return new ReflectionToStringBuilder(this, new RecursiveToStringStyle()).toString();
  }
}

This will produce a toString of the format: Person@7f54[name=Stephen,age=29,smoker=false,job=Job@43cd2[title=Manager]]

Graph safety: within one top-level toString() call, each object is rendered in detail at most once. A second (identity-equal) occurrence of an object (whether through a true cycle or through a shared (acyclic) reference) is rendered in the abbreviated Object.toString() format instead of being re-traversed. This keeps traversal cost linear in the size of the object graph; without it, shared references (reference "diamonds") would be re-traversed exponentially. An optional output-length limit can be set via RecursiveToStringStyle.Builder.setMaxOutputLength(int); once the produced string reaches the limit, further nested objects are replaced by a "...<truncated>" marker.

Since:
3.2
See Also:
  • Constructor Details

    • RecursiveToStringStyle

      Constructs a new instance with no output-length limit.
    • RecursiveToStringStyle

      public RecursiveToStringStyle(int maxOutputLength)
      Constructs a new instance with an output-length limit.
      Parameters:
      maxOutputLength - once the produced string reaches this length, further nested objects are replaced by a "...<truncated>" marker; 0 means unlimited.
      Since:
      3.21.0
  • Method Details