Ada*_*dam 5 java eclipse javabeans
我在我工作的一些项目中使用Java bean,这意味着很多像这样的手工样板代码.
我正在使用Eclipse插件,或者是一种配置Eclipse代码模板的方法,允许开发人员从简单的骨架类生成setter,其方式与"Generate Getters and Setters"对POJO类似.
输入
public class MyBean {
private String value;
}
Run Code Online (Sandbox Code Playgroud)
预期产出
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);
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
我知道项目Lombok,但我更喜欢坚持纯Java/Eclipse方法.
我正在考虑为此自己编写一个Eclipse插件,真正有用的是Eclipse中一个更强大的模板插件,可以解决这个问题和其他问题.
这是一个使用 Eclipse 代码模板的简单解决方案。此响应基于此答案,该答案还提供了用于设置PropertyChangeSupport. 我只是提供了有关设置过程的更多详细信息。
在 Eclipse 中,选择Windows > 首选项 > Java > 编辑器 > 模板 > 新建。使用明确的名称添加以下代码模板,例如BeanProperty:
private ${Type} ${property};
public ${Type} get${Property}() {
return ${property};
}
public void set${Property}(${Type} ${property}) {
${propertyChangeSupport}.firePropertyChange("${property}", this.${property}, this.${property} = ${property});
}
Run Code Online (Sandbox Code Playgroud)
现在,只需输入BeanProperty您的目标类别,按Ctrl+Space显示模板建议,然后选择BeanProperty模板。您可以使用密钥循环浏览字段类型、字段名称和 getter/setter 名称tab。按Enter应用您的更改。
有关更多详细信息,请参阅使用代码模板帮助条目,有关其他有用的代码模板,请参阅此问题。当然,这个解决方案仍然受到 Eclipse 的限制,并且需要一个类似于自动 getter/setter 工具的更强大的插件。
| 归档时间: |
|
| 查看次数: |
284 次 |
| 最近记录: |