Builder pattern for Java records

- liangchenblue at gmail.com
Fri May 21 19:16:56 UTC 2021


As far as I see, builders are more useful when there are too many arguments
or a lot of them have default values. These needs aren't as significant as
on records where few components are inferred.

In addition, using builders may lead to extra object allocation and
increase distribution size for the extra bytecode. So I guess people can
use an annotation processor to generate record builders optionally than
mandating a builder type for every record.

On Friday, May 21, 2021, Alberto Otero Rodríguez <albest512 at hotmail.com>
wrote:
> Hi, I have found this project on GitHub which creates a Builder for Java
records:
> https://github.com/Randgalt/record-builder
> [
https://opengraph.githubassets.com/a4e3a7b3c7b16b51e0854011fe4b031577bcc09919058baef412b03613295d20/Randgalt/record-builder
]<https://github.com/Randgalt/record-builder>
> GitHub - Randgalt/record-builder: Record builder generator for Java
records<https://github.com/Randgalt/record-builder>
> The target package for generation is the same as the package that
contains the "Include" annotation. Use packagePattern to change this (see
Javadoc for details).. Usage Maven. Add the dependency that contains the
@RecordBuilder annotation. < dependency > < groupId
>io.soabase.record-builder</ groupId > < artifactId >record-builder-core</
artifactId > < version >set-version-here</ version ...
> github.com
>
> And I was wondering if this could be made by default in all Java Records.
>
> Technically a Builder is only used when creating the Object so I think is
possible and, if Java creates it by default, would be less error prone than
creating it manually.
>
> Regards,
>
> Alberto.
>


More information about the core-libs-dev mailing list