Class RecursiveToStringStyle

  • All Implemented Interfaces:
    java.io.Serializable
    Direct Known Subclasses:
    MultilineRecursiveToStringStyle

    public class RecursiveToStringStyle
    extends ToStringStyle

    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]]

    Since:
    3.2
    See Also:
    Serialized Form
    • Constructor Detail

      • RecursiveToStringStyle

        public RecursiveToStringStyle()

        Constructor.

    • Method Detail

      • appendDetail

        public void appendDetail​(java.lang.StringBuffer buffer,
                                 java.lang.String fieldName,
                                 java.lang.Object value)
        Description copied from class: ToStringStyle

        Append to the toString an Object value, printing the full detail of the Object.

        Overrides:
        appendDetail in class ToStringStyle
        Parameters:
        buffer - the StringBuffer to populate
        fieldName - the field name, typically not used as already appended
        value - the value to add to the toString, not null
      • appendDetail

        protected void appendDetail​(java.lang.StringBuffer buffer,
                                    java.lang.String fieldName,
                                    java.util.Collection<?> coll)
        Description copied from class: ToStringStyle

        Append to the toString a Collection.

        Overrides:
        appendDetail in class ToStringStyle
        Parameters:
        buffer - the StringBuffer to populate
        fieldName - the field name, typically not used as already appended
        coll - the Collection to add to the toString, not null
      • accept

        protected boolean accept​(java.lang.Class<?> clazz)
        Returns whether or not to recursively format the given Class. By default, this method always returns true, but may be overwritten by sub-classes to filter specific classes.
        Parameters:
        clazz - The class to test.
        Returns:
        Whether or not to recursively format the given Class.