Learn the Java annotation Summary.

Source: Internet
Author: User

Source url:http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html



the meaning of the annotation:

Annotation (annotation) is Java provides a way and method of associating any information with any metadata (metadata) in the elements of a program. Annotion (annotation) is an interface in which a program can retrieve the Annotion object of a specified program element by reflection, and then obtain the metadata in the annotation by Annotion object.

Annotation can be used to correlate any information to a program element (class, method, member variable, and so on). It should be noted that there is a basic rule: Annotation can not affect the execution of program code, regardless of adding, deleting Annotation, the code is consistent execution. In addition, while some annotation are accessed through the Java Reflection API method at run time, the Java language interpreter ignores these annotation at work. It is because the Java virtual machine ignores the annotation that the annotation type is "non-functional" in the code, and only through some kind of matching tool will the information in the annotation type be accessed and processed.


Meta Data:

Metadata-data about the data.

Metadata features, such as Javadoc annotations can generate documents, which is one of the metadata features. In summary, metadata can be used to create documents, track the dependencies of code, perform compile-time format checks, and replace existing configuration files. Specifically as follows:

1. Writing documents: Generating documents from metadata identified in code
2. Code Analysis: Code analysis through the metadata identified in the code
3. Compile check: The metadata identified in the code allows the compiler to achieve basic compilation check
In Java, metadata exists as a label in Java code, and the existence of metadata labels does not affect the compilation and execution of program code, it is only used to generate other files or a needle at runtime to know the description of the code being run.
Sum up:
First, metadata exists in the form of a label in Java code.
Second, the metadata describes the information is type-safe, that is, the fields within the metadata are explicitly typed.
Third, metadata requires tools that are outside the compiler for additional processing to generate other program parts.
Finally, the metadata can exist only at the Java source level, or within the compiled class file.

Annotation and annotation types:

Annotation:

annotation uses the new syntax that comes with java5.0, which behaves much like public and final modifiers. Each annotation has a >=0 name and number of members. The members of each annotation have names and values called Name=value pairs (just like JavaBean), Name=value loaded with annotation information.

Annotation type:

  The annotation type defines the annotation name, type, and member default values. A annotation type can be said to be a special Java interface , its member variables are restricted, and the new syntax is required when declaring annotation types. When we access annotation through the Java Reflection API, the return value will be an object that implements the annotation type interface, and we can easily access its annotation members by accessing the object. The following sections refer to the 3 standard annotation types included in the Java.lang package in java5.0.

Classification of Annotations :

Depending on the number of annotation parameters, we can divide the annotations into three categories:

1. Mark annotation: A annotation type that does not have a member definition is called a tag annotation. This annotation type uses only its own existence or not to provide us with information. For example, the following system annotation @override;
2. Single value annotation
3. Full annotation

Depending on the method and use of the annotation, we can divide the annotation into three categories:
1.JDK Built-in system annotation
2. Meta-annotation
3. Custom annotations

System built-in standard annotation:

Note syntax is relatively simple, in addition to the use of the @ symbol, he is basically consistent with the Java inherent syntax, javase built-in three standard annotations, defined in the Java.lang:
@Override: A method used to modify the method that overrides the parent class;
@Deprecated: Used to modify methods that are obsolete;
@SuppressWarnnings: Used to tell the Java compiler to prevent specific compilation warnings.


1. The role of annotations

Write documentation, code analysis, compile check, parameter configuration

2. Note to the compiler to see, comments are for people to see.


Custom Annotations :

The function of the meta annotation is to annotate other annotations. Java5.0 defines 4 standard meta-annotation types that are used to provide descriptions of other annotation types. Java5.0 defined meta Annotations:
1. @Target,
2. @Retention,
3. @Documented,
4. @Inherited
These types and the classes they support can be found in the Java.lang.annotation package. Let's take a look at the role of each meta annotation and the instructions for using the corresponding argument.

When you use @interface to customize annotations, the Java.lang.annotation.Annotation interface is automatically inherited and other details are automatically completed by the compiler. When you define annotations, you cannot inherit other annotations or interfaces. @interface used to declare an annotation, in which each method actually declares a configuration parameter. The name of the method is the name of the parameter, and the return value type is the type of the parameter (the return value type can only be the base type, Class, String, enum). You can declare the default value of a parameter via default.

To define the annotation format:
Public @interface Note name {definition body}

the supported data types for the 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. All of the above types of arrays

The following article is very detailed about the application:


Http://www.cnblogs.com/peida/archive/2013/04/26/3038503.html

Special Note: Annotations can be modified and added during operation.

Http://www.kaifajie.cn/kecheng/java/7652.html

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.