The word " annotations " is a relatively high rate of interpretation in Java programming and is a commonplace topic. When we were talking about spring -related stuff, the annotations were ubiquitous, and we talked about some of the "annotations" in the past, such as how to make a "note" combination in spring. Because annotations in Java programming is still more important, so our blog today on the annotated thing to the system introduction, of course, we will rely on specific examples.
"Annotations" is a way to store data , if the annotation is carried out the function is generally, if the "annotation" and Java "reflection mechanism", then you can do more things. That is, you can use reflection to read the information provided by the annotations, and then to do something according to your specific needs. Of course, before we used XML to provide information for the reflection mechanism, but the "XML" configuration is not "annotated" This data form good management, good maintenance, so the status of "annotation" is still more important.
Let's talk about meta annotations and then customize annotations based on these meta annotations and use the "reflection mechanism" of Java to read various types of annotation information .
One, Yuan annotations
In the first part of this blog, let's take a look at the "meta-annotations"as a whole, then the content below is then expanded according to these meta-annotations.
1. @Target
How to use:@Target (ElementType. CONSTRUCTOR)
@Target Annotation is more important, Target's Chinese is the meaning of " destination, location ", see the name of the idea. The @Target is used to declare where the annotations we create are placed, that is, what elements the annotations we create can modify. The @Target parameter is an enumeration of ElementType , each of which represents a location. Below are a few ElementType enumerations that are commonly used:
-
type: class, if the parameter of @target is type, then the annotations we create can only be modified on these types, such as classes, interfaces, enumerations, and so on.
-
field: Fields are decorated, and if our custom annotations are field types, then our annotations can only be used to decorate a class or enumeration of fields, that is, member variables.
-
CONSTRUCTOR: constructor type, annotations of this type can only decorate the constructor.
-
Method: An annotation that modifies "methods".
-
PARAMETER: The annotation that modifies the parameters in the method.
-
local_variable: An annotation that modifies "local variables".
Of course, the above is a simple chat, below will give a specific example of the above types of annotations. The following are all the options in ElementType and the role of each enumeration value. The following two are the newly added enumeration entries after 1.8 , as shown below:
2. @Retention
How to use:@Retention (retentionpolicy. RUNTIME)
Above is the use of @Retention , Retention in Chinese means " reserved ", that is, the meta-annotation gives the "annotation" of the retention period. @Retention is also a parameter that receives an enumeration type, below which is the type that the enumeration contains. The English note below is specific to the meaning of each enumerated item.
-
Source: Note that our annotations will only stay in our source code and will not be compiled.
-
CLASS: Our annotations are compiled into bytecode stored in a . CLASS file, but are not linked in the virtual machine.
-
Runtime: This means that our annotations are kept to the runtime of the program, and if you want to do something with the reflection mechanism at runtime based on the information of the annotations, then we have to keep our annotations to this stage.
3. @Document and @inherited
These two annotations are relatively simple,@Document Explain that this annotation is included in Javadoc , while @Inherited means that the annotation can be inherited by the quilt class.
The above introduction may be somewhat abstract, and next we will use the reflection mechanism to manipulate the corresponding type of custom annotations based on the example.
Second, the test case introduction
Below is a list of the demos involved in this blog and the main operating classes.
-
Annotationtracker: This class is responsible for obtaining the corresponding type of annotated object and related information in the annotations through the "reflection mechanism" of Java. In the class of Annotationtracker, all static methods are used, and static methods are passed to the class of the related annotation adornments. The approximate structure is shown below.
-
Ce... Annotation: These classes are different types of annotations, which we'll discuss in more detail later.
-
TestClass: This class is a test class that is modified by annotations.
-
Main: The execution method of our demo test case.
Iii. type annotations:@Target (ElementType. TYPE)
Next, let's look at the creation and use of type annotations. Below we create an annotation of a decorated type, then add the adornment of the annotation to the related class, and finally use the reflection mechanism of Java to obtain the corresponding annotation information.
1. Create annotations
First create our annotations, the steps are as follows, select Annotation After you type the note name click Enter.
The code snippet below is the detail in the created note. We can see that the arguments to the @Target meta annotations are of type elementtype.type . That is to say that the annotation we created is a modifier type annotation that can scope classes, interfaces, enumerations, and so on. Then we also see that the @Retention parameter is of type retentionpolicy.runtime , indicating that the annotation has been persisted to the runtime.
Annotations are declared using @Interface , which is similar to what an interface does. @Interface Rear followed by the name of the note, the name of this note is cetypeannotation. There is an id attribute of the Public (Common) integer (int) type. The default value for this property is 0, as shown below.
2. Use of annotations
The code snippet below is used for the above annotations. Because the annotations created above are of type elementtype.type , we use this annotation to decorate a class that we created, which is the testclass below. When the callout is decorated, we set a value for the ID, which is the id = 10 below.
3. Use reflection to get information about the cosmetic type annotations
Next, we'll add a "reflection mechanism" in the Annotationtracker class that takes advantage of Java to get information about the corresponding TestClass class annotations, as shown in the key code. The parameter of the Tracktypeannotation () method is a class type and can then be used by the class's getannotation () method to get the annotation object in the corresponding class. As shown in the red box below.
Once the corresponding annotation object has been obtained, we can obtain the configuration information in the corresponding annotations.
4. Test Cases and test results
Next we call the above method in the Annotationtracker class in the main method and pass in the TestClass, as shown below. Below is the result of its printing.
Iv. Other types of annotations
We talked about the elementtype.type type of annotations in detail above, and then we look at other types of annotations and how they are used.
1,@Target (ElementType. CONSTRUCTOR)
Next we'll create an annotation that modifies the constructor. The ceconstructorannotation below is the annotation we created to decorate the class constructor. The default value of the Value field is an empty string.
2,@Target (ElementType. FIELD)
Next we'll create an annotation of the modified field, and we'll name the field cefieldannotation, as shown in the following code:
3,@Target (ElementType. METHOD)
Below is an annotation of the adornment method we created, which we named Cemethodannotation, as shown below.
4,@Target (ElementType. PARAMETER)
Below is an annotation of the parameters in the adornment method, which we name as follows:
V. Use of the relevant annotations above
Below is the use of various types of annotations as defined above. Specifically, do not do too much to repeat.
Vi. using the reflection mechanism to obtain different types of annotation information
We've talked about how to use the "Java" reflection mechanism to get information about annotations, and we'll get information about each of these types of annotations separately below. The code below is primarily related to the code in Annotationtracker.
1. Get the callout information for the modifier constructor type
2. Get the annotation information of the modification method and method parameter
3. Get the annotation information for the modified field
4. Test Cases and output results
Today's blog will be here first, the next blog will still update the Java-related blog.
Explanatory notes in the reflection of Java programming