projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.83k stars 2.37k forks source link

[FEATURE] Add @ToString.Using() annodation to allow using other object methods instead of Object.toString() #3307

Open steve-todorov opened 1 year ago

steve-todorov commented 1 year ago

Describe the feature

Consider the following example:


@Data
@ToString
class Car {

    @ToString.Using(value = "toShortString")
    private final Manufacture manufacture;

    @ToString.Using(value = "toShortString")
    private final Model model;

}

@Data
@ToString
class Manufacturer {
    private final UUID uuid;
    private final String name;

    // assume 30 more fields containing all sorts of information.

    public String toShortString() {
       return "{uuid = "+ UUID + ", name = "+name+", ...}";
    }
}

@Data
@ToString
class Model {
    private final UUID uuid;
    private final String name;

    // assume 30 more fields containing all sorts of information.

    public String toShortString() {
       return "{uuid = "+ UUID + ", name = "+name+", ...}";
    }
}

Depending on the context you are using the Manufacture and Model classes, you will always get the same toString() 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 the Manufacture and Model classes when I do Car.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 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.

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 help lombok 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 of Manufacture, but when you do Manufacture.toString() you still get the regular output which might be more relevant depending on the context 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.

OneCricketeer commented 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) {

  }
}