projectlombok / lombok

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

[BUG] @Accessors doesn't support inherted class well #3273

Open asngeo opened 2 years ago

asngeo commented 2 years ago

Describe the bug The @Accessors doesn't support inherted class well. It can't return the subclass type when calling a setter of superclass on a subclass instance.

To Reproduce For example, there are two classes:

@Data
@Accessors(fluent = true, chain = true)
class Parent {
    private int id;
}

@Data
@Accessors(fluent = true, chain = true)
class Child extends Parent {
    private String name;
}

I can't do such things: new Child().id(1).name("name"); because the definition of the id setter actually is: public Parent id(int id).

Expected behavior I hope the usage like new Child().id(1).name("name"); can work. I think that we can use covarient type to override the chain-setters in subclasses just like public Child id(int id).

Version info (please complete the following information):

Thanks, Geo

asngeo commented 2 years ago

Could anyone take a look for this issue? Many thanks!

janrieke commented 2 years ago

It's not possible at the moment.

Either you need resolution to find out which methods to override in a subclass, or some sophisticated concept of a SELF type that you can use as return type for the chainable setter methods. Both do not exist in Lombok and will probably not in the foreseeable future.