-
First, a few commonly used annotations are introduced:
@author author name
@date date
@version version identifier
@parameter parameters and their meaning
@return return value
@throws exception class and throwing conditions
@deprecated causes a deprecated warning
@override override
We can often see this annotation in java code.
-
Steps to set a comment template: Click Window -->Preferences-->Java-->Code Style -->Code Templates on the menu bar, and edit the specific comment information in the Comments option on the right. You can check Automatically add comment information, and comments will be automatically generated when the java file is generated. Of course, you can also manually insert comments. The setting interface is as follows:
-
Click the Edit button to enter the editing page. After all editing is complete, click "Apply And Close" to complete the setting and close the setting page.
-
The following describes how to set the specific Comment:
1. Click Files under Comments to comment on the entire Java file: including company name, copyright ownership, author information, date, etc.
/**
* <p>Title: ${file_name}</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2017</p>
* <p>Company: www.baidudu.com</p>
* @author shenlan
* @date ${date}
* @version 1.0
*/
-
2. Click Types to annotate the class:
/**
* <p>Title: ${type_name}</p>
* <p>Description: </p>
* @author shenlan
* @date ${date}
*/
3. Click Fields to annotate the fields:
/** ${field}*/
4. Click Constructors to annotate the constructor:
/**
* <p>Title: </p>
* <p>Description: </p>
* ${tags}
*/
5. Click Methods to annotate the method:
/**
* <p>Title: ${enclosing_method}</p>
* <p>Description: </p>
* ${tags}
*/
-
6. Click Overriding Methods to annotate the overriding method:
/* (non-Javadoc)
* <p>Title: ${enclosing_method}</p>
* <p>Description: </p>
* ${tags}
* ${see_to_overridden}
*/
7. Delegate methods annotate the delegate method:
/**
* ${tags}
* ${see_to_target}
*/
8. Getters annotate the get method:
/**
* @return the ${bare_field_name}
*/
9. Setters annotate the set method:
/**
* @param ${param} the ${bare_field_name} to set
*/
-
Import and export of annotation templates: Click the Import and Export buttons.
END
Use of Java Annotation Templates
-
When setting the template, if automatically add comment information is checked, the document and class comment information will be automatically generated when creating a Java file. If it is not checked, it can also be generated by pressing Shift+Alt+J shortcut keys.
-
To annotate a method in a class: enter /** above the method and click Enter to generate a method annotation; or place the cursor on the method name, and hold down the Shift+Alt+J shortcut keys; or Right-click on the method, source>Generate Element Comment can also generate comments.