When I'm using Jackson to create payloads for a third party API, I often end up with lots of nested classes, each annotated with @Data and @Builder, eg:
(Annotations and "public static ..." left off for readability)
class Foo {
private Bar bar;
class Bar {
private Baz;
class Baz {
private String name;
}
}
}
So, to construct a Foo, I would do something like:
Foo foo = Foo.builder()
.bar(Foo.Bar.builder()
.baz(Foo.Bar.Baz.builder()
.name("Ed")
.build()
)
.build()
)
.build();
Which, no doubt, is great. However, when you take into account that most class names are longer than three characters, you may be fully qualifying them with package names, and some of these payloads get nested more than three levels deep -- it gets a little tedious.
Would it be possible for Lombok, when generating the Builder code -- to generate an additional method for any member it finds whose type is also annotated with @Builder?
for example, the Builder for Foo would have two methods:
public Foo.Builder bar(Bar bar);
and either:
public Foo.Builder bar(Function<Bar.Builder, Bar> f);
or
public Foo.Builder bar(Function<Bar.Builder, Bar.Builder> f);
(in the latter case, it would save you from typing ".build()" -- Lombok could just call it for you)
Then, the above code (assuming the latter of the two methods above) could become:
Foo foo = Foo.builder()
.bar( (b)->
b.baz((b)->
)
).build();
With some Java warnings about "b" shadowing a variable already in scope (or is that an error? getting my languages mixed up).
Anyway, this would greatly simplify some code I've got at the moment.
Thank you for making Lombok!
Ed