javalombok

Can Lombok toString() output include newlines?


I would like Lombok's toString() output to put each value on its own line. Enabling it like this would be great:

@ToString(includeNewLines = true)
public class MyClass {
   ...
}  

Anything like that possible?


Solution

  • There is currently no direct @ToString annotation support for output formatting as described in your question. In fact, the Lombok @ToString documentationref includes the following disclaimer:

    We don't promise to keep the output of the generated toString() methods the same between lombok versions. You should never design your API so that other code is forced to parse your toString() output anyway!

    You could use: @ToString(onlyExplicitlyIncluded = true) to suppress normal toString field processing along with @ToString.Include to call an instance (non-static) method that takes no arguments to implement custom formatting, but that pretty much defeats the whole purpose.

    If custom toString output formatting is important to you, a better option is available in the Apache Commons Langref org.apache.commons.lang3.builder package, which provides output format control using the ToStringBuilderapi and ToStringStyleapi classes.