Java Annotations (i): initial knowledge of Java annotations

Source: Internet
Author: User
Tags modifiers

Reference: http://www.cnblogs.com/skywang12345/p/3344137.html

Http://www.cnblogs.com/taoxiaoji/archive/2011/01/19/1939651.html

http://blog.csdn.net/tigerdsh/article/details/8848890

Http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

First you know what the annotations are, what to do, and when to use them.

1. What is the annotation?

That is, a description of the data. Providing a formal way for us to add information to our code is that we can easily use the data at a later time (the Java virtual machine uses the data by parsing the annotations ). That is, it is a description of the data.

2. What can I do with annotations?

    • generate the document . This is the most common, and also the earliest annotations that Java provides. Commonly used @see @param @return, etc.
    • Tracking Code Dependencies , implementing alternative profile features. The most common is the annotation-based configuration that starts with spring 2.5. The function is to reduce the configuration. Now the framework basically uses this configuration to reduce the number of configuration files.
    • A format check is made at compile time . If you do not overwrite the superclass method with @override before the method, you will be able to check it at compile time.

It is also a comment, but it is not only the meaning of annotations. Maybe you'll feel a little bit abstract. We still use @override example, if it flags a method that does not override its parent's method, it will error at compile time. Is this a description of the method (you can understand the method is also a kind of data)? This description is equivalent to a certain qualification, exceeding the limit throws the corresponding exception, can be faster and more convenient encoding, reduce the amount of code.

3. How do I use annotations?

3.1 Definition of annotations: using @interface

modifiers  Annotationname    { elementdeclaration1;  element Declaration2; }

    modifiers : Private, public, protect, default

    annotationname: The name of the custom annotation, similar to the class name.

    element: The type of the annotation element

    declaration1,declaration2: The name of the annotation element

  3.2 Declaration and definition of elements in annotations  

Supported data types for annotation parameters:

1. All basic data Types (Int,float,boolean,byte,double,char,long,short)
2.String type
3.Class type
4.enum type
5.Annotation type
6. Arrays of all types above

1. Type elementname ();   2. Type elementname () default value;

See here, you already know how to customize the annotations.

Before creating a custom annotation, let's look at the class composition of the Java.lang.annotation package in Java:

  

  1) Annotation: All annotations inherit the Annotation interface, which is equivalent to all classes in Java and are inherited from the Object class.

  2) Annotationformaterror, Annotationtypemismatchexception, incompleteannotationexception: These 3 classes are custom exceptions .

  3) ElementType, Retentionpolicy: Two enumeration types.

    ElementType: A total of 8 objects used to describe the scope of use of annotations and limit the annotated markup.

public enum ElementType {    //This value corresponds to adornment: class, interface (including annotation type), enum type, and    //This value corresponds to adornment: property (including enumeration constant)    FIELD,    // This value corresponds to the adornment: Method    of methods,    //This value corresponds to adornment: Parameter PARAMETER in method    ,    //This value corresponds to adornment: Constructor method    CONSTRUCTOR,    //This value corresponds to adornment: local variable    local_variable,    //This value corresponds to decoration: annotation    annotation_type,    //This value corresponds to decoration: package Packages    
}

    retentionpolicy: Indicates the level at which this comment information needs to be saved to describe the life cycle of the annotation (i.e., the scope of the annotation being described is valid)

public enum Retentionpolicy {     //annotations will be discarded by the compiler     SOURCE,    //annotations are available in the class file, but will be discarded by VM (virtual machine)     class,/    / The VM (virtual machine) also retains comments at run time, so the information of the annotations can be read through the reflection mechanism.    RUNTIME}

4) Target, Retention, documented, inherited: 4 yuan Annotations, you may see here, do not know what the meta-annotations are? Keep looking down.

    target: The object used to qualify the annotation tag.

@Documented @Retention (retentionpolicy.runtime)//Limit @Target (Elementtype.annotation_type)//Limit Target annotations, only with markup annotations. Public @interface Target {
Elementtype[] Value (); Target annotation element, type elementtype[], name is value
}

See here, you will find the element of the Target annotation, is an array of previously defined enumeration type ElementType, inside can be multiple values. So Target is used to limit the range of annotation tags, and then the value is one or more of the ElementType!

So the definition of meta-annotations : Annotations can only be labeled annotations, in other words, all meta-annotations can only mark annotations, that is, the value of the @Target is elementtype.annotation_type

    Retention: What level of validity is used to qualify annotations

@Documented @Retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @interface Retention    {    retentionpolicy value (); }

Similar to Target, the value of the element in the retention annotation is the value of the enumeration type Retentionpolicy, and the value can only be one of the retentionpolicy, only!!!

    documented: A public API used to describe other types of annotation that should be labeled as program members, so it can be documented by tools such as Javadoc. Documented is a markup annotation with no members .

@Documented @Retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) public @interface documented {    }

    inherited: is a markup annotation, @Inherited illustrates that a type that is labeled is inherited. If a annotation type that uses the @inherited modifier is used for a class, the annotation will be used for subclasses of that class.

Note: @Inherited the annotation type is inherited by subclasses of the class being labeled. The annotation class does not inherit from the interface it implements, and the method does not inherit annotation from the method it overloads.

@Documented @Retention (retentionpolicy.runtime) @Target (elementtype.annotation_type) Public @interface inherited {    }

To this, is not the Java annotations have a preliminary understanding, this is certainly not enough, the best way to learn is to use it!!!

As an example you can read this blog directly (http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html) very detailed!

  

Java Annotations (i): initial knowledge of Java 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.