IDEA 自定义 Java 类注释、方法注释模板

2022/8/19 JetBrainsJava

# Java 类注释

# 类注释设置

File -> Settings -> Editor -> File and Code Templates

选择 Include -> File Header

Snipaste_2022-08-19_23-02-14.png

# 类注释模板

 /**
  * $description
  * @author Your Name
  * @since ${DATE}
  */

# 注释模板不完全变量参考表

预定义变量 描述信息
${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

# Java 方法注释

# 方法释设置

File -> Settings -> Editor -> Live Templates

# 1.添加模板组

在Live Templates 右侧点击+号,添加一个Templates Group

Snipaste_2022-08-19_23-12-14.png

# 2.命名模板组

给新的组添加一个名称

Snipaste_2022-08-19_23-14-00.png

在刚刚创建的模板组下创建一个 Live Templates ,如下

# 3.添加实时模板

Snipaste_2022-08-19_23-15-54.png

# 4.修改模板

Snipaste_2022-08-19_23-16-11.png

  • 在位置1处: 输入模板的简写码
  • 在位置2处:输入模板的描述
  • 在位置3处: 输入方法注释模板样式 可变变量要用 变量名 来表示,如:param; 模板如下:
/**
* @title $title$
* @author Kevin $param$
* @updateTime $date$ $TIME$ $return$
* @throws $throws$
*/

# 5.编辑变量

Snipaste_2022-08-19_23-22-07.png

Snipaste_2022-08-19_23-22-21.png

变量param 为方法的参数变量,需要根据方法的参数多少进行变化;变量 return 为返回值类型,也要根据方法的返回值进行变化,所一要自行设置该方法,设置的代码如下,复制粘贴即可:

# param

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

# return

使用脚本可过滤void返回生成->只有有实际返回类型才回生成@return

# 返回全量方法返回类型(包括包名和泛型)
 groovyScript("def param=\"${_1}\"; if(param == \'\' || param == \'void\') return \'\'; return \' \\n * @return \' + param", methodReturnType())
# 返回简洁方法返回类型(无包名无泛型,只有简单的主要类型)
 groovyScript("def param=\"${_1}\"; if(param == \'\' || param == \'void\') return \'\'; param = param.replaceAll(\'<.*>\', \'\'); param = param.substring(param.lastIndexOf(\'.\') + 1); return \'\\n * @return \' + param", methodReturnType())

# 6.添加使用范围

鼠标右键选择如图位置1或点击下面快捷位置2

Snipaste_2022-08-19_23-26-32.png

# 7.勾选java

Snipaste_2022-08-19_23-27-10.png

# 8.完成配置

Snipaste_2022-08-19_23-30-20.png

# 9.使用

使用:在方法上键入 /* + tab键即可生成javadoc模板注释

最近更新: 7 个月前