Java @Retention meta annotations

Source: Internet
Author: User

@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

Related Article

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.