代碼注釋規范-IDEA 配置 Java 類方法注釋模板


1. 引言

    團隊開發時,業務模塊分配的越清晰,代碼注釋管理越完善,越有利於后面維護,后面再管理也方便不少。另外也起着"文字磚"的作用,你懂的。注釋不需要很詳細,把代碼塊方法塊功能簡述一下就行。不然三月后回頭看就要罵人了,罵完發現是自己寫的,嘖嘖嘖...

    三種常用的 Java 注釋方式

    // 聲明常量
    int number;
    /*
     * 類主函數
     */
    public static void main(String[] args) {
    }
    /**
     * @param masterId	品牌商Id
     * @param shopId	店鋪Id
     * @param skuId		商品skuId
     * @description: 校驗商品標識碼與店鋪的所屬關系
     * @return: net.jdcloud.APIECRM.model.ValidateSkuUsingGETResponse
     * @author: niaonao
     * @date: 2020/01/13
     */
    public static ValidateSkuUsingGETResponse validateSkuUsing(String masterId, String shopId, String skuId){
        return null;
    }

2. 自定義注釋模板

2.1 類注釋模板

2.1.1 配置模板

    菜單路徑 File-Settings-Editor-File and Code Templates-Incudes-File Header 下添加注釋模板,配置模板后點擊 Apply OK 應用。

    自定義注釋模板

/**
 * @className: ${NAME}
 * @description: TODO 類描述 
 * @author: niaonao
 * @date:  ${DATE}
 **/

    新建接口文件自動生成注釋,效果如下

/**
 * @className: CrowdService
 * @description: 人群對象業務
 * @author: niaonao
 * @date: 2020/1/13
 **/
public interface CrowdService {
}

2.1.2 自定義注釋模板不完全變量參考表

預定義變量 描述信息
${NAME} the name of the current file
${PACKAGE_NAME} name of the package in which the new file is created
${USER} current user system login name
${DATE} current system date
${TIME} current system time
${YEAR} current year
${MONTH} current month
${MONTH_NAME_SHORT} first 3 letters of the current month name. Example: Jan, Feb, etc.
${MONTH_NAME_FULL} full name of the current month. Example: January, February, etc.
${DAY} current day of the month
${DAY_NAME_SHORT} first 3 letters of the current day name. Example: Mon, Tue, etc.
${DAY_NAME_FULL} full name of the current day. Example: Monday, Tuesday, etc.
${HOUR} current hour
${MINUTE} current minute
${PROJECT_NAME} the name of the current project

2.2 方法注釋模板

2.2.1 配置模板

    菜單路徑 File-Settings-Editor-Live Templates 下添加一個新模板組,名字自定義 JavaTemplateGroup。選中模板組,右側點擊新增按鈕,創建新模板。

  • Abbreviation 配置為* 。
  • Description 自定義描述信息。
  • Template Text 自定義模板
*
 $param$
 * @description: TODO
 * @return: $return$
 * @author: niaonao
 * @date: $date$
 */

    Edit variables 編輯變量

  • 變量 return 表達式為 methodReturnType()
  • 變量 date 表達式為 date()
  • 變量 param 表達式為
groovyScript("def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {result+='* @param: ' + params[i] + ((i < params.size() - 1) ? '\\n ' : '')};return result", methodParameters()) 

    若有警告信息 No Applicable contexts,點擊 Define 選中 Java 即可。

    此處 Expend With 配置為 Enter 回車鍵,注釋生成快捷方式,看個人習慣,也可以時 Tab 鍵。

    點擊 Apply OK 應用配置即可。效果如下

    /**
     * @param: masterId
     * @param: shopId
     * @param: skuId
     * @description: TODO
     * @return: net.jdcloud.APIECRM.model.ValidateSkuUsingGETResponse
     * @author: niaonao
     * @date: 2020/1/13
     */
    public static ValidateSkuUsingGETResponse validateSkuUsing(String masterId, String shopId, String skuId) {
        return null;
    }

2.2.2 補充說明

    方法注釋模板不可用在,方法外,若用在方法外 @param 獲取不到,注釋為 @param null;

    類注釋模板在文件創建時生成,已創建文件不會觸發該模板,會觸發方法注釋模板。

Power By niaonao, The End, Thanks


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM