Default values for builder fields via @Default / @DefaultValue (#227) - #230
Merged
Conversation
…or that it checks if there is a builder or a parameterless constructor
…method-definition and to map them afterwards to generation specific classes
…initionCreator, only on fallback cases it is in RoasterCodeGenerator
Bring the default-value feature up to date with current main (PR #229 JavaDoc-example generation + model refactor is now merged there). Conflicts resolved by taking main's version of the JavaDoc-example / model-refactor code and re-applying the default-value feature on top: - TrackedValue.valueOr / ifSet(...).orElse(...) fluent default API - FieldDto default-value field + isRequired() - CoreMethodsEnhancer applies defaults in build() - BuilderDefinitionCreator + FieldAnnotationExtractor extract @Default/@DefaultValue - README Default Values section, example DTOs, DefaultValueTest Generated example builders regenerated via 'mvn -pl example clean compile' (CI-consistent, no fmt); BookDtoBuilder is byte-identical to main, only the new OrderWithDefaultsBuilder/ProductWithDefaultsBuilder are added. main's strict-mode, resilience and exception-isolation retained. Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Co-Authored-By: Andreas Igel <andreas.igel@computacenter.com>
…s/simple-builders into AndreasIgel/issue227
AndreasIgel
commented
Aug 5, 2026
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



Summary
Implements default values for builder fields (issue #227): a field annotated with
@Default(or any@Default/@DefaultValuewith aString value()) gets that value applied inbuild()when the field was never set. Works for both record constructor parameters and class setter-fields.Why the existing implementation didn't cover this: the builder always constructs via the max-parameter (canonical) constructor and passed
null/0for unset fields, so a smaller delegating record constructor is never selected and there was no "use this when omitted" hook. This PR adds that hook without changing constructor selection.How it works
Default,DefaultValue, any package, e.g. Jakarta REST@DefaultValue). Extracted inBuilderDefinitionCreatorfor both constructor params and the backing field element (FieldAnnotationExtractor), then formatted per field type (String -> quoted,char-> single-quoted, numeric/boolean -> as-is, complex -> raw expression).FieldDtogainsdefaultValue+isRequired()(a defaulted field is never required, even with@NotNull, so no validation error when unset).CoreMethodsEnhancer.build():this.field.valueOr(<default>)this.field.ifSet(result::setX).orElse(<default>)TrackedValuegainsvalueOr(T)and a backward-compatible fluentifSet(consumer).orElse(default)(existingifSet(consumer);callers are unaffected):Docs / examples / tests
ProductWithDefaults(record) andOrderWithDefaults(class) + their committed generated builders.DefaultValueTest(7 cases) andTrackedValueTestadditions.example/pom.xml: registersgenerated-example-builderas a compiled source root viabuild-helper-maven-pluginso the committed generated builders are compiled.Full build green, 312 tests pass; the committed generated example builders regenerate byte-identical to a fresh
mvn -pl example clean compile(generated-source gate passes).