You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lombok's @Builder annotation always adds a builder() method to the annotated class, which can be used to easily create instances with specific values using a fluent interface.
This annotation was later extended with a toBuilder parameter which causes Lombok to also generate a toBuilder() method. This method returns a builder based on the object's current attribute values. A practical usage of this that we use extensively in our entities is to very easily provide a shallow-clone for objects by annotating the class with @Builder(toBuilder = true) and calling toBuilder().build() to get a shallow-cloned instance.
A separate @CopyConstructor annotation has previously been denied with the reasoning that the above pattern is entirely sufficient for performing shallow clones (which is indeed the case).
Our Use Case
We'd like to continue using toBuilder like this to shallow-clone objects; however, the fact that Lombok warns on every single "missing" @Builder.Default annotation is quite annoying. @Builder.Default does not make a lot of sense in combination with toBuilder, as the latter method already copies over the contents of all fields regardless of their initializer.
When the class is annotated with @Builder, Lombok will produce a warning for the above field stating that it will not be initialized when not explicitly set in a builder call like:
MainEntity.builder().affectedUsers(...).build()
This makes sense with standard builder classes, however on classes that are only used with toBuilder to provide easy cloning, warnings on such fields do not make sense, since toBuilder() simply copies over all values from the existing instance anyway, including the already initialized field in the example.
Proposal
Therefore I'd like to propose a parameter onlyToBuilder or something similar for the @Builder annotation, which
leads to no builder() method being added and
no warning being produced for fields with default initializers but no @Builder.Default annotation.
This would allow us to continue using toBuilder() to easily perform shallow clones, but not
add an unused builder() method and
spam the compile output with Builder.Default warnings.
The text was updated successfully, but these errors were encountered:
That feels far too exotic; it breaks the 'it's a parameter party!' rule (which is: If accepting all feature requests of similar exoticness as this one, does the lombok annotation end up having many tens of parameters? Then deny the feature request).
However, I do see the painpoint, I just don't know how to address it without making the annotation grow a few too many parameters.
What about this:
We make it legal to write @Builder(builderMethodName = ""), and we interpret this to mean: Don't make that method at all.
Additionally, if we don't make a builder() method, either because of the above setting, or because you already wrote one yourself, we skip the step where we emit those missing @Default annotation warnings?
Current Functionality
Lombok's
@Builder
annotation always adds abuilder()
method to the annotated class, which can be used to easily create instances with specific values using a fluent interface.This annotation was later extended with a
toBuilder
parameter which causes Lombok to also generate atoBuilder()
method. This method returns a builder based on the object's current attribute values. A practical usage of this that we use extensively in our entities is to very easily provide a shallow-clone for objects by annotating the class with@Builder(toBuilder = true)
and callingtoBuilder().build()
to get a shallow-cloned instance.A separate
@CopyConstructor
annotation has previously been denied with the reasoning that the above pattern is entirely sufficient for performing shallow clones (which is indeed the case).Our Use Case
We'd like to continue using
toBuilder
like this to shallow-clone objects; however, the fact that Lombok warns on every single "missing"@Builder.Default
annotation is quite annoying.@Builder.Default
does not make a lot of sense in combination withtoBuilder
, as the latter method already copies over the contents of all fields regardless of their initializer.Imagine the following field in an entity class:
When the class is annotated with
@Builder
, Lombok will produce a warning for the above field stating that it will not be initialized when not explicitly set in a builder call like:This makes sense with standard builder classes, however on classes that are only used with
toBuilder
to provide easy cloning, warnings on such fields do not make sense, sincetoBuilder()
simply copies over all values from the existing instance anyway, including the already initialized field in the example.Proposal
Therefore I'd like to propose a parameter
onlyToBuilder
or something similar for the@Builder
annotation, whichbuilder()
method being added and@Builder.Default
annotation.This would allow us to continue using
toBuilder()
to easily perform shallow clones, but notbuilder()
method andBuilder.Default
warnings.The text was updated successfully, but these errors were encountered: