Starting today will begin to write a series of blog, adhere to finish one thing, at the same time also hope to help everyone, also the foundation of the lines, to understand a language anxiety has recently been your grade in this line, the greater the original go, recently be suddenly enlightened, seriously go down, open up a new way of working and way of life is also full of happy things, But when you leave this business, this position, maybe you should leave something behind, like a series of blogs

Origin:

  1. Java introduced annotations in 1.5: the concept of annotations is the same as that of tags when we read books. For example, by marking the number of pages in a book, we can quickly find the pages;

Members:

1. Meta annotations: The annotations on top of the annotations are the basic annotations provided by the Java JDK so that we can define them ourselves

  1. All meta annotations in Java implement Anntation interface

2 @target: Used to mark custom annotations

Elementtype.annotation_type can be applied to annotation types. Elementtype. CONSTRUCTOR can be applied to constructors. Elementtype. FIELD can be applied to fields or properties. Elementtype. LOCAL_VARIABLE can be applied to local variables. Elementtype. METHOD can be applied to METHOD level annotations. Elementtype. PACKAGE can be applied to PACKAGE declarations. Elementtype. PARAMETER can be applied to a method PARAMETER. Elementtype. TYPE can be applied to any element of a class.

3@Retention: Annotations Specifies how the tag annotations will exist

Retentionpolicy.source – Annotations for the tag are retained only at the SOURCE level and are ignored by the compiler. The retentionPolicy.class – tag’s annotations are retained by the compiler at compile time but ignored by the Java Virtual Machine (JVM). Retentionpolicy.runtime – The annotation for the tag is retained by the JVM, so the RUNTIME environment can use it. (the priority of the three values is SOURCE < CLASS < RUNTIME, i.e. CLASS contains SOURCE, RUNTIME contains SOURCE, CLASS.)

  1. There are also @Documented and @inherited meta annotations. The former is used to be extracted into documents by the Javadoc tool, and the latter indicates that subclasses are allowed

Inherits the annotations defined in the parent class.

Use:

/ / @ Target (ElementType. TYPE) can only marks in the class the annotation @ Target ({ElementType. TYPE, ElementType FIELD}) / / allow marks in the class and the class attribute of the annotation @retention (retentionpolicy.source) public @interface Lance {}Copy the code

Assignment tags, used to tag information, are available in logic ZHon

@Target({ElementType.TYPE,ElementType.FIELD}) @Retention(RetentionPolicy.SOURCE) public @interface Lance { String value(); Int age() default 1; } @lance (value=" wave ",age = 2) int I;} @lance (value=" wave ",age = 2) int I;Copy the code

The APT annotation processor is used in conjunction with the class annotation to help generate some code. Developers, such as Ali’s Arouter, use APT’s Java to dynamically generate auxiliary code at compile time

APT is called Anotation Processor Tools, meaning annotation Processor. As the name suggests, it is used to process annotations. Write good Java source text

, which needs to be compiled by JavAC and translated into a bytecode Class file that can be loaded by the virtual machine. The annotation handler is a tool that comes with Javac that scans for annotation information at compile time. You can register your own annotation handler for certain annotations. The registered annotation handler is invoked by JavAC and passes annotation information to the annotation handler for processing.