設定注釋模板的入口:
Window-<Preference-<Java-<CodeStyle-<Code Template 然後展開Comments節點就是所有需設定注釋的元素。現就每一個元素逐一介紹:
檔案(Files)注釋標籤:
/**
* Copyright ${year}公司名字. All rights reserved.
*
* @Title: ${file_name}
* @Prject: ${project_name}
* @Package: ${package_name}
* @Description: ${todo}
* @author: ${user}
* @date: ${date} ${time}
* @version: V1.0
*/
類型(Types)注釋標籤(類的注釋):
/**
* @ClassName: ${type_name}
* @Description: ${todo}
* @author: ${user}
* @date: ${date} ${time}
* ${tags}
*/
欄位(Fields)注釋標籤:
/**
* @fieldName: ${field}
* @fieldType: ${field_type}
* @Description: ${todo}
*/
建構函式標籤:
/**
* @Title:${enclosing_type}
* @Description:${todo}
* ${tags}
*/
方法(Constructor & Methods)標籤:
/**
* @Title: ${enclosing_method}
* @Description: ${todo}
* ${tags}
* @return: ${return_type}
*/
覆蓋方法(Overriding Methods)標籤:
/* (non Javadoc)
* @Title: ${enclosing_method}
* @Description: ${todo}
* ${tags}
* ${see_to_overridden}
*/
代表方法(Delegate Methods)標籤:
/**
* ${tags}
* ${see_to_target}
*/
getter方法標籤:
/**
* @return the ${bare_field_name}
*/
setter方法標籤:
/**
* @param ${param} the ${bare_field_name} to set
*/
或者可以從網上下載配置好的.xml檔案匯入也可以。下載請點擊我!
PS:在按照上面的方法設定後,一定要到windows->Preferences->general->Content Types,右側Context Types樹,點開Text,選擇Java Source File,在下面的Default encoding輸入框中輸入UTF-8,點Update,則設定Java檔案編碼為UTF-8,否則會出現字元編碼的問題。
經過如上的設定後,就可以使用快速鍵Alt+Shift+J來快速產生文檔的注釋了。使用方法如下:
檔案頭注釋:使用New->Class方法產生的檔案會在建立類的對話方塊選項中提示是否產生注釋,預設是選中的。這樣產生的.java檔案就有在檔案頭帶有上面設定的著作權資訊。若是使用New->File方法建立的java檔案則不會自動組建檔案頭的著作權資訊,必須手動輸入。方法、類、欄位等注釋:這一類的注釋只需要選中方法名、類名、或欄位名,然後使用Alt+Shift+J來自動產生注釋,然後補全資訊即可。