2012-01-17 67 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