Make @Builder work with both Default and throwing constructor #2878
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, if the constructor throws a checked exception then
@Builder
must be on the constructor in order for the checked exception to propagate to the throws clause of thebuilder()
method. However,@Builder.Default
requires that the@Builder
annotation be set on the class. It is possible to annotate both the class and the constructor, but then the constructor one gets skipped. This means it is currently impossible to use@Builder.Default
simultaneously with a throwing constructor (except perhaps by manually implementing thebuilder()
method), as demonstrated by the tests added in commit d905be5.This attempts to fix that. If you set
@Builder
on both the class and the constructor, the constructor's checked exceptions will now be added to thebuilder()
method initially generated by the class annotation. I have also verified that this works in the real-world context where I encountered the issue.