Builder adhering to @RequiredArgsConstructor

26 views
Skip to first unread message

Morten Sabroe Mortensen

unread,
Mar 30, 2024, 7:23:51 PMMar 30
to Project Lombok

Hi

It would be of great value if @Builder upon a class would adhere to constructors generated by @RequiredArgsConstructor and not only to constructors generated by @AllArgsConstructor.

I hit this situation frequently. The only solution is to create a constructor explicitly and put the @Builder on this constructor. This has a price:

-- I do not get the benefit from having @RequiredArgsConstructor generate the constructor
-- I do not get the benefit from @Builder.Default
-- I have to test for and set default values explicitly (if not in my constructor then in a declared builder-class).

It would be nice if @Builder on a class would adhere to "the constructor with the highest argument count" or, say, just adhere to @RequiredArgsConstructor and given that no @AllArgsConstructor has been set and there has not been declared any explicit constructor with more arguments.

The documentation is absolutely strictly correct:

Finally, applying @Builder to a class is as if you added @AllArgsConstructor(access = AccessLevel.PACKAGE) to the class and applied the @Builder annotation to this all-args-constructor.

/Morten Sabroe Mortensen


Reply all
Reply to author
Forward
0 new messages