Official account: Java Xiaokaxiu, website: Javaxks.com

Author :Jitwxs, link: jitwxs.cn/4135e0a9.ht…

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:

/ * * * @ author jitwxs * @ the date from ${MONTH} ${YEAR} ${DAY}, ${TIME} * /Copy the code

In the sample template I provided, stating the author and time, all template parameters supported by IDEA are listed in Description below. 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

Instead of the current copy-and-paste method annotation tutorials on the web, this article implements the following:

  • Automatically generate @param annotations based on the number of parameters
  • An @Return annotation is intelligently generated based on whether the method has a Return value

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.

*
 * 
 * @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.

Remember that Template text contains parameters like dateDatedate, and IDEA does not know what these parameters are. Let’s map these parameters to IDEA. Click the Edit Variables button:

Set the corresponding Expression for each parameter:

It should be noted that the Expression of date and time uses the built-in function of IDEA and can be directly selected by using the drop-down box. However, the default implementation of param parameter IDEA is very poor, so we need to implement it manually. The code is as follows:

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!

Iii. Inspection results

3.1 class annotation

Class annotations are automatically generated only when a new class is created, with the following effect:

3.2 Method Notes

The following scenarios will be demonstrated:

1. The invisible refs

2. Single parameter

3. Multiple parameters

4. No value is returned

5. There is a return value

Q & A

1) Why the Abbreviation of templates must be called *? Expand with make sure it’s Enter, okay?

A: Because the IDEA template generation logic is template name + build key, when the build key is Enter, we can trigger the template by typing * + Enter. This also explains why the first line of the comment template is an *, because when we type /* and then * + Enter to trigger the template, the first line is spelled with /**, which conforms to The Javadoc specification.

(2) Why is there an empty * line in the comment template?

A: BECAUSE I am used to writing instructions on this line, I have reserved a blank line for you to delete.

(3) Comment in the templatetime$$paramWhy do these two obviously unrelated things stick together?

A: First of all, most of the param generating functions available on the web still generate an empty @param line with no parameters. So I modified the param function code to not generate @param with no parameters, but this would require paramParamparam to be on the same line as everyone else. Otherwise we can’t handle backspace.

MethodReturnType (methodReturnType);

A: methodReturnType() returns void if it has no value, which doesn’t make sense, so I’m processing the methodReturnType() return value and only generates it if it has a value. #### (5) Why is returnReturnReturn not a separate line? A: The backspace problem cannot be handled when methodReturnType() returns null for the same reason.