Randgalt / record-builder

Record builder generator for Java records
Apache License 2.0
723 stars 51 forks source link

Adds RecordBuilder.Options.defaultNotNull parameters #185

Open agustafson opened 1 month ago

agustafson commented 1 month ago

Thanks for putting together record-builder! It's definitely a welcome addition and enhancement on top of records.

Issue/Enhancement

Some codebases expect fields to be non-null by default, for example if NullAway enforcement is in place. Therefore it is useful to set a parameter to expect that all fields will be non-null unless explicitly marked as nullable. Currently our codebase uses https://immutables.github.io/immutable.html which defaults to non-null fields, and it would be good to have record-builder have the capability for the same behaviour.

Changes

Adds the ability to default fields to be non-null, unless they are annoted with a nullable annotation, ie:

@RecordBuilder.Options(defaultNotNull = true/false)

Example

For example:

@RecordBuilder.Options(defaultNotNull = true)
@RecordBuilder
public record RecordWithDefaultNotNull(Integer notNullInteger, String notNullString, @Null String nullString) {
}

will check/ensure that notNullInteger and notNullString are not null via requireNonNull calls.

Questions