Feature Proposal - Enhanced Null-Protection for Objects Created Outside of Builder Pattern

97 views
Skip to first unread message

Kirill Andreev

unread,
Nov 29, 2023, 9:00:11 PM11/29/23
to Project Lombok


Hello Lombok Community,

I hope this message finds you well. I've been exploring Lombok's capabilities, particularly the @Builder annotation, and I've identified a scenario where enhanced null-protection could greatly improve compatibility with external libraries, such as Gson.

Problem Statement:

While Lombok's @Builder annotation efficiently handles null-protection when using @Builder.Default, certain libraries, like Gson, create objects without utilizing builders. In such cases, the absence of a default/no-argument constructor poses challenges, as these libraries rely on this type of constructor for object instantiation.

Proposed Solution:

I suggest enhancing the @Builder functionality in Lombok to automatically generate a default/no-argument constructor. This constructor would initialize fields with their respective @Builder.Default values, ensuring consistent null-protection even when objects are created outside of the builder pattern.

Mat Jaggard

unread,
Nov 30, 2023, 6:38:56 PM11/30/23
to project-lombok
Hi Kirill,
@NoArgsConstructor  already does exactly that.

Thanks,
Mat.

--
You received this message because you are subscribed to the Google Groups "Project Lombok" group.
To unsubscribe from this group and stop receiving emails from it, send an email to project-lombo...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/project-lombok/217b0108-58b4-449d-86d8-1ed9a74e19d5n%40googlegroups.com.

Kirill Andreev

unread,
Dec 1, 2023, 4:57:12 AM12/1/23
to Project Lombok
Thanks Mat

You are partially correct - @NoArgConstructor will generate a constructor with no parameters.
However, "fields are initialized with 0 / false / null". It does not say it respects @Builder.Default values.

Here is the example:
@NoArgsConstructor
@Builder
public class Book {
@Builder.Default
public final String title = "Book";
@Builder.Default
public final String author = "Agatha christie";
}

The class is failed to compile. Here is the error:
java: constructor Book in class app.Book cannot be applied to given types;
  required: no arguments
  found:    java.lang.String,java.lang.String
  reason: actual and formal argument lists differ in length 

I am trying with 1.18.30
Let me know if I am missing something

Mat Jaggard

unread,
Dec 1, 2023, 5:32:24 AM12/1/23
to project...@googlegroups.com
Got you. That's because by defining `@NoArgsConstructor` you're explicitly creating a constructor which `@Builder` will then use - so you have no parameters. You need to specify all 3 - `@NoArgsConstructor` `@AllArgsConstructor` and `@Builder`.

I forgot about that because I normally use `@SuperBuilder` which doesn't have this issue. It always creates a new constructor which just takes the builder object.

Kirill Andreev

unread,
Dec 1, 2023, 5:49:32 AM12/1/23
to Project Lombok
Thank you, Mat!

I appreciate your help, and the solution works perfectly for my needs. I believe this valuable information could benefit others in the Lombok community. Is it possible to include this in the documentation? I believe a shot note for Builder annotation would be sufficient. 

Thanks again,

Mat Jaggard

unread,
Dec 1, 2023, 6:08:38 AM12/1/23
to project...@googlegroups.com
I've created a PR to attempt to explain this. https://github.com/projectlombok/lombok/pull/3560

(Also, note to the maintainers of Lombok, I have another outstanding PR https://github.com/projectlombok/lombok/pull/3548 )

Reply all
Reply to author
Forward
0 new messages