Java four meta annotations related to
summarize
The annotations are removed from theJava1.5
Since its introduction, it has continuously simplified the process of writing code for us, and gradually it has become a must-learn technology for us. We have learned a variety of annotations, their usage, and their limitations, have you ever thought about their composition? Below I will share my understanding of meta-annotations with you.
Meta-annotations are annotations used to modify annotations in theUnder the package, when we need to define an annotation to do something ourselves, we have to put some restrictions on that annotation to make sure that the scope of our annotations, some of which have attributes, and some of which don't, are visible to us when we click on them. There are four such annotations, as follows
Defining an annotation with@interface
to modify the file type, these four meta annotations we analyze one by one
@Documented
This annotation is used to declare whether our annotation can be used by theJavadoc
et al. (and other authors)API
The documentation generation tool demonstrates that if an annotation is declared above the@Documented
, it means that this annotation can be displayed by those tools that generate documentation.
We can see by clicking on it that the annotation uses three annotations, including himself, and two meta-annotations, and that the annotation has no attributes, so we have a simple way to use it, which is to write or not to write the annotation.
@Retention
We can see by clicking into this annotation that first he writes@Documented
, indicating that he is able to be displayed in the document by the document generation tool, and that he has an attribute that he similarly uses on himself@Retention
and is given a value of ->You can see the same thing as we did above.
@Documented
annotation is the same, and then we point to theRetentionPolicy
class to see what values can be assigned to this location.
The first thing we notice is that this class is an enum class, which means we can also write it like this
So what do all three constants mean?
- SOURCE: let's translate the comment: the compiler will discard the annotation that is to say that if he is used, then your annotation compiles into the
.class
file is thrown away afterward, so if our annotation is used to aid in the compilation process, then we can use it.
for example
@SuppressWarnings
We usually use this annotation to tell the compiler to ignore certain warning messages that we don't need after compilation, so there's certainly nothing wrong with using SOURCE in this scenario
- CLASS: Let's translate the annotations: the compiler records the annotations in the class file.
VM
It is not necessary to keep these comments at runtime. This is the default setting behavior. That is, the compiler compiles the.class
file, the annotation is still there, but it is not in ourjvm
de-load.class
file will be dropped, this is not used much so we just know what this means. If you don't write it, the default is to selectCLASS
。 - RUNTIME: one of the most used constants, just go to a class library and look up the annotations, and it's basically RUNTIME. As usual, we'll start by translating the annotations: the annotations will be documented by the compiler in the class file and will be used at runtime by the
VM
defined, so it can be read reflexively. This means that the scope defined by this constant is the largest possible, and can be reflected, i.e. not just compiled by the compiler,JVM
Loaded can still be read reflexively and will always be there. When we define annotations ourselves, unless there is a special need, the general business is also to use this constant, which is a bit more convenient.
@Target
As you can see from the figure, this meta-annotation uses three meta-annotations, the two we mentioned above and himself. We will not go into too much detail here. Then look at his properties, you can see the use of parentheses, indicating that the parameters of the annotation here can be placed in an array of enumerations, we click on the enumeration you can see the declaration of many constants.
The usual first translation of the notes, basically you can see what it means, theTYPE_PARAMETER
cap (a poem)TYPE_USE
beJDK1.8
incoming
- TYPE: class, interface (including annotated types), or enumeration declaration.
- FIELD: Field declarations (including enumeration constants)
- METHOD: Method declaration
- PARAMETER: Formal parameter declarations
- CONSTRUCTOR: constructor declaration
- LOCAL_VARIABLE: Local Variable Declaration
- ANNOTATION_TYPE: annotation type declaration
- PACKAGE: Package Statement
- TYPE_PARAMETER: Type parameter declaration. This constant can be used in front of various parameter types (including classes)
- TYPE_USE: The use of the type. This constant can be used to label the name of any type, including
TYPE_PARAMETER
(used form a nominal expression)
So we can choose the constants here according to our needs, but of course we can choose more than one!
For example, like this, as written in the image, this@annotationT
It can then be declared on both fields and methods.
@Inherited
As can be seen in the figure, this annotation is one that can be displayed by the document generation tool and will not be compiled by the compiler,JVM
discarded, and it is used toANNOTATION_TYPE
, which means that this annotation can only be defined to annotations, so he must be using it to specify certain characteristics of the annotation.
The old look we translate the notes above, due to the length of this does not show the specific content, the big brother can click in to see, probably: the annotations annotated by this annotation can be automatically inherited, query the class on the type of note, if the class declaration does not have annotations for the type of note will automatically query the superclass (until the Object)
PS: Note that this meta-annotation type is invalid if the annotation type is used to annotate anything other than the class. Note also that this meta-annotation will only cause the annotation to inherit from the superclass; annotations on implemented interfaces have no effect.
Simply put, if you want the annotations you define to be inherited, then put the@Inherited
Hang on to your notes.
summarize
The above is the basic information of the meta annotations that act on all of our annotations, we understand what each meta annotation represents will make it easier for us to understand some of theJDK
Self-contained , or third-party class libraries defined or used in the annotations , but also to facilitate our own to define some of the annotations , such as annotations can be used to achieve in the operation of our custom annotations annotations before or after the execution of the method to do something , when we really will be using these meta-annotations to customize their own annotations , you will certainly understand why the use of annotations is so common .
Thanks for reading ORZ, if I can help you it's my luck, if there is any problem with this blog feel free to leave a comment to point it out, thanks~