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

The attention

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.

In the figure above, the contents of the · Template text are as follows, please copy directly into it. Note that the first line does not have /, 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.

Set the applicable contexts

$date$= $date$= $date$= $date$= $date$ Click the Edit Variables button:

Set the corresponding Expression for each parameter:

Set up the Expression

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:

Class notes

3.2 Method Notes

The following scenarios will be demonstrated:

  1. Intangible and

  2. A single parameter

  3. Multiple parameter

  4. There is no return value

  5. Returns a value

Methods the annotation

Q & A

(1) why the Abbreviation of template 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.

$time$$param$

A: First of all, most param generators still generate an empty @param line with no arguments, so I modified the param function to not generate @param with no arguments, but this would require $param$to be on the same line as someone else, otherwise it would not be able to 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 $return$a separate line?

A: The backspace problem cannot be handled when methodReturnType() returns null for the same reason.