From 4fb9c6f7f82d020a9ebcae9816e911ecd4c047a4 Mon Sep 17 00:00:00 2001
From: Reinier Zwitserloot
By setting the includeFieldNames
parameter to true you can add some clarity (but also quite some length) to the output of the toString()
method.
- By default, all non-static fields will be printed. If you want to skip some fields, you can name them in the exclude
parameter; each named field will not be printed at all. Alternatively, you can specify exactly which fields you wish to be used by naming them in the of
parameter.
+ By default, all non-static fields will be printed. If you want to skip some fields, you can annotate these fields with @ToString.Exclude
. Alternatively, you can specify exactly which fields you wish to be used by using @ToString(onlyExplicitlyIncluded = true)
, then marking each field you want to include with @ToString.Include
.
By setting callSuper
to true, you can include the output of the superclass implementation of toString
to the output. Be aware that the default implementation of toString()
in java.lang.Object
is pretty much meaningless, so you probably don't want to do this unless you are extending another class.
+
+ You can also include the output of a method call in your toString
. Only instance (non-static) methods that take no arguments can be included. To do so, mark the method with @ToString.Include
.
+
+ You can change the name used to identify the member with @ToString.Include(name = "some other name")
, and you can change the order in which the members are printed via @ToString.Include(rank = -1)
. Members without a rank are considered to have rank 0, members of a higher rank are printed first, and members of the same rank are printed in the same order they appear in the source file.
Arrays are printed via Arrays.deepToString
, which means that arrays that contain themselves will result in StackOverflowError
s. However, this behaviour is no different from e.g. ArrayList
.
- Attempting to exclude fields that don't exist or would have been excluded anyway (because they are static) results in warnings on the named fields. You therefore don't have to worry about typos. + If a method is marked for inclusion and it has the same name as a field, it replaces the toString output for that field (the method is included, the field is excluded, and the method's output is printed in the place the field would be printed). +
+ Prior to lombok 1.16.22, inclusion/exclusion could be done with the of
and exclude
parameters of the @ToString
annotation. This old-style inclusion mechanism is still supported but will be deprecated in the future.
- Having both exclude
and of
generates a warning; the exclude
parameter will be ignored in that case.
+ Having both @ToString.Exclude
and @ToString.Include
on a member generates a warning; the member will be excluded in this case.
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!
- By default, any variables that start with a $ symbol are excluded automatically. You can only include them by using the 'of' parameter.
+ By default, any variables that start with a $ symbol are excluded automatically. You can only include them by using the @ToString.Include
annotation.
If a getter exists for a field to be included, it is called instead of using a direct field reference. This behaviour can be suppressed:
@ToString(doNotUseGetters = true)
--
cgit