Randgalt / record-builder

Record builder generator for Java records
Apache License 2.0
723 stars 51 forks source link

Allow subclassing of generated Builder #157

Closed KangoV closed 8 months ago

KangoV commented 10 months ago

I'd like to subclass the generated builder in order to add more quality of life methods, e.g.:

public record Grouping(
    UUID id,
    String name,
    String description,
    String context,
    List<String> objectRefs )  {

    public static class Builder extends GroupingBuilder {
        public Builder id(String id) {
            this.id(UUID.fromString(id));
            return this;
        }
    }

}
epkugelmass commented 6 months ago

Calling any other method on Grouping.Builder other than id(String id) returning a GroupingBuilder not a Grouping.Builder so I don't think this actually very helpful. The more general way of doing this (because Java does not have a true return self feature) is to parameterize GroupingBuilder<T extends GroupingBuilder> and then have class Builder extends GroupingBuilder<Builder> {}

@Randgalt would you be open to supporting this? I can provide the PR if needed.

Randgalt commented 6 months ago

@epkugelmass yes, a PR would be welcome but it must be behind an option. It will be complex to get this right as there are already a ton of options to contend with.