@Retention yuan Annotations
There are three kinds of values: Retentionpolicy.source, Retentionpolicy.class, Retentionpolicy.runtime, respectively: Java source files (. java files)----;. class file----> byte code in memory
Retention Note Description
When an annotation is added to the Java source, the Java source is compiled by Javac, and Javac compiles the Java source file into a. class file, which may remove some annotations from the Java source program when it is translated into class, and the Java compiler (JAVAC) When dealing with Java source program, you may think that this annotation is useless, so the annotation is removed, then in the compiled class can not find annotations, this is the compiler compiled Java source program when the annotation processing the first possible situation, Assuming that the Java compiler is compiling Java source program into class, the Java source program is not removed from the annotations, then in the compiled class can be found in the annotations, when the program uses the compiled class file, you need to use the ClassLoader class file loaded into memory, Class file is not a byte code, the class file is loaded into memory by the ClassLoader, the class loader when loading the class file, will be in the class file inside the processing of things, such as security checks, after processing the final in-memory binary things are the bytecode, The ClassLoader also has a conversion when the class file is loaded into memory, and it is also argued that the annotations in the class file are preserved when converting. There are three stages in the life cycle of an annotation: the Java source file is a stage, the class file is a stage, and the bytecode in memory is a phase , Javac the Java source file is compiled into a. class file, it is possible to remove the annotations inside, and the ClassLoader may remove the annotations from the. class file when it is loaded into memory. When customizing annotations, you can use retention annotations to indicate the lifecycle of a custom annotation, the lifecycle of a custom annotation in the Retentionpolicy.source phase (Java source file phase), or the retentionpolicy.class phase (class file phase), or in the retentionpolicy.runtime phase (bytecode runtime phase in memory), the API provided by the JDK can be known by default in the Retentionpolicy.class phase ( The JDK API writes: The retention policy defaults to Retentionpolicy.class.)
Java @Retention meta annotations