Simple example of Java annotation processor
If there is no method and work to read annotations, annotations will not be more useful than annotations. An important part of the process of using annotations is created by using the annotation processor. Java se 5 extends the API of reflection mechanism 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 program elements, which is the parent interface of all annotation types. In addition, Java is in Java The annotatedelement interface is added under lang.reflect package, which represents the program elements that can accept annotations in the program. The interface mainly includes the following implementation classes:
Class: class definition
Constructor: constructor definition
Field: member variable definition
Method: method definition of class
Package: the package definition of the class
java. The lang.reflect package mainly contains some tool classes that implement the reflection function. In fact, Java All reflection APIs provided by the lang.reflect package extend the ability to read runtime annotation information. When an annotation type is defined as a runtime annotation, the annotation can be visible at runtime. When the class file is loaded, the annotation saved in the class file can be read by the virtual machine.
The annotatedelement interface is the parent interface of all program elements (class, method and constructor), so after the program obtains the annotatedelement object of a class through reflection, the program can call the following four methods of the object to access the annotation information:
Method 1: < textendsannotation > tgetannotation (class < T > annotationclass): returns the annotation of the specified type that exists on the modifier element. If the annotation of this type does not exist, it returns null.
Method 2: annotation [] getannotations (): returns all annotations that exist on the program element.
Method 3: Boolean annotation present (class annotationclass): judge whether the program element contains annotations of the specified type. If it exists, return true; otherwise, return false
Method 4: annotation [] getdeclaraedannotations(): returns all annotations that exist directly on this element. Unlike other methods in this interface, this method ignores inherited comments. (if no comments exist directly on this element, an array of zero length is returned.) the caller of this method can modify the returned array at will; this will not have any impact on the arrays returned by other callers.
A simple annotation processor:
The basic knowledge points of Java annotation (see the guide below) have been basically reviewed. In the next article, we will design a simple ORM framework based on annotation to comprehensively apply and further deepen the understanding and application of various knowledge points of annotation.
summary
The above is all about the simple example of Java annotation processor in this paper. I hope it will be helpful to you. Interested friends can continue to refer to other related topics on this site. If there are deficiencies, please leave a message to point out. Thank you for your support!