Class annotation
Open the Settings of IDEA, go to Editor–>File and Code Templates, click the Class under the File TAB on the right, and add the contents in the red box in the image:
/**
* @description: TODO
* @author ${USER}
* @date ${DATE} ${TIME}
* @version 1.0
*/
Copy the code
After saving, class annotations are automatically added when you create a new class. If you want this to work for interfaces, you can also configure the Interface option shown above.
2. Method annotation
Adding an annotation template to a method is more complicated than adding a class template. Start with Editor >Live Templates in Settings.
Select 2. Template Group… To create a template group:
In the dialog box that pops up, fill in the group name, which I’ll call userDefine:
Then select the newly created Template group userDefine, click +, and select 1. Live Template:
An empty Template is created, and we modify its Abbreviation, Description, and Template text. Note that the Abbreviation must be *, and finally check to see if Expand with is Enter.
In the figure above, the content of · Template text is as follows, please copy it directly into it. Note that there is no/in the first line and * is top.
*
*
* @author jitwxs
* @date $date$ $time$$param$ $return$
*/
Copy the code
Could you please check No Applicable Contexts yet at this time?
Click Define and select Java in the popup box to apply this template to all Java type files.
$date$= $date$= $date$= $date$= $date$ Click the Edit Variables button:
Set the corresponding for each parameterExpression
:It’s important to note that,date
和 time
的 Expression
Use the built-in function of IDEA, directly use the drop-down box to select, andparam
This parameter IDEA is poorly implemented by default, so we need to implement it manually, with the following code:
groovyScript("def result = ''; def params = "${_1}".replaceAll('[\\[|\\]|\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) {if(params[i] ! = '')result+='* @param ' + params[i] + ((i < params.size() - 1) ? '\r\n ' : '')}; return result == '' ? null : '\r\n ' + result", methodParameters())Copy the code
In addition, I also implemented the return parameter by myself, the code is as follows:
groovyScript("return "${_1}" == 'void' ? null : '\r\n * @return ' + "${_1}"", methodReturnType())
Copy the code
Note: You will also notice that I did not check the Skip if Defined property, which means that if this item is defined during annotation generation, the mouse cursor will Skip it directly. I do not need this feature, so it is checked.
Click OK to save the Settings and you’re done!