Explanation of annotations in reflection of Java programming and explanation of java programming annotations
The word "annotation" is a common topic in Java programming with a high appearance rate. Annotations were everywhere when we talked about Spring-related things. Before that, we briefly talked about some "Annotations" related content, for example, how to combine annotations in Spring. Because annotations are important in Java programming, today's blog will introduce the Annotations to the system. Of course, we will rely on specific instances.
To put it bluntly, "annotation" is a way to store data. If the annotation is extracted, it is common. If the "annotation" is combined with the "reflection mechanism" of Java, there will be more to do. That is to say, you can read the information provided by the "annotation" through reflection, and then do something according to your specific needs. Of course, we used XML to provide information for the reflection mechanism before, but the XML configuration still does not have the "annotation" Data Form for good management and maintenance, therefore, the "annotation" status is important.
Below we will talk about the "meta annotation" first, then define the annotation based on these "meta annotation", and use the "reflection mechanism" of Java to read various types of annotation information.
I. Metadata Annotation
In the first part of this blog, let's take a look at the "meta annotation" as a whole, and then expand the content below based on these meta annotations.
1. @ Target
Usage: @ Target (ElementType. CONSTRUCTOR)
@ Target annotation is very important. Target Chinese is the meaning of "Target, location. @ Target is used to declare the position where the created annotation is stored, that is, the elements that can be modified by the created annotation. @ Target is an ElementType enumeration. Each enumeration item represents a position. Below are some commonly used ElementType enumeration values:
TYPE: class. If the @ Target parameter is TYPE, the annotation we create can only be used to modify classes, interfaces, enumeration, and other types.
FIELD: FIELD modifier. If our custom annotation is of the FIELD type, our annotation can only be used to modify fields of the class or enumeration, that is, member variables.
CONSTRUCTOR: CONSTRUCTOR type. The "annotation" of this type can only modify CONSTRUCTOR.
METHOD: modifies the annotation of the "METHOD.
PARAMETER: modifies the annotation of parameters in the "method.
LOCAL_VARIABLE: annotation for modifying "local variables.
Of course, the above is a simple chat, and the following will show the specific examples of the above types of annotations. The following are all the options in ElementType and the functions of each enumerated value. The following two enumeration items are added after 1.8, as shown below:
2. @ Retention
Usage: @ Retention (RetentionPolicy. RUNTIME)
The above is the usage of @ Retention. The Chinese meaning of Retention is "retained", that is, the meta annotation provides the Retention period of "annotation. @ Retention is also a parameter that receives an enumeration type. below is the type contained in this enumeration. The English comment below has given the meanings of each enumeration item.
SOURCE: Note that our annotations will only stay in our SOURCE code and will not be compiled.
CLASS: Our annotations will be compiled into bytecode and stored in the. class file, but will not be linked to the Virtual Machine for running.
RUNTIME: This indicates that our annotation will be retained until the program runs. If you want to do something during RUNTIME Based on the annotation information through the reflection mechanism, so we must keep our annotations at this stage.
3. @ Document and @ Inherited
The two annotations are relatively simple. @ Document indicates that the annotation is included in Javadoc, while @ Inherited indicates that the annotation can be Inherited by the quilt class.
The above introduction may be somewhat abstract. Next we will use the reflection mechanism based on the instance to operate the corresponding types of custom annotations.
II. Introduction to test cases
Below is the Demo directory and main operation classes involved in this blog.
AnnotationTracker: This class obtains the object of the corresponding annotation type and related information in the annotation through the "reflection mechanism" of Java. In the AnnotationTracker Class, all static methods are passed in. The general structure is as follows.
CE... Annotation: these classes are annotations of different types, which will be discussed in detail later.
TestClass: this class is the test class modified by the annotation.
Main: how to execute the test cases in this Demo.
Iii. TYPE annotation: @ Target (ElementType. TYPE)
Next, let's take a look at the creation and use of type annotations. In the following content, we create an annotation for the modifier type, add the annotation to the relevant class, and finally use the Java reflection mechanism to obtain the corresponding annotation information.
1. Create Annotation
First, create an Annotation. The specific steps are as follows. Select Annotation, type the Annotation name, and click Enter.
The code segment below is the detailed content in the created annotation. We can see that the @ Target meta annotation parameter is of the ElementType. TYPE. That is to say, the annotation we created is an annotation of the modifier type, which can be of the scope class, interface, enumeration, and other types. Then we can see that the @ Retention parameter is of the RetentionPolicy. RUNTIME type, indicating that the annotation has been retained until RUNTIME.
Annotations are declared using @ Interface, which is similar to what interfaces are. @ Interface is followed by the annotation name. The annotation name is CETypeAnnotation. There is a public (public) INTEGER (int) type id attribute. The default value of this attribute is 0, as shown below.
2. Use of annotations
The following code snippet is used for the preceding annotation. Because the annotation created above is of the ElementType. TYPE, we use this annotation to modify a class we created, that is, the TestClass below. During annotation modification, we set a value for id, that is, id = 10 below.
3. Use reflection to obtain information about the modifier annotation.
Next, we need to add the "reflection mechanism" of Java to the AnnotationTracker class to obtain the annotation information of the corresponding TestClass class. The key code is as follows. The parameter of the trackTypeAnnotation () method is of the Class type, and the annotation object in the corresponding Class can be obtained through the getAnnotation () method of the Class. As shown in the red box below.
After obtaining the corresponding annotation object, we can get the configuration information in the corresponding annotation.
4. Test Cases and test results
Next we will call the above method in the AnnotationTracker class in the Main method, and pass in TestClass, as shown below. The following figure shows the output.
4. Annotations of other types
We have discussed in detail the annotation of ElementType. TYPE. Next, let's take a look at the annotation of other types and the usage of these annotations.
1. @ Target (ElementType. CONSTRUCTOR)
Next we will create an annotation for the modifier. The following CEConstructorAnnotation is the annotation we created to modify the class constructor. The default value of the value field is an empty string.
2. @ Target (ElementType. FIELD)
Next, we will create an annotation to modify a field. We will name this field CEFieldAnnotation. The Code is as follows:
3. @ Target (ElementType. METHOD)
Below is the annotation of the modifier we created. we name it CEMethodAnnotation. The specific code is as follows.
4. @ Target (ElementType. PARAMETER)
Below is the parameter annotation in the modifier method. we name it as follows:
5. Use of the preceding annotations
The following is the usage of various types of annotations defined above, and each of them performs their respective duties. I will not go into details too much.
6. Use the reflection mechanism to obtain different types of annotation information
We have talked about how to use the "Java" reflection mechanism to obtain the information of related annotations. Below we will obtain the information about the various types of annotations mentioned above. The code below is mainly related to the AnnotationTracker code.
1. Get the annotation information of the modifier type
2. Obtain the annotation information of the modifier and method parameters.
3. Obtain the annotation information of the modifier Field
4. Test Cases and output results
Today's blog is here, and the next blog will update the Java-related blog.