Annotations in Java (Annotation)--annotation processor

Source: Internet
Author: User
Tags constructor reflection

Annotations are no more useful than annotations if there is no method or work for reading annotations. In the process of using annotations, it is important to create a use of the annotation processor. The Java SE5 expands the reflection mechanism API to help programmers quickly construct custom annotation processors.

Annotation Processor Class Library (java.lang.reflect.AnnotatedElement):

Java uses the annotation interface to represent the annotations in front of the program element, which is the parent interface for all annotation types. In addition, Java in the Java.lang.reflect package of the new Annotatedelement interface, which represents the program can accept the annotation of the program elements, the interface is mainly the following implementation classes:

Class: Classes define

Constructor: constructor definition

Field: Tired member variable definition

Method: Methods definition for a class

Package: Package definition for Class

The Java.lang.reflect package contains a number of tool classes to implement the reflection function, in fact, the Java.lang.reflect package provides a reflection API that expands the ability to read run-time annotation information. When a annotation type is defined as a run-time annotation, the annotation can be visible at runtime, and the annotation that is stored in the class file when the class file is loaded is read by the virtual machine.

The Annotatedelement interface is the parent interface of all program elements (class, method, and constructor), so that after the program obtains the Annotatedelement object of a class by reflection, The program can invoke the following four methods of the object to access the annotation information:

Method 1:<t extends Annotation> T getannotation (class<t> annotationclass): Returns a specified type of annotation that exists on the modifier element, if the type annotation does not exist, NULL is returned.

Method 2:annotation[] Getannotations (): Returns all annotations that exist on the program element.

Method 3:boolean is annotationpresent (class<?extends annotation> annotationclass): Determines whether a specified type of annotation is included on the program element, and returns true if it exists. otherwise returns false.

Method 4:annotation[] Getdeclaredannotations (): Returns all comments that exist directly on this element. Unlike other methods in this interface, the method ignores inherited annotations. (if no comment exists directly on this element, an array of zero length is returned.) The caller of the method is free to modify the returned array, which does not have any effect on the array returned by the other caller.

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.