2012-01-17 64 views
5

对于MVC模型类,我的setter方法是这样的:Eclipse的模板()

enum BoundProperty {FIELD_NAME, ...} 

private Type fieldName; 

public setFieldName(Type newValue) { 
    Type oldValue = fieldName; 
    fieldName = newValue; 
    firePropertyChange(BoundProperty.FIELD_NAME, oldValue, newValue); 
} 

给定一个领域,这可以从输出自动生成的制定者产生?如果没有,有没有办法从模板获取这个输出?

输出结果应该是CamelCase的字段名以产生方法名,所以fieldName生成setFieldName()和Uppercase字段名来产生属性枚举。

所以fieldName生成FIELD_NAME(或FIELDNAME也可以)。

+1

它看起来像[这个答案](http://stackoverflow.com/a/7237108/274350)做一些我在找的东西。 – 2012-01-17 13:50:21

回答

1

我可以在“Generate getters/setters”对话框中看到此消息。 获取者/设置者的格式可以在代码模板首选项页面上配置。你可以去那里(Setter BodyCode部分)并修改如下。

Type oldValue = ${field}; 
${field} = ${param}; 
firePropertyChange(BoundProperty.FIELD_NAME, oldValue, ${param}); 

但是,它不会生成BoundProperty虽然。它需要更多的研究来确定它是否可行。这些链接可以帮助

Useful Eclipse Java Code TemplatesGetting started with Eclipse code templates

+0

感谢您的回复。我尝试过,但这还不够 - Type应该是字段的类型,FIELD_NAME应该是fieldName的大写版本。似乎没有办法从代码模板屏幕中提供的变量中获取这些变量。 – 2012-01-17 13:39:51

+0

我刚刚在答案中加入了一些链接。也许你可以在那里得到新的变量? – Reddy 2012-01-17 13:41:03

+0

顺便说一句你需要一种方法来在枚举中添加新字段吗? – Reddy 2012-01-17 13:46:02

1

我认为这是不通过Eclipse模板,使这个简单的方法,主要是关于驼峰/大写和代枚举值。您可以检查这两个问题Is there a way to capitalize the first letter of a value of a variable in Eclipse (Helios) code templates,Programmatically add code templates?以深入了解更多细节。

恕我直言,实现你想要的最好的方法是使用Fast Code Eclipse Plugin并为该插件编写一个速度模板,以生成所有字段的代码。或者更改"getter_setter" template of that plugin