Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explain builder default gotchas #3560

Merged
merged 1 commit into from
Jan 11, 2024
Merged

Conversation

mjaggard
Copy link
Contributor

@mjaggard mjaggard commented Dec 1, 2023

No description provided.

@@ -66,7 +66,7 @@
</p><p>
Now that the "method" mode is clear, putting a <code>@Builder</code> annotation on a constructor functions similarly; effectively, constructors are just static methods that have a special syntax to invoke them: Their 'return type' is the class they construct, and their type parameters are the same as the type parameters of the class itself.
</p><p>
Finally, applying <code>@Builder</code> to a class is as if you added <code>@AllArgsConstructor(access = AccessLevel.PACKAGE)</code> to the class and applied the <code>@Builder</code> annotation to this all-args-constructor. This only works if you haven't written any explicit constructors yourself. If you do have an explicit constructor, put the <code>@Builder</code> annotation on the constructor instead of on the class. Note that if you put both `@Value` and `@Builder` on a class, the package-private constructor that `@Builder` wants to generate 'wins' and suppresses the constructor that `@Value` wants to make.
Finally, applying <code>@Builder</code> to a class is as if you added <code>@AllArgsConstructor(access = AccessLevel.PACKAGE)</code> to the class and applied the <code>@Builder</code> annotation to this all-args-constructor. This only works if you haven't written any explicit constructors yourself or created one with @NoArgsConstructor. If you do have an explicit constructor, put the <code>@Builder</code> annotation on the constructor instead of on the class. Note that if you put both `@Value` and `@Builder` on a class, the package-private constructor that `@Builder` wants to generate 'wins' and suppresses the constructor that `@Value` wants to make.
Copy link

@feech feech Dec 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

chatGPT rephrases it:
Applying @Builder to a class is equivalent to adding @AllArgsConstructor(access = AccessLevel.PACKAGE) to the class and applying the @Builder annotation to this all-args constructor. This mechanism is effective only when explicit constructors or constructors created with @NoArgsConstructor are absent. If an explicit constructor is present, it's recommended to place the @Builder annotation on the constructor instead of the class. It's worth noting that when both @Value and @Builder are used in a class, the package-private constructor intended by @Builder takes precedence and suppresses the constructor specified by @Value

As non-native speaker i find it's easier to understand.

@feech
Copy link

feech commented Dec 1, 2023

Thanks, Mat
It looks good to me.

@rzwitserloot rzwitserloot merged commit 9d9df38 into projectlombok:master Jan 11, 2024
45 checks passed
@rzwitserloot
Copy link
Collaborator

I accepted this then immediately updated the text. The gotcha on the gotcha is: @RequiredArgsConstructor can also cause this 'issue' if you try real hard (by using @NonFinal, for example). See commit 84cb36d.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants