idea 配置java方法注釋模板
依次點擊File -> Settings -> Editor -> Live Templates
第一步:在Live Templates 右側(cè)點擊+號,添加一個Templates Group,命名為 myTemplate,如下圖:

image-20211125111854750.png
第二步:在剛剛創(chuàng)建的 myTemplate下創(chuàng)建一個 Live Templates ,如下圖:

image-20211125112116160.png
第三步:輸入注釋模板的縮寫(Abbreviation),描述(Description),模板內(nèi)容(Template text)以及選擇觸發(fā)注釋模板的按鈕(Expand with)

image-20211125112255435.png
模板內(nèi)容如下:
**
* <description>
* $param$ $return$
* @throws
* @author bj
* @since 1.0.0 $date$
*/
第四步:配置變量,點擊Edit variables

image-20211125113117115.png
$param$的值如下:
groovyScript("def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); if(params[0] == ''){return result;}else{result+=\"\\r\\n\"}; for(i = 0; i < params.size(); i++) {result+=' * @param ' + params[i] + ' ' + params[i] + ((i < params.size() - 1) ? '\\r\\n' : '')}; return result", methodParameters())
$return$的值如下:
groovyScript("def result=\"${_1}\"; if(result == \"void\"){return \"\";}else{return \"\\r\\n * @return {@link \"+result+\"}\";}", methodReturnType())
$date$的值如下:date()
第五步:更改所選模板的上下文類型
點擊下方的change,或者右鍵我們定義的模板,如下圖所示:

image-20211125132703141.png

image-20211125132833203.png
在打開的選項框中勾選可以添加該注釋的位置,如下圖所示:

image-20211125132927491.png
第六步:驗證效果, 在方法上輸入/c然后按tab按鈕,可以看到如下效果
public class TaskService {
/**
* <description>
*
* @throws
* @author bj
* @since 1.0.0 2021/11/25
*/
public void test() {
}
/**
* <description>
*
* @return {@link List<T>}
* @throws
* @author bj
* @since 1.0.0 2021/11/25
*/
public <T> List<T> test1() {
return null;
}
/**
* <description>
*
* @param params1 params1
* @param params2 params2
* @return {@link List<T>}
* @throws
* @author bj
* @since 1.0.0 2021/11/25
*/
public <T> List<T> test1(T params1, T params2) {
return null;
}
}
注意點:
-
方法模板的內(nèi)容不能設(shè)置成以
/開頭的完整格式,設(shè)置成這樣會出現(xiàn)檢測不到方法參數(shù)和返回值,原因未知。例如設(shè)置成這樣:/** * <description> * $param$ $return$ * @throws * @author bj * @since 1.0.0 $date$ */出來的效果:
image-20211125133927889.png @throws后面的值沒法自動生成,暫時未找到方法,需要手動添加。
