在Eclipse中生成Java Bean setter [英] Generating Java Bean setters in Eclipse

查看:385
本文介绍了在Eclipse中生成Java Bean setter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在我工作的一些项目中使用Java bean,这意味着很多像这样的手工样板代码。

We use Java beans on some projects where I work, this means a lot of handcrafted boilerplate code like this.

我是在使用Eclipse插件,或者是配置Eclipse代码模板的方法,允许开发人员从简单的骨架类生成setter,类似于'Generate Getters and Setters'为POJO做的。

I'm after an Eclipse plugin, or a way of configuring Eclipse code templates that allows a developer to generate the setters from a simple skeleton class, in a similar fashion to the 'Generate Getters and Setters' does for POJOs.

输入

public class MyBean {
    private String value;
}

预期产出

 public class MyBean {
     private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

     private String value;

     public String getValue() {
         return this.value;
     }

     public void setValue(String newValue) {
         String oldValue = this.value;
         this.value = newValue;
         this.pcs.firePropertyChange("value", oldValue, newValue);
     }

     [...]
 }



<我知道项目Lombok,但我更喜欢坚持纯Java / Eclipse方法。

I'm aware of project Lombok, but I'd prefer to stick to a pure Java/Eclipse based approach.

我正在考虑为此自己编写一个Eclipse插件,真正有用的是Eclipse中一个更强大的模板插件,可以解决这个问题和其他问题。

I'm considering writing an Eclipse plugin for this myself, what would be really useful is a more powerful template plugin in Eclipse, that could solve this problem and others.

推荐答案

这是一个使用Eclipse代码模板的简单解决方案。此回复基于此答案,该答案还提供了设置 PropertyChangeSupport 。我只是提供了有关设置过程的其他详细信息。

Here's a simple solution that uses the Eclipse code templates. This response is based on this answer which also provides a template for setting up the PropertyChangeSupport. I've simply provided additional details regarding the setup process.

在Eclipse中,选择 Windows>首选项> Java>编辑器>模板>新建。使用明确的名称添加以下代码模板,例如 BeanProperty

In Eclipse, select Windows > Preferences > Java > Editor > Templates > New. Add the following code template using an unambiguous name, e.g. BeanProperty:

private ${Type} ${property};

public ${Type} get${Property}() {
    return ${property};
}

public void set${Property}(${Type} ${property}) {
    ${propertyChangeSupport}.firePropertyChange("${property}", this.${property}, this.${property} = ${property});
}

现在,只需输入 BeanProperty 在目标类中,按 Ctrl + Space 显示模板建议,然后选择 BeanProperty 模板。您可以使用选项卡键循环显示字段类型,字段名称和getter / setter名称。按输入以应用更改。

Now, simply type BeanProperty in your target class, press Ctrl+Space to show the Template Proposals, and select the BeanProperty template. You can cycle through the field type, field name, and getter/setter names using your tab key. Press Enter to apply your changes.

参见使用代码模板帮助条目以获取更多详细信息,以及这个问题,用于其他有用的代码模板。当然,这个解决方案仍然受到Eclipse的限制,并且需要一个类似于auto getter / setter工具的更强大的插件。

See the Using Code Templates Help entry for more details, and this question for additional, useful code templates. Of course, this solution is still bound by the limitations of the Eclipse, and a more powerful plugin akin to the auto getter/setter tool would be desirable.

这篇关于在Eclipse中生成Java Bean setter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆