Click on the blue font above and select "Star Official Account"
High-quality articles, delivered immediately
Follow the official account backstage to reply to pay or mall to obtain actual project information + video
The new 5G mobile phone is so cool! Free shipping!
Author of the article: Jitwxs
Link: https://jitwxs.cn/4135e0a9.html
One, class annotation
IDEA is open Settings
, click Editor-->File and Code Templates
, right click File
the tab below Class
, where the figures add the contents of the red box:
/**
* @author jitwxs
* @date ${YEAR}年${MONTH}月${DAY}日 ${TIME}
*/
In the example I provided the template, indicating the author and time, all of the template parameters supported under the IDEA Description
is listed in.
After saving, the class comment will be added automatically when you create a new class. If you want to enter into force on the interfaces, configure the image above Interface
items can be.
Two, method notes
Different from the current method annotation tutorial of copying and pasting each other on the Internet, this article will achieve the following functions:
The mesh shape parameters automatically generated
@param
annotationsAccording to whether the method returns a value generation intelligent
@Return
comment
Compared to the class template, add comments template for the method to more complex, first Settings
click Editor-->Live Templates
.
Click on the far right +
, first choose 2. Template Group...
to create a template group:
Fill in the group name in the pop-up dialog box, I call it userDefine here:
Then select the template group you just created userDefine
, then click +
to select 1. Live Template
:
At this time, it will create an empty template, we modify the template Abbreviation
, Description
and Template text
. Note that, Abbreviation
must *
, at last check Expand with
value whether the Enter key.
· Figure above Template text
reads as follows, please copy directly into it, need not pay attention to the first line /
, and \*
is the top grid .
*
*
* @author jitwxs
* @date $date$ $time$$param$ $return$
*/
He noted the lower right corner of No applicable contexts yet
the right, which shows the template language at this time have not specified applications:
Click Define
and check in the pop-up box to Java
indicate that the template will be applied to all Java type files.
Remember when we configured Template text
when it contains similar to $date$
this parameter, then IDEA not know these parameters is hell, let's method of mapping these parameters, let IDEA able to understand the meaning of these parameters. Click on the Edit variables
button:
Set the corresponding for each parameter Expression
:
It should be noted, date
and time
the Expression
use of IDEA is built-in functions, direct use of drop-down box to select it, and the param
poor of this parameter IDEA default implementation, so we need to manually implement the code 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())
In addition return
to this I also own parameters achieved, the code is as follows:
groovyScript("return \"${_1}\" == 'void' ? null : '\\r\\n * @return ' + \"${_1}\"", methodReturnType())
Note: You also notice that I did not check the
Skip if defined
property, which means that if if this item is defined, then the mouse cursor will skip it when generating comments. I don't need this feature, so this attribute is checked.
Click OK to save the settings, and you're done!
3. Inspection results
3.1 Class annotation
Class annotations are automatically generated only when a new class is created , and the effect is as follows:
3.2 Method notes
The following scenarios will be demonstrated:
No formal parameters
Single parameter
Multiple formal parameters
No return value
Has a return value
四、Q & A
(1) Why the template Abbreviation
must be called \*
? Expand with
Make sure it is the Enter key?
A: Because IDEA template generation logic is that 模板名 + 生成键
, when the key is generated Enter when we enter * + Enter
can trigger template.
This also explains why the first line is a comment template *
, because when we first entered /*
, and then enter the * + Enter
trigger template, the first line just makes up /**
, in line with Javadoc specifications.
(2) Why is there a blank line in the comment template \*
?
Answer: Because I am used to writing method descriptions in this line, I have reserved a blank line for writing, and you can also delete it.
(3) Notes template $time$$param$
why these two obviously irrelevant things close together?
A: Most param generating function first offered online in the absence of a reference case still generates a line empty @param
, so I modified the code param function, so as not to generate in the absence of a reference case @param
, but this requires $param$
to be and at others In the same line, otherwise it cannot handle backspace.
(4) Why is the return parameter not used methodReturnType()
, but has to be implemented by yourself?
Answer: methodReturnType()
In the case of no return value, void will be returned, which is meaningless, so I processed the return value of methodReturnType() and generated it only when there is a return value.
(5) Why $return$
is not a single line?
A: Because when methodReturnType()
returning null, backspace can not handle the problem, because with the third point.
有热门推荐????
Java后端线上问题排查常用命令收藏SpringBoot+Prometheus+Grafana实现应用监控和报警10个解放双手实用在线工具,有些代码真的不用手写!微信小程序练手实战:前端+后端(Java)
又一神操作,SpringBoot2.x 集成百度 uidgenerator搞定全局ID为什么要在2021年放弃Jenkins?我已经对他失去耐心了...Docker + FastDFS + Spring Boot 一键式搭建分布式文件服务器
点击阅读原文,前往学习SpringCloud实战项目