In the development process, you need to add comments to classes and methods. Class comments are easy to implement. By setting a template, you can automatically create preset comments during creation. Adding comments to methods is not so easy. The main impact is on the processing of parameters.
For IDEA, after entering / * * + enter above the method of the interface, a formatting comment will be automatically created
/** * * @param id * @return */ Share get(String id);
For other methods, follow the above operation, only a simple multi line comment will be created without parameter information
/** * */ public Share get(String id) { Share share=new Share(); share.setId(id); share.setUserId("1"); share.setTitle("My share"); //Using Feign to invoke a remote service // UserDto userDto = userService.getUser("1"); UserDto userDtoParam=new UserDto(); userDtoParam.setId("2"); UserDto userDto = userService.query(userDtoParam); String userName=userDto.getName(); share.setUserName(userName); return share; }
So, how to generate templated annotations for any method to improve development efficiency?
For IDEA, the first thing I think of is the live template function. You can customize code fragments and quickly complete them through custom abbreviations. You have configured it yourself before. Refer to previous experience and configure it as follows:
Template definition
/** * $params$ * @return */
Variable definition
Use the built-in method methodParameters to obtain parameters. The actual running result is
/** * * @return */ public void get(String id,String name) { }
What happened? In fact, we didn't get the method parameters. Through the search, we found that many people said that we can get the method parameters only by putting them inside the method, and then manually copy, paste and move the method body... Try it, it's true, but isn't it a toss? Not to mention reducing efficiency, it's so awkward to just operate like this.
Therefore, we further looked for a solution and found that there was a key problem, which was /, which would lead to the invalidation of the methodParameters method in the template. It was required that the template should not have /, and then a solution appeared, that is, / * was removed from the template and input externally, that is
* * $params$ * @return */
At this time, it is required to enter / * + user-defined acronym + tab. At this time, the parameters can be read, but multiple parameters are displayed as an array, like this... How to specify the meaning of each parameter?
/** * [id, name] * @return */ Share get(String id,String name);
In addition, there are many people on the Internet who say that the abbreviation must be set to *, and that this is very clever, but I really can't see where the cleverness is. I've also tried it. The abbreviation is arbitrary, such as d, as long as the external input is guaranteed, such as / *. This method is still quite awkward.
Continue to explore the scheme and find that the groovy script is used to parse the parameter array, as follows:
Template definition
** * $params$ * @return */
Variable definition
groovyScript("if(\"${_1}\".length() == 2) {return ' * ';} else {def result=''; def params=\"${_1}\".replaceAll('[\\\\[|\\\\]|\\\\s]', '').split(',').toList(); for(i = 0; i < params.size(); i++) { result+=' * @param ' + params[i] + ((i < params.size() - 1) ? '\\n':'')}; return result;}", methodParameters());
This is a script that can achieve the final desired result after repeated debugging. There are many online scripts with various defects. For example, for methods without parameters, a line @ param will also be generated. In addition, it is common to generate blank lines, dislocation, etc.
When using, just enter / + acronym + tab
/** * * @param id * @param name * @return */ Share get(String id,String name);
In addition, it's hard to configure this template yourself. There's another idea. Use the idea plug-in named Easy Javadoc. After installation, select the method name and press the shortcut key ctrl + \. You can directly generate method comments and translate them at the same time. The effects are as follows.
/** * obtain * * @param id ID * @param name name * @return {@link Share} */ Share get(String id,String name);
There are several points to note about this plug-in:
1. The default hotkey ctrl + \ conflicts with an operation built in idea. You need to modify the shortcut key settings
2. The default translation engine is Google. It may not work normally because it is blocked. It can be changed to Baidu translation or Youdao translation in the settings
3. When a method is operated for the first time, a translation confirmation box will pop up, and nothing will happen after clicking OK. However, after arbitrarily modifying the method name, comments can be automatically generated, and there is no problem changing it back, which is a little strange.