用于Fluent API方法的Eclipse插件

时间:2015-05-05 08:44:27

标签: java eclipse eclipse-plugin fluent fluent-interface

我正在寻找一个可以在我的bean中生成流畅的API方法的eclipse插件。

例如,给定这个bean:

public class MyBean {

    private String name;

    private int age;

    //Setters and getters
}

有没有为我生成这些方法的eclipse插件?

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
}

我发现google plugin生成Builder个对象,但我更喜欢每个Bean类中的流畅API。

1 个答案:

答案 0 :(得分:3)

虽然找不到任何东西,但你可以像我一样。

生成设置者,然后"查找" (检查"正则表达式"):

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

并替换为:

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

可能有一个更简单的表达,但这有效;)

[更新] @ 07-MAR-2018

我现在正在使用lombok通过简单的注释生成getterssettersbuilders。 (分别为@Getter@Setter@Builder

它也可以使用with注释生成@Wither方法,但不幸的是它是一个实验性功能,因此应该避免使用。

相关问题