Package 

Class ReflectionToStringBuilder

  • All Implemented Interfaces:
    external.org.apache.commons.lang3.builder.Builder

    
    public class ReflectionToStringBuilder
    extends ToStringBuilder
                        

    Assists in implementing toString methods using reflection.

    This class uses reflection to determine the fields to append. Because these fields are usually private, the class uses setAccessible to change the visibility of the fields. This will fail under a security manager, unless the appropriate permissions are set up correctly.

    Using reflection to access (private) fields circumvents any synchronization protection guarding access to these fields. If a toString method cannot safely read a field, you should exclude it from the toString method, or use synchronization consistent with the class' lock management around the invocation of the method. Take special care to exclude non-thread-safe collection classes, because these classes may throw ConcurrentModificationException if modified while the toString method is executing.

    A typical invocation for this method would look like:

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

    You can also use the builder to debug 3rd party objects:

    System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));
    

    A subclass can control field output by overriding the methods:

    • accept
    • getValue

    For example, this method does not include the password field in the returned String:

    public String toString() {
        return (new ReflectionToStringBuilder(this) {
            protected boolean accept(Field f) {
                return super.accept(f) && !f.getName().equals("password");
            }
        }).toString();
    }
    

    The exact format of the toString is determined by the ToStringStyle passed into the constructor.

    • Constructor Detail

      • ReflectionToStringBuilder

        ReflectionToStringBuilder(Object object)
        Constructor.
        Parameters:
        object - the Object to build a toString for, must not be null
      • ReflectionToStringBuilder

        ReflectionToStringBuilder(Object object, ToStringStyle style)
        Constructor.
        Parameters:
        object - the Object to build a toString for, must not be null
        style - the style of the toString to create, may be null
      • ReflectionToStringBuilder

        ReflectionToStringBuilder(Object object, ToStringStyle style, StringBuffer buffer)
        Constructor.
        Parameters:
        object - the Object to build a toString for
        style - the style of the toString to create, may be null
        buffer - the StringBuffer to populate, may be null
      • ReflectionToStringBuilder

        ReflectionToStringBuilder(T object, ToStringStyle style, StringBuffer buffer, Class<out Object> reflectUpToClass, boolean outputTransients, boolean outputStatics)
        Constructor.
        Parameters:
        object - the Object to build a toString for
        style - the style of the toString to create, may be null
        buffer - the StringBuffer to populate, may be null
        reflectUpToClass - the superclass to reflect up to (inclusive), may be null
        outputTransients - whether to include transient fields
        outputStatics - whether to include static fields
    • Method Detail

      • setAppendStatics

         void setAppendStatics(boolean appendStatics)

        Sets whether or not to append static fields.

        Parameters:
        appendStatics - Whether or not to append static fields.
      • setAppendTransients

         void setAppendTransients(boolean appendTransients)

        Sets whether or not to append transient fields.

        Parameters:
        appendTransients - Whether or not to append transient fields.
      • setUpToClass

         void setUpToClass(Class<out Object> clazz)

        Sets the last super class to stop appending fields for.

        Parameters:
        clazz - The last super class to stop appending fields for.
      • toString

         static String toString(Object object)

        Builds a toString value using the default ToStringStyle through reflection.

        It uses AccessibleObject.setAccessible to gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso not as efficient as testing explicitly.

        Transient members will be not be included, as they are likely derived. Static fields will not be included.Superclass fields will be appended.

        Parameters:
        object - the Object to be output
      • toString

         static String toString(Object object, ToStringStyle style)

        Builds a toString value through reflection.

        It uses AccessibleObject.setAccessible to gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso not as efficient as testing explicitly.

        Transient members will be not be included, as they are likely derived. Static fields will not be included.Superclass fields will be appended.

        If the style is null, the default ToStringStyle is used.

        Parameters:
        object - the Object to be output
        style - the style of the toString to create, may be null
      • toString

         static String toString(Object object, ToStringStyle style, boolean outputTransients)

        Builds a toString value through reflection.

        It uses AccessibleObject.setAccessible to gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso not as efficient as testing explicitly.

        If the outputTransients is true, transient members will be output, otherwise theyare ignored, as they are likely derived fields, and not part of the value of the Object.

        Static fields will not be included. Superclass fields will be appended.

        If the style is null, the default ToStringStyle is used.

        Parameters:
        object - the Object to be output
        style - the style of the toString to create, may be null
        outputTransients - whether to include transient fields
      • toString

         static String toString(Object object, ToStringStyle style, boolean outputTransients, boolean outputStatics)

        Builds a toString value through reflection.

        It uses AccessibleObject.setAccessible to gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso not as efficient as testing explicitly.

        If the outputTransients is true, transient fields will be output, otherwise theyare ignored, as they are likely derived fields, and not part of the value of the Object.

        If the outputStatics is true, static fields will be output, otherwise they areignored.

        Static fields will not be included. Superclass fields will be appended.

        If the style is null, the default ToStringStyle is used.

        Parameters:
        object - the Object to be output
        style - the style of the toString to create, may be null
        outputTransients - whether to include transient fields
        outputStatics - whether to include transient fields
      • toString

         static <T> String toString(T object, ToStringStyle style, boolean outputTransients, boolean outputStatics, Class<out Object> reflectUpToClass)

        Builds a toString value through reflection.

        It uses AccessibleObject.setAccessible to gain access to private fields. This means that it willthrow a security exception if run under a security manager, if the permissions are not set up correctly. It isalso not as efficient as testing explicitly.

        If the outputTransients is true, transient fields will be output, otherwise theyare ignored, as they are likely derived fields, and not part of the value of the Object.

        If the outputStatics is true, static fields will be output, otherwise they areignored.

        Superclass fields will be appended up to and including the specified superclass. A null superclass is treated asjava.lang.Object.

        If the style is null, the default ToStringStyle is used.

        Parameters:
        object - the Object to be output
        style - the style of the toString to create, may be null
        outputTransients - whether to include transient fields
        outputStatics - whether to include static fields
        reflectUpToClass - the superclass to reflect up to (inclusive), may be null
      • toStringExclude

         static String toStringExclude(Object object, Collection<String> excludeFieldNames)

        Builds a String for a toString method excluding the given field names.

        Parameters:
        object - The object to "toString".
        excludeFieldNames - The field names to exclude.
      • toStringExclude

         static String toStringExclude(Object object, Array<String> excludeFieldNames)

        Builds a String for a toString method excluding the given field names.

        Parameters:
        object - The object to "toString".
        excludeFieldNames - The field names to exclude
      • isAppendStatics

         boolean isAppendStatics()

        Gets whether or not to append static fields.

      • isAppendTransients

         boolean isAppendTransients()

        Gets whether or not to append transient fields.