IDEA configures Java class method annotation template

1. Class annotation template

File -> Settings -> Editor -> File and Code Templates -> Files
select Class, Interface, Enum, etc., we can see that in the right area, above the public class, there is a line #parse("File Header.java")

img

This code introduces the File Header.java file as a comment for the Class Interface, Enum and other files we created. So where is this class? We can see that on the right side of Files, there is an Includes option. Here, We can define various templates and introduce this template where needed. The File Header.java template has been introduced in the class file. Then we can change this template to what we want to set it to.

image-20220512152855134

Custom annotation template

/**
 * @className: ${NAME}
 * @author: Kevin
 * @date:  ${DATE}
 **/

New interface files automatically generate comments, the effect is as follows

/**
 * @className: CrowdService
 * @author: Kvein
 * @date: 2022年05月12日 15:33:00
 **/
public interface CrowdService {
    
    
}

Annotation template incomplete variable reference table

Predefined variables Description
${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. Method annotation template

File -> Settings -> Editor -> Live Templates

1. Click the + sign on the right side of Live Templates to add a Templates Group and name it methodTemplates

img

img

2. Create a Live Templates under the methodTemplates just created, as follows

img

1). At position 1: Enter the abbreviation code of the template.
At position 2: Enter the description of the template.
At position 3: Enter the method comment template style. For variable variables, use variable name.variable name to represent, such as:param paramp a r a m ;
If it is not set to such a variable name, position 4 cannot be clicked. The template is as follows:

/**
 * @title $title$
 * @author Kevin $param$
 * @updateTime $date$ $TIME$ $return$
 * @throws $throws$
 */

Click at position 4: You can edit the value of the defined variable, as follows:

img

The variable param is the parameter variable of the method, which needs to be changed according to the number of parameters of the method; the variable return is the return value type, and also needs to be changed according to the return value of the method, so you need to set the method yourself. The setting code is as follows, copy and paste. Can:

param :

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

return :

groovyScript("def result=''; def data=\"${_1}\"; def stop=false; if(data==null || data=='null' || data=='' || data=='void' ) { stop=true; }; if(!stop) { result += '\\r\\n' + ' * @return: ' + data; }; return result;", methodReturnType())

Under Position 3, click to select the scope of application of the template. Select Everywhere to indicate that the annotation can be added anywhere.

img

Click Expand with in options to select the shortcut keys used with this template, such as Tab key, Space key, Enter key, etc.;

For example, if the template keyword set here is * and the shortcut key used is the Tab key
, enter * in the method and press the Tab key to generate comments according to the template.

image-20220512154405338

Additional information

The method annotation template cannot be used outside the method. If it is used outside the method, @param cannot be obtained, and the annotation will be @param null;

The class annotation template is generated when the file is created. The created file will not trigger the template, but the method annotation template will be triggered.

Guess you like

Origin blog.csdn.net/weixin_52173254/article/details/124733010