projectlombok / lombok

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

[FEATURE] Add support for setter method coexisting with fluent setter method. #2141

Open jearton opened 5 years ago

jearton commented 5 years ago

Describe the feature

@Setter
@FluentSetter
public class User {
    private String id;
}

will generate:

public class User {

    private String id;

    public void setId(String id) {
        this.id = id;
    }

    public User id(String id) {
        this.id = id;
        return this;
    }
}

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.

janmaterne commented 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");
    }

}
jearton commented 5 years ago

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. -_-!

janmaterne commented 5 years ago

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

Saljack commented 4 years ago

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");
pnavato commented 10 months ago

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.

janrieke commented 10 months ago

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.

janrieke commented 10 months ago

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.

rzwitserloot commented 9 months ago

We don't like this. JanRieke correctly explained why.

janrieke commented 9 months ago

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).

adnsimona commented 7 months ago

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.