Java Annotations-annotation processor (Compile period | Retentionpolicy.source) __java

Source: Internet
Author: User

Reprint Source: 暃 Road»java Annotation (3)-annotation processor (Compile period | Retentionpolicy.source)

The processing of annotations can be processed in the compile period in addition to the reflection mechanism at run time. When annotations are processed at compile time, they are processed until the new source file is no longer generated, and then all source files are compiled.

The APT tool is provided in the JAVA5 for annotation processing at compile time. Apt is a command-line tool, matched by a set of Api:mirror APIs (com.sun.mirror.*) that describe the static structure of the program at compile time. The Mirror API allows you to obtain information about the elements of the Java type being annotated, providing custom processing logic. The specific processing tool is handed over to apt to handle. The core of the annotation processor is two classes: the annotation processor (com.sun.mirror.apt.AnnotationProcessor), Note Processor Factory (com.sun.mirror.apt.AnnotationProcessorFactory). Apt tool after the annotation processing, will automatically call Javac to compile processing completed source code. However, the APT tool is a private implementation provided by Oracle (which does not exist in the JDK development package's class library). In JDK6, the annotation processor is normalized to form a java.annotation.processing API package, and the Mirror API is encapsulated to form a Javax.lang.model package. The development of the annotation processor has been streamlined to integrate this functionality into the Javac command instead of using the APT tool alone. (Currently the JDK version of the current development is generally more than 6, so the APT tool does not do research). compile-time annotation processor

An example program is used to explain the use of the compile-time annotation processor (Javac tool to handle).

The annotation processor is used to generate the corresponding interface file for the given Java source file, and only the public methods in the class are abstracted as methods in the interface. 2.1. Define Annotation @generateinterface

Package com.zenfery.example.annotation;
Import Java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import Java.lang.annotation.RetentionPolicy;
Import Java.lang.annotation.Target;
@Target (Elementtype.type)

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.