Learn from teacher Wang Note (c): meta-annotations

Source: Internet
Author: User

Learn from teacher Wang Note (c): meta-annotations
Teacher: Wang Shaohua QQ Group No.: 483773664

Meta annotations, which are used to modify other annotation definitions. The Java.lang.annotation package provides 4 meta-annotations, namely @target annotations, @Retention annotations, @Documented annotations, and @inherited annotations.

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/82/92/wKioL1dbj76Au-VgAAAtBqwp4Nk787.png " data_ue_src= "E:\My knowledge\temp\bb473eb7-0b0b-43fb-a259-f283bfb9dc1f.png" >

First, @Target
(a) Introduction
    • Used to specify which program elements the modified annotations can be used to decorate

    • @Target the annotation type has a unique value as a member variable, the type of value is java.lang.annotation.elementtype[] type.

    • When the member variable value of the @Target is the following value, you can specify that the modified annotation can only be labeled as follows

Elementtype.annotation_type: modified annotations can only be used to modify annotations

Elementtype.constructor: Only the construction method can be modified

Elementtype.field: can only modify member variables

Elementtype.local_variable: only local variables can be modified

Elementtype.method: only method declarations can be modified

Elementtype.package: can only decorate the package

Elementtype.parameter: can only be used to modify parameters

Elementtype.type: can be used to decorate classes, interfaces, enum declarations

(ii) method of use

View @suppresswarning's source code

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/93/wKiom1dbjrGTi6g5AAAZODdZqns546.png " data_ue_src= "E:\My knowledge\temp\d62d6005-b75c-4422-b70b-b99bcf9e88a8.png" >

Second, @Retention annotations
(a) Introduction

@Retention annotations Describe whether the annotations being modified are discarded by the compiler or retained in the class file. Annotations are saved in the class file by default, but cannot be accessed by reflection at run time.

The @Rentention contains a value member variable of type Retentionpolicy, which is derived from the value of the enumeration type of Java.lang.annotation.RetentionPolicy, with the following 3 values:

Retentionpolicy.class ( default ): The compiler will record annotations in the CLASS file, and when the Java program is run, the Java program Java virtual machines no longer retain annotations

Retentionpolicy.runtime: The compiler will record annotations in the class file, and Java virtual machines will retain annotations when running Java programs. The program can get the annotation through reflection

The Retentionpolicy.source compiler will discard the modified annotations directly

(ii) method of use

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M02/82/92/wKioL1dbj77R4jYBAAAXnTo_CpE806.png " data_ue_src= "E:\My knowledge\temp\da9a631d-02b8-448c-b833-e8ef40f901e3.png" >

Because @suppresswarning only cancels the compile-time warning, @suppresswarning does not need to be saved.

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/93/wKiom1dbjrHClAVLAAAYhM4t_F4540.png " data_ue_src= "E:\My knowledge\temp\4ced2976-edcb-4804-96ef-60323818c44b.png" >

Third, @Documented annotations
(a) Introduction

@Documented used to specify that the modified annotations will be extracted by the Javadoc tool into a document.

If a @documented adornment is used when defining annotations, then all of the program elements that use the callout adornment will contain the note description in the API documentation.

There are no member variables in @Documented annotation type

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/92/wKioL1dbj7_AbW2XAAAVkmS_zy0682.png " data_ue_src= "E:\My knowledge\temp\1b08b471-d2ff-4133-8cb0-5ecf506e2ca5.png" >

(ii) method of use

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M00/82/93/wKiom1dbjrKw63POAAAb5ZoAJWs820.png " data_ue_src= "E:\My knowledge\temp\b165bf26-73e8-4f14-aa90-fc4bcf906539.png" >

650) this.width=650; "border=" 0 "src=" http://s3.51cto.com/wyfs02/M01/82/93/wKiom1dbjrKS4hIqAAAc3Hkx58M982.png " data_ue_src= "E:\My knowledge\temp\e8889525-3a03-4574-9efa-98c9163716b7.png" >

(iii) Javadoc order

There are more than 50 Javadoc command parameters, here is a simple description of three parameters:-d,-subpackages,-sourcepath

-d Specifies the output directory of the API document, which is the current directory by default. It is recommended to always specify this parameter.
-SOURCEPATH Specifies the source code path, which is the current directory by default. This parameter is usually required.
-subpackages handles each sub-package in a recursive manner. Key Parameters! If you do not use this parameter, you can only process one child package at a time (or you need to list all the child packages manually).

Use the parameter-author to export the author information (@author * * *) to the final generated API document,-version can generate version information. If you write a bag, don't forget to use-author.

123456789101112131415161718 package cn.net.imentors;   import javax.annotation.Resources; /**   * Program Entry   * @author Wangsh   * @createDate: June 1, 2016   * @version: v1.0   * Function Description:   */ @Resources ({}) public class javadoctest {        public static void main (string[] args)          {               System.out.println ( "Hello!javadoc" );          }   }
123456789 publicclass Person {    /**     * age属性     */    public int age;    public int count;    publicPerson() {    }}


1 D:\>javadoc -d d:\api -sourcepath d:\src -subpackages cn.net.imentors -version -author
Iv. @Inherited Notes (i) Introduction

@Inherited annotations are used to specify that the modified annotations will have inheritance.

If a class uses annotations that are @inherited decorated when defined, its subclasses automatically have this annotation.

(ii) Use of @Inherited

Allow subclasses to inherit annotations from parent classes

Save to the next section, and then give you an example of the use of @inherited annotations.













From for notes (Wiz)

Learn from teacher Wang Note (c): meta-annotations

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.