[Android annotation skills] How did you write the annotation above Android functions? (In Eclipse), androideclipse

Source: Internet
Author: User

[Android annotation skills] How did you write the annotation above Android functions? (In Eclipse), androideclipse

Have you used the Eclipse shortcut Alt + Shift + J? Have you read the source code? If so, have you noticed the comments above the source code? Do you know why you can directly click to jump to some identified parameters in the source code comment?

First, let's take a question and define the simplest Person class, three attributes, one name, one age, one gender, and one constructor with all attribute parameters. How do you write this?

public class Person {     private String mName;     private int mAge;     private int mSex;      public Person(final String name, final int age, final int sex) {         super();         this.mName = name;         this.mAge = age;         this.mSex = sex;     } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

I believe no one is doing this in the project! No comments at all! The example here is simple. It can be seen from the attribute name. If it is difficult to understand, and the amount of Code increases, it will become a headache.

1. How to quickly generate document comments

In fact, Eclipse has a way to quickly Generate document comments. Place the cursor on the class, attribute, or function to be annotated, right-click and choose-> Source-> Generate Element Comment, I prefer to use the shortcut key Alt + Shift + J to automatically generate comments!

Below is a basic technique.

  • Generate Constructor using
    Fields...
    You can quickly generate constructors with attribute parameters. (As I mentioned above, you can generate constructors with multiple parameters)
  • Generate Getters and Setters...
    Quickly generate the property accessors and seters. (This feature is provided by the school teacher only during the internship so that we don't need to comment on the Code)
  • Override/Implement Methods...
    Quickly select the super-class function to be rewritten or implemented;

Then the code after the annotation is automatically generated becomes like this

/*** @ ClassName Person * @ Description human * @ author AZZ * @ Date 3:27:39 on January 1, August 6, 2015 * @ version 1.0.0 */public class Person {/*** @ Field @ age: age */private int mAge;/*** @ Field @ name: name */private String mName;/*** @ Field @ sex: Gender */private int mSex; /*** @ Description constructor * @ param age * @ param name * @ param sex Gender */public Person (int age, String name, int sex) {super (); this. mAge = age; this. mName = name; this. mSex = sex ;}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

Although the Code gets longer, the comments are clear and easy to read. The most important thing is that the comments in the document can prompt you where you use the class, method, and attribute.
Does your code look like this after adding comments? I think it should be different. Because I modified the annotation template !~ So you can see some custom tags, such as "@ ClassName", "@ Description", and "Field ". If you like this template, you can see how to change it.

2. Meaning of fields in the comments (important)

Because the image below is too large, it does not look very concise. So let's sum up it first. The following are all descriptions that you don't understand.

 
 
  • (Null) The text written on all labels will be the key words used to describe the function.
  • @ Author information
  • @ Param parameter information
  • @ Return
  • @ Exception
  • @ Throws exception information (@ exception and @ throws are tested to have the same effect)
  • @ Category Information
  • @ Since
  • @ See some functions use this label to identify by using other classes, functions, or attributes.
    • @ See # This type of function name/attribute name can be used to view other functions or attributes
    • @ See package name. The class name can be used to view other classes
      -
  • @ Deprecated indicates that this function is not recommended. In this label, write why it is not recommended and provide a new method to replace this method. After this label is added, no prompt is displayed in the comment display, but the function name will be drawn together.Strikethrough 
    -
  • References other parameters, classes, or functions. You can do this: Use {@ link # function name/attribute name} to link this class property/function, and use {@ link package name. class name} to link other classes.
    -
  • 2015.8.14 update: If you want to change the annotation line, you can add a comment before the new line.<p>(If you do not add a line break, no line break is displayed when you place the cursor on the function, similar to html)

The following is a formal illustration:

Some fields are used in the document annotations to indicate information, which can clearly tell others the role of this function/class, A great note in this document is that when you place the cursor over the function/class for other calls, you can see the comments you have written before.

In the document annotation code segment, the default fields include

  • (Null) The text written on all labels will be the key words used to describe the function.
  • @ Author information
  • @ Param parameter information
  • @ Return
  • @ Exception
  • @ Throws exception information (@ exception and @ throws are tested to have the same effect)
  • @ Category Information
  • @ Since

Test code segment

/*** Test method-display of each comment tag * @ author information-AZZ * @ param input parameter * @ return parameter * @ throws Exception parameter illegal Exception * @ exception IllegalArgumentException param is less than 0 or param is greater than 100 * @ category information * @ since JDK1.0 */public boolean test (int param) throws Exception {if (param <0 | param> 100) {throw new Exception ("wrong param");} return false ;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

Place the mouse over test and the following figure will be displayed:

  • @ See some functions use this label to identify by using other classes, functions, or attributes.
    • @ See # This type of function name/attribute name can be used to view other functions or attributes
    • @ See package name. The class name can be used to view other classes


      Click to jump to display corresponding class/function/attribute comments

  • @ Deprecated indicates that this function is not recommended. In this label, write why it is not recommended and provide a new method to replace this method. After this label is added, no prompt is displayed in the comment display, but the function name will be drawn together.Strikethrough

  • @ Custom tag names, such as @ Date @ Description, can be customized by yourself. The annotations of these tags are automatically arranged under the default tag

 
  • In addition, in the document annotations, for example, in the @ param interpretation, sometimes we need to reference other parameters, classes, or functions. For example, two Integer constants are defined in the Person class to identify men and women.setSex()In the function, I want to prompt the user to set the two constants I have already given. You can do this: Use {@ link # function name/attribute name} to link the attributes/functions of this class, use {@ link package name. class Name} to link other classes (Do you think of @ see ?)
/*** @ Field @ MALE: MALE */public static int MALE = 0;/*** @ Field @ FEMALE: FEMALE */public static int FEMALE = 1; /*** the mSex to set * @ param sex either {@ link # FEMALE} or {@ link # MALE} * test link Method {@ link # test (int )} * test the link class {@ link com. test. note. person} */public void setSex (int sex) {this. mSex = sex ;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Place the mouse over the function name

Click to jump to the comment

  • Update
    When you want to annotate the rows for another line, you can add<p>(If you do not add a line break, no line break is displayed when you place the cursor on the function, similar to html)

    Add<p>Label
3. How to modify the annotation Template

The template I am using is provided without bypassing the circle.
To learn more about the keyword "Eclipse annotation template", you can customize the template by yourself.

Usage: Open Eclipse-> Window-> Preferences-> Java-> Code Style
1. Click Code Templates-> Import... "MyCodetemplates. xml"
2. Click Formatter-> Import ..." MyFormatter. xml"

Q: customized IT education platform, one-to-one service, Q & A. Official Website for developing programming social headlines: www.wenaaa.com

QQ Group 290551701 has gathered many Internet elites, Technical Directors, architects, and project managers! Open-source technology research, welcome to the industry, Daniel and beginners interested in IT industry personnel!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.