Open jearton opened 5 years ago
What's wrong with the existsing:
@Setter
@Accessors(chain = true)
public class User {
private String id;
private String name;
public static void main(String[] args) {
User user = new User()
.setId("42")
.setName("Douglas Adams");
}
}
I want to keep the setter methods returning void
. And then add extra fluent setter methods to return this
. A framework of my company can only recognize the standard setter methods of returning void. -_-!
You cant have both.
void setId(String id)
String setId(String id)
Both methods have the same signature and can't coexist.
Maybe @Wither is your way? https://projectlombok.org/features/experimental/Wither
No he wants
void setId(String id);
User id(String id);
I don't want setters with returning "this" but I would like have both a setter void setXXXX(Param param)
and a fluent method ClassName XXX(Param param)
User user = new User().id("id");
user.setId("id");
Yes, the best solution would be having traditional getters/setters along with fluent setters (without the "set" prefix). Non-void setXXX methods break the JavaBean specification and are not accepted by many frameworks (see, for example, @Accessors(fluent = true) does not work with Jakson and Spring ConfigurationProperties to work with fluent setters or custom setters) so they should be simply discarded. This feature has been requested multiple times, both here (https://github.com/projectlombok/lombok/issues/1595 and https://github.com/projectlombok/lombok/issues/3265) and on Stack Overflow but yet nothing happened.
Having two setter methods for every field is definitely a bad practice/code smell. Lombok aims at helping devs in writing well-designed classes. So IMO this is not something Lombok should support (but I'm not a Lombok maintainer, so it's not up to me).
Most frameworks support this
-returning setters out of the box or can be configured in this way.
If your framework does not, file a feature request or pull request.
BTW: I'd like to know which (public) frameworks are affected by this kind of problem. E.g., if it's Jackson, we can talk about an extension to @Jacksonized
to automatically configure Jackson such that fluent and/or chainable setters work.
We don't like this. JanRieke correctly explained why.
BTW: Jackson indeed has problems with fluent accessors (chained is fine though).
I think we can upgrade @Jacksonized
to mitigate this. I'll investigate this in the next weeks (can't promise too much here, I'm rather busy ATM).
Hi, I got across this topic because we would like to use fluent getters but javabeans setters with Lombok.
So a @FluentGetter
would be also nice to have next to @FluentSetter
.
Describe the feature
will generate:
Describe the target audience
The standard setter method is useful to Json serialization tool like
jackson
. And fluent setter method is developer friendly when coding.