"Java" in Java Annotated annotations (annotation)

Source: Internet
Author: User

Java annotations are some of the meta-information that is attached to the code, used by some tools to parse and use at compile and run time, to illustrate and configure functionality.
Annotations do not and do not affect the actual logic of the code, only the auxiliary role

Let us elaborate on this note, in the end is how one thing, step by step to see, always after harvest.

Note Origin: Annotation (note) was introduced in JDK5.0 and later versions. In the java.lang.annotation package.

annotation functions: It can be used to create documents, track dependencies in code, and even perform basic compile-time checks.

annotation Format: annotations are present in the code with the ' @ Annotation name '

annotation Categories: tag annotations, single-value annotations, complete annotations three categories

In addition: it does not directly affect the semantics of the program, but as an annotation (identity) exists, we can be programmed through the reflection mechanism to achieve access to these metadata .

Yuan Annotation said, I first come to the battle-----

------------------------------Main-----------------------------------

One meta-Annotations:

meta-annotation function: is to annotate other annotations.

Our function is divided into three categories:

  Writing Documents: generating documents from the metadata identified in the code;

 Code Analysis: The code is analyzed by the metadata identified in the code;

  compile check: the metadata identified in the code allows the compiler to implement basic compilation checks.

meta-annotations include:@Retention @Target @Document @Inherited four kinds.

@Target notes:

---------------------------------------------------

@Target Explanation:

  @Target illustrates the range of objects that annotation modifies:

Annotation can be used for packages, types (classes, interfaces, enumerations, annotation types), type members (methods, construction methods, member variables, enumeration values), method parameters, and local variables (such as loop variables, catch parameters).

Target can be more clearly decorated by using target in the declaration of the annotation type.

@Target function: used to describe the scope of use of annotations (i.e., where the annotations described can be used)

@Target Java source code:

@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type)  public @Interface  Target {    elementtype[] value ();}

Note: First explain interface represents an interface, and @interface represents annotation

——————————————————————————

ElementType Value:

Through the source code can see,ElementType , its value is:

1.CONSTRUCTOR: Used to describe the constructor
2.FIELD: Used to describe the domain
3.local_variable: Used to describe local variables
4.METHOD: Used to describe the method
5.PACKAGE: Used to describe the package
6.PARAMETER: Used to describe parameters
7.TYPE: Used to describe classes, interfaces (including annotation types) or enum declarations

------------------------------------------------

Example: (We have customized two custom annotations, according to the values given above, @table will be used for classes, interfaces, or enum declarations)

@Target (Elementtype.type)  public @Interface  Table {    /**     * Datasheet name annotation, default value is class name      @ return     */public    default "ClassName";} @Target (Elementtype.field) public @Interface  nodbcolumn {}

So here, our @target yuan annotation explained, there is not understand the place, @ Penguin: 2783309477

@Retention notes:

@Retention Streamlined Explanation:

 Is the management of the lifecycle of custom annotations.

@Retention explained in detail:

@Retention defines how long the annotation is retained: Some annotation appear only in the source code and are discarded by the compiler, while others are compiled in the class file The annotation that are compiled in the class file may be ignored by the virtual machine, while others will be read when class is loaded (note that it does not affect class execution because annotation is separated from the class in use). Using this meta-annotation can limit the "lifecycle" of annotation

——————————————————————————————

@Retention: Defining retention policies for annotations

@Retention (Retentionpolicy.source)//annotations only exist in the source code and are not included in the class bytecode file@Retention (Retentionpolicy.class) //default retention policy, annotations exist in the class bytecode file, but are not available at runtime @Retention (Retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time

——————————————————————————————

@Retention Java source code:

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

  The Retentionpolicy value can be seen through the source code. So next

———————————————————————————————

@Retention Value:

  Retentionpolicy Desirable value:

1.SOURCE: Valid in source file (i.e., source file retention)
2.CLASS: Valid in class file (that is, class reserved)
3.RUNTIME: Valid at run time (that is, runtime retention)

@Retention Example:

@Target (Elementtype.field) @Retention (retentionpolicy.runtime)  public @Interface  Column {    publicdefault "FieldName";      Public default "SetField";      Public default "GetField";       Public Boolean default false ;}

Note: Also, as you can see, column will have a valid range at run time.

General format:

  @Retention (Retentionpolicy.source) //annotations only exist in the source code and are not included in the class bytecode file

@Retention (retentionpolicy.class) //default retention policy, annotations exist in the CLASS bytecode file, but are not available at run time .

@Retention (Retentionpolicy.runtime)//annotations are present in the class bytecode file and can be obtained through reflection at run time

OK, this explains here, there is no understanding @ Penguin: 2783309477 (IF), then

@Documented yuan Annotations:

 @Documented the public API used to describe other types of annotation that should be labeled as program members.

This can therefore be documented by tools such as Javadoc.

Documented is a markup annotation with no members.

@Document Java source code:

@Documented @retention (retentionpolicy.runtime) @Target (elementtype.annotation_type)  public @Interface  documented {}

@Document Example:

  @Target (Elementtype.field) @Retention (retentionpolicy.runtime) @Documented  public  @interface   Column { public  String name () default
     "FieldName" ;  public  String setfuncname () default     "SetField" ;  public  String getfuncname () default      "GetField" ;  public  boolean  Defaultdbvalue () default  false  ;}  

@Inherited yuan Annotations:    @Inherited Explanation:   is a markup annotation, @Inherited illustrates that a type that is labeled is inherited. That is, if the annotation type it modifies is used on a class, the annotation will be used in the subclass. Note:    1. @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. 2, when the @inherited annotation type labeled annotation retention is retentionpolicy.runtime,The reflection API enhances this inheritance. If we use Java.lang.reflect to query a @inherited annotationtype of annotation, the reflection code check expands work: Checks the class and its parent class until the specified annotation type is foundis found, or reaches the top level of the class inheritance structure. To be continued (custom annotations/default annotations/...) )

"Java" in Java Annotated annotations (annotation)

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.