Open steve-todorov opened 1 year ago
what if I'd like to always print just the short version
Then you'd use ToString.Exclude
like you said
called
@ToString.Using("...")
You're just redefining toString()
at that point, so do that instead.
keeping the output format consistent with how lombok generates it and of course you shouldn't forget to update the string every time you make a change to the Car properties
Isn't that the same problem with the proposed annotation?
Depending on the context
Such as? I feel like the better pattern here would be
public class ShortStringFactory {
public static String shorten(Manufacture m) {
}
public static String shorten(Car c) {
}
}
Describe the feature
Consider the following example:
Depending on the
context
you are using theManufacture
andModel
classes, you will always get the sametoString()
representation which contain all possible fields. This might be what you need. But what if I'd like to always print just the short version of theManufacture
andModel
classes when I doCar.toString()
?As of writing this there is no way to do this other than implementing the
Car.toString()
method from scratch and go through the tedious process of keeping the output format consistent with howlombok
generates it and of course you shouldn't forget to update the string every time you make a change to theCar
properties.I believe this could be fixed by introducing a new annotation called
@ToString.Using("toShortString")
. Similarly to the@ToString.Include/Exclude
, the@ToString.Using
annotation should be placed over a field/property which would helplombok
to know which method it should use when generating the string representation of the object.Looking at the example above, when you do
Car.toString()
you would get the short version ofManufacture
, but when you doManufacture.toString()
you still get the regular output which might be more relevant depending on thecontext
you are calling it.Describe the target audience
This feature might be considered as a "corner case" by some. Nevertheless I truly believe having this possibility would be very beneficial and time saving to everybody in the community, because it would allow slight changes to be applied without having to start from scratch every time.