Annotation Type ToPrettyString


  • @Documented
    @Target(METHOD)
    public @interface ToPrettyString
    Annotates instance methods that return an easy-to-read String representing the instance. When the method is abstract and enclosed in an AutoValue class, an implementation of the method will be automatically generated.

    When generating an implementation of an @ToPrettyString method, each property of the @AutoValue type is individually printed in an easy-to-read format. If the type of the property itself has a @ToPrettyString method, that method will be called in assistance of computing the pretty string. Non-@AutoValue classes can contribute a pretty string representation by annotating a method with @ToPrettyString.

    Collection and Collection-like types have special representations in generated pretty strings.

    If no @ToPrettyString method is found on a type and the type is not one with a built in rendering, the Object.toString() value will be used instead.

    @ToPrettyString is valid on overridden toString() and other methods alike.

    Example

       @AutoValue
       abstract class Pretty {
         abstract List<String> property();
    
         @ToPrettyString
         abstract String toPrettyString();
       }
    
       System.out.println(new AutoValue_Pretty(List.of("abc", "def", "has\nnewline)).toPrettyString())
       // Pretty{
       //   property = [
       //     abc,
       //     def,
       //     has
       //     newline,
       //   ]
       // }
       }