projectlombok / lombok

Very spicy additions to the Java programming language.
https://projectlombok.org/
Other
12.81k stars 2.37k forks source link

Allow @NoArgsConstructor to force-initialize final fields with null? #772

Open lombokissues opened 9 years ago

lombokissues commented 9 years ago

Migrated from Google Code (issue 737)

lombokissues commented 9 years ago

:bust_in_silhouette: b.trafficlight   :clock8: Sep 22, 2014 at 17:48 UTC

What steps will reproduce the problem?

  1. Create a class with final fields
  2. Try adding @ NoArgsConstructor - fails because final fields remain not initialized

What is the expected output? What do you see instead?

It would be cool if @ NoArgsConstructor had an attribute which forced a constructor to initialize all final fields with null.

Currently to support Spring/Hibernate entities one needs to write @ RequiredArgsConstructor and then rudimentary:

MyClass() { this(null, null, null); }

...which is a boilerplate code - Lombok's specialization.

What version of the product are you using? On what operating system?

1.12, RHEL5

Please provide any additional information below.

Something like this?

@ NoArgsConstructor(nullFinalFields=true)

lombokissues commented 9 years ago

:bust_in_silhouette: r.spilker   :clock8: Jan 21, 2015 at 01:19 UTC

There's no need for a nullFinalFields parameter; if you explicitly ask for a NoArgsConstructor while there are uninitialized final fields we can safely assume your code won't compile unless we set the fields to null.

lombokissues commented 9 years ago

End of migration

MikePryadko commented 1 year ago

Hi! Is there any news? I'm also interested in this feature. My case: Spring REST, some DTO with (public) final fields. De-serialization is carried out by Jackson. He access fields via reflections so he can set values for my final fields. Second usage of my DTOs is to create it manually via "all-args" constructor, but Jackson can't use it without specifying each properties (I don't want write it and lombok can't generate it so that option is not acceptable). Because of it I want to use "no-args" constructor for Jackson and "all-args" for myself, but I can't use @NoArgsConstructor-- final fields remains uninitialized.

we can safely assume your code won't compile unless we set the fields to null

And for that reason we need "no-args" constructor which will set nulls to final fields! 👓