Annotation features of Java

Source: Internet
Author: User

JDK1.5 begins with the addition of the annotation feature, which can be used in declarations of classes, construction methods, member variables, methods, parameters, and so on. This feature does not affect the program's operation, but it can have an impact on accessibility tools such as compiler warnings.

First, define a annotation

 public @interface username{}

Define an annotation using the @interface keyword

Second, add members

 public @Interface  username{    String value ();  }

Member types support: String\class\primitive\enumerated\annotation, and Arrays of the listed types;

Third, add the default value

 public @Interface  username{    default "default value";  }

Iv. specifying the types that can be annotated

 public @interface username{}

Applies to annotations on the method, for example:

Class car{    = "Laye")    publicvoid Drive () {}  }

The types of ElementType are:

    1. Annotation_type: indicated for ANNOTATION type;
    2. Type: Represents the class, interface, and annotation and enumeration types;
    3. CONSTRUCTOR: expressed for construction methods;
    4. FIELD: Represents the use of member variables and enumeration constants;
    5. Method: means used;
    6. PARAMETER: indicated for the parameter;
    7. Local_variable: expressed for local variables;
    8. Package: Indicates the use of packages;

V. Specify the valid range of annotations

@Retention (retentionpolicy.runtime)  public @interface username{}

Indicates valid at run time, retentionpolicy optional type is as follows:

    1. SOURCE: Indicates that it is not compiled into the class file;
    2. Class: Represents the compilation to the class file, but the runtime does not load;
    3. Represents the load at run time;

Vi. Access Annotations

The Java.lang.reflect package has a class: Accesibleobject, which method\field\constructor the parent class at the same time, and it implements the Annotatedelement interface. Therefore, Method\field\constructor contains an interface method for accessing annotations;

Specific examples are as follows:

@Target (Elementtype.field)//annotation type is variable@Retention (Retentionpolicy.runtime)//valid range is run-time Public@inteface username{//Statement AnnotationsString value () default "no Body";//Add Member}classcar{@UserName (value= "Lay")//annotations on the variable     PublicString owner;//Default Value     Public Static voidMain (string[] args) throws exception{Class C= Car.class;//Get class typeField field = C.getfield ("owner");//get the corresponding variableUserName UserName = (UserName) field.getannotation (Username.class);//Get AnnotationsSystem.out.println (Username.value ());//get member variables for annotationsCar car =NewCar ();//Get InstanceField.set (Car, Username.value ());//set the value of the instance's owner variable    }}

The above is an example of accessing field, such as method and constructor;

Common methods:

    1. Isannotationpresent (class Class): Whether to add annotations of the specified type;
    2. Getannotation (class Class): Gets the annotations of the specified type;
    3. Getannotations (): Get all types of annotations;
    4. Getparameterannotations (): Get annotations for all parameters;

Annotation features of Java

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.