Generating Java Bean setters in Eclipse

China☆狼群 提交于 2019-12-03 15:39:09
Austin D

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.

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});
}

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!