Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse plugin for Fluent API methods

I'm looking for an eclipse plugin that can generate fluent API methods in my beans.

For instance, given this bean:

public class MyBean {

    private String name;

    private int age;

    //Setters and getters
}

Is there any eclipse plugin that generates these methods for me?

public class MyBean {

    private String name;

    private int age;

    public MyBean withName(String name) {
        setName(name);
        return this;
    }

    public MyBean withAge(int age) {
        setAge(age);
        return this;
    }

    //Setters and getters
}

I've found a google plugin that generates Builder objects, but I prefer fluent API inside each Bean class.

like image 617
jfcorugedo Avatar asked Feb 15 '26 18:02

jfcorugedo


1 Answers

While can't find anything, you can do like me.

Generate the setters, then "Find" (checking "regular expressions") for:

\tpublic void set(.+)\((.+)\) \{\R\t\tthis\.(.+) = (.+);\R\t\}

 and replace with:

\tpublic [PUT_TYPE_HERE] with$1\($2\) \{\R\t\tthis\.$3 = $4;\R\t\treturn this;\R\t\}

Probably there's a simpler expression, but this works ;)

[UPDATE] @ 07-MAR-2018

I'm now using lombok which generates getters, setters and builders throught simple annotations. (@Getter, @Setter and @Builder respectively)

It can generate with methods using the @Wither annotation too, but unfortunately its an experimental feature so it should be avoided.

like image 59
Pedro Silva Avatar answered Feb 17 '26 07:02

Pedro Silva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!