Search code examples
javaaccessorlombok

How can I generate both standard accessors and fluent accessors with lombok?


I tried this.

@lombok.Getter
@lombok.Setter
@lombok.Accessors(chain = true, fluent = true)
private String prop;

And @Accessor took precedence and getProp and setProp are not generated.

How can I make it generate this?

public String getProp() {
    return prop;
}

public String prop() {
    //return prop;
    return getProp(); // wow factor
}

public void setProp(String prop) {
    this.prop = prop;
}

public Some prop(String prop) {
    //this.prop = prop;
    setProp(prop); // wow factor, again
    return this;
}

Solution

  • Unfortunately this is impossible. You need to implement own getters and setters, and add @Getter @Setter and @Accessors(fluent = true) annotaions to achieve this.

    @Getter
    @Setter
    @Accessors(fluent = true)
    public class SampleClass {
        private int id;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    }
    

    In result you will have class like:

    public class SampleClass {
        private int id;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int id(){
            return id;
        }
    
        public SampleClass id(int id){
            this.id=id;
            return this;
        }
    }