Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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;
}
like image 334
Jin Kwon Avatar asked Apr 12 '16 05:04

Jin Kwon


People also ask

How do you use Lombok to create getters and setters?

You can annotate any field with @Getter and/or @Setter , to let lombok generate the default getter/setter automatically. A default getter simply returns the field, and is named getFoo if the field is called foo (or isFoo if the field's type is boolean ).

What are Lombok accessors?

The @Accessors annotation is used to configure how lombok generates and looks for getters, setters, and with-ers. By default, lombok follows the bean specification for getters and setters: The getter for a field named pepper is getPepper for example.

What are Lombok annotations?

Project Lombok (from now on, Lombok) is an annotation-based Java library that allows you to reduce boilerplate code. Lombok offers various annotations aimed at replacing Java code that is well known for being boilerplate, repetitive, or tedious to write.


1 Answers

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;
    }
}
like image 56
Arek Kubiak Avatar answered Sep 28 '22 11:09

Arek Kubiak