27

I have an Entity

@Builder
class MyEntity {
   private Set<OtherEntitiy> children = new HashSet<>()
}

And i get a lombok warning.

warning: @Builder will ignore the initializing expression entirely. If you want the initializing expression to serve as default, add @Builder.Default. If it is not supposed to be settable during building, make the field final. Set = new HashSet<>();

The question is: how can i suppress lombok's warning?

Also. I need to initialize children because i want to avoid NullPointerException. Also i can't mark this filed as final because it is not final really. I cant mark filed @Builder.Default because i wanna create this entity not only with builder and i wanna to save default value for other constructors.

dpr
  • 10,591
  • 3
  • 41
  • 71
Bukharov Sergey
  • 9,767
  • 5
  • 39
  • 54
  • 2
    I think Lombok is trying to tell you that it always overwrites that value. https://github.com/rzwitserloot/lombok/issues/916 I don't really know Lombok very well but a workaround could be to target a constructor with `@Builder` instead of the class. – Radiodef Sep 05 '17 at 14:34
  • I think the warning is pretty self explanatory and you should think of fixing the warning by following the suggestions in the message instead of trying to suppress it. – dpr Sep 05 '17 at 15:01
  • @dpr you are right. First we need think how to fix the issue. But in this case suggested decision is not acceptable. I had explain it in my question above. – Bukharov Sergey Sep 05 '17 at 15:20
  • 1
    Just some ideas: 1. You could add `@AllArgsConstructor(access=AccessLevel.PRIVATE)` and remove the initialization. The Builder will do the initialization and you wan't call you private ctor. +++ 2. You maybe could use `@Singular`. +++ 3. The field should probably be final as you can do everything by modifying the collection instead of replacing it. – maaartinus Sep 05 '17 at 18:53

1 Answers1

38

Use @Builder.Defaultto add default behavior for your Builder

@Builder
class MyEntity {
   @Builder.Default
   private Set<String> children = new HashSet<>();
}

You use it on the field that has a default value defined an Lombok
will then pick up the value during object creation

@Builder.Default functionality was added in lombok v1.16.16. So if you're using lower version of Lombok you will not be able to use it.

Daniel Taub
  • 5,133
  • 7
  • 42
  • 72
  • 11
    how can this be the accepted answer when the OP says "I cant mark filed @Builder.Default because..." ? – Niccolò Sep 18 '18 at 07:57
  • 2
    There was a known issue/bug with this in v1.16.16. Here's the link: https://github.com/rzwitserloot/lombok/issues/1347. This annotation was removing the default initialization from the field declaration, which broke the no-args constructor if you had it because using it would not initialize the fields anymore. – tomosius Sep 19 '18 at 14:00
  • 1
    Additional info about a potential problem with using Builder.Default: https://stackoverflow.com/questions/47883931/default-value-in-lombok-how-to-init-default-with-both-constructor-and-builder – tomosius Sep 19 '18 at 14:07
  • 1
    @tomosius And it's still broken. Builder.Default is unusable if any other constructor is needed (e.g. for JSON deserialization). – kevin cline Jan 28 '20 at 02:28