(Source:https://www.cnblogs.com/yangjian-java/p/6674772.html)
First, Background introduction
Rich annotations and good code specifications play a critical role in code readability and maintainability. Almost every company's requirements for this is still relatively strict, often forming its own set of coding specifications. But in the process of implementation, if it is done by hand, not only inefficient, but also difficult to ensure that the real compliance. Therefore, it is necessary to combine the IDE environment to generate annotations automatically. Today we will talk about how to use Eclipse to provide us with the functionality of custom code templates to work with.
Second, set the comment template
To set the entry for an annotation template: Window->preference->java->code style->code templates
then expand the comments node, which is all the elements that need to be annotated!
Changes, just click on the button on the right panel – edit, and you can change your own comments!
Write a picture description here
Iii. Detailed introduction 1, document (files) Note tags
The Files tab is a description of the newly created file and appears at the top of the file
- Recommended annotation Style:
/** * Copyright ? ${year} eSunny Info. Tech Ltd. All rights reserved. * * 功能描述: * @Package: ${package_name} * @author: ${user} * @date: ${date} ${time} */
2. Type (Types) Comment Label (Note for Class)
The types tag is a description of class classes that appears above the class
- Recommended annotation Style:
/** * Copyright: Copyright (c) ${year} LanRu-Caifu* * @ClassName: ${file_name}* @Description: 该类的功能描述** @version: v1.0.0* @author: ${user}* @date: ${date} ${time} ** Modification History:* Date Author Version Description*---------------------------------------------------------** ${date} ${user} v1.0.0 修改原因*/
3. Field (fields) Comment label
Fields tab is a description of the variable field
- Recommended annotation Style:
// @Fields ${field} : ${todo}
4. Constructor (constructors) label
The constructors tag is a description of the class's constructor
- Recommended annotation Style:
/** * @Function: ${file_name}* @Description: 该函数的功能描述** @param:参数描述* @version: v1.0.0* @author: ${user}* @date: ${date} ${time} */
5. Method (Methods) label
The methods tag is a description of the function method
- Recommended annotation Style:
/** * @Function: ${file_name}* @Description: 该函数的功能描述** @param:描述1描述* @return:返回结果描述* @throws:异常描述** @version: v1.0.0* @author: ${user}* @date: ${date} ${time} ** Modification History:* Date Author Version Description*---------------------------------------------------------** ${date} ${user} v1.0.0 修改原因*/
6. Overlay method (overriding Methods) label
Overriding methods tag is a description of the override method
- Recommended annotation Style:
/** * ${see_to_overridden} * @Function: ${file_name}* @Description: 该函数的功能描述** @param:描述1描述* @return:返回结果描述* @throws:异常描述** @version: v1.0.0* @author: ${user}* @date: ${date} ${time} ** Modification History:* Date Author Version Description*---------------------------------------------------------** ${date} ${user} v1.0.0 修改原因*/
7. Representative method (Delegate Methods) label
- Recommended annotation Style:
/** * ${tags} * ${see_to_target} */
8. Getter Method Label
- Recommended annotation Style:
/** * @return the ${bare_field_name} */
9. Setter Method Label
- Recommended annotation Style:
/** * @param ${param} the ${bare_field_name} to set */
Iv. How to automatically add note 1, the following three ways to automatically add comments:
- Type "/**" and enter.
- Use the shortcut key Alt+shift+j (Select a method, class name, or variable name first).
- Select "Source > Generate elementcomment" In the right-click menu.
2. When creating a new file or class, how do I automatically generate a file comment?
Write a picture description here
Just tick automatically and comments for new methods and types!
3. Import/Export Code format template
If you have worked hard to customize your code style, and then changed the machine for operation or re-installed eclipse, do you want to reconfigure it again? The answer, of course, is no, and Eclipse provides "export" and "import" functionality, and you can export your own templates for use on other machines.
Write a picture description here Five, create a custom comment template
Eclipse brings some comment templates, such as Date (@date), file name (@ClassName), author (@author), and so on, how do you customize some of the comment templates?
Eclipse modifies default annotations