背景簡介豐富的注釋和良好的代碼規(guī)范,對于代碼的閱讀性和可維護性起著至關重要的作用。幾乎每個公司對這的要求還是比較嚴格的,往往會形成自己的一套編碼規(guī)范。但是再實施過程中,如果全靠手動完成,不僅效率低下,還難以保證真正的符合規(guī)范。所以結合IDE環(huán)境,自動生成注釋,還是很有必要的。今天我們就說一下,如何使用Eclipse給我們提供的自定義代碼模版的功能來作業(yè)。 設置注釋模板設置注釋模板的入口:Window->Preference->Java->Code Style->Code Template ,然后展開Comments節(jié)點就是所有需設置注釋的元素了!

接下來,對每一個元素逐一介紹:
文件(Files)注釋標簽 Files標簽是對新建的文件的說明,出現(xiàn)在文件最上面 
舉栗子: /** * Copyright ? ${year} eSunny Info. Tech Ltd. All rights reserved. * * @Package: ${package_name} * @author: ${user} * @date: ${date} ${time} */
類型(Types)注釋標簽(類的注釋) Types標簽是對類Class的一個說明,出現(xiàn)在類上面 舉栗子: /** * @ClassName: ${type_name} * @Description: ${todo} * @author: ${user} * @date: ${date} ${time} * ${tags} */
字段(Fields)注釋標簽 Fields標簽是對變量字段的說明 舉栗子: // @Fields ${field} : ${todo}(用一句話描述這個變量表示什么)
構造函數(shù)(Constructors)標簽 Constructors標簽是對類的構造函數(shù)的說明 舉栗子: /** * @Title:${enclosing_type} * @Description:${todo} * ${tags} */
方法(Methods)標簽 Methods標簽是對函數(shù)方法的說明 舉栗子: /** * @Title: ${enclosing_method} * @Description: ${todo} * ${tags} ${return_type} * @author ${user} * @date ${date}${time} */
覆蓋方法(Overriding Methods)標簽 Overriding Methods標簽是對覆蓋方法的說明 舉栗子: /* (non Javadoc) * @Title: ${enclosing_method} * @Description: ${todo} * ${tags} * ${see_to_overridden} */
代表方法(Delegate Methods)標簽 舉栗子: /** * ${tags} * ${see_to_target} */
getter方法標簽 舉栗子: /** * @return ${bare_field_name} */
setter方法標簽 舉栗子: /** * @param ${param} 要設置的 ${bare_field_name} */
以上標簽,只需要點擊右側面板上的按鈕 – 編輯(Edit), 便可修改成自己的注釋! 如何自動添加注釋可通過如下三種方法自動添加注釋: (1)輸入“/**”并回車。 (2)用快捷鍵 Alt+Shift+J(先選中某個方法、類名或變量名)。 (3)在右鍵菜單中選擇“Source > Generate ElementComment”。 另外,新建文件或類的時候,怎么自動生成文件(file)的注釋呢?

只需要勾選Automatically and comments for new methods and types 導入/導出代碼格式模板如果你辛辛苦苦定制好了自己的代碼風格,然后換了臺機器進行操作或重裝了Eclipse,是不是要重新配置一遍呢?答案當然是No了,Eclipse提供了“導出”和“導入”功能,你可以把自己的模板導出來在其他機器上使用。

創(chuàng)建自定義注釋模板 eclipse自帶一些注釋模板,如日期(@date)、文件名(@ClassName)、作者(@author)等,那么怎么自定義一些注釋模板呢?

codetemplates.xml模板內(nèi)容,可直接導入eclipse
/** * @Fields field:field:{todo}(用一句話描述這個變量表示什么) *//** * MIT License * Copyright (c) 2018 haihua.liu * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the “Software”), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. *//** * @param paramtheparamthe{bare_field_name} to set *//** * @return ${bare_field_name} *//** * @ClassName: ${type_name} * @Description: ${todo}(這里用一句話描述這個類的作用) * @author ${user} * @date ${date} * * ${tags} *//** (非 Javadoc) * * * ${tags} * ${see_to_overridden} *//** * ${tags} * ${see_to_target} *//** * @Title: ${enclosing_method} * @Description: ${todo}(這里用一句話描述這個方法的作用) * @param ${tags} 參數(shù) * @return ${return_type} 返回類型 * @throws */ /** * 創(chuàng)建一個新的實例 ${enclosing_type}. * * ${tags} */
|