Annotations are something that anyone who is a Java developer will be familiar with, but we use so many annotations, how do our annotations work for us? Through my study some time ago, I have a new understanding of annotations.
In our popular view, annotation is an implementation of the Annotation interface. It is at the same declaration and usage level as classes and interfaces. They all inherit the Object base class and have the .class attribute.
But do the annotations themselves really work?
If you don’t believe it, try defining an annotation yourself and then put it in your code. The result will be useless.
We can think of annotation as a mark with attributes. When we mark our code with this mark, it means that our code has certain characteristics represented by the annotation, but it does not mean that we It has this characteristic the moment I annotate it.
Our code needs to be compiled before running. Sometimes we also need to compile dynamically during runtime. At this time, if we embed reflection or dynamic proxy code to parse this class , and add the characteristics it should have to this class. At this time, the class has the meaning represented by the annotation.
For example, when we were in kindergarten, we needed to raise our hands to go to the toilet, and the teacher would lead us there. At this time, raising your hands means that you have marked the annotation for going to the toilet. If the teacher does not If I were to blame you, you wouldn't be able to go to the toilet and would have to hold it in. If the teacher scans the whole class at this time and finds you raising your hands, she will go to your place and take you with a group of classmates who are all raising their hands. To the restroom. Only then did you develop your ability to go to the toilet.
Most of the annotations we see are not actually defined by Java at the beginning. The annotations specified at the beginning are only the first four meta-annotations. , they are:
@Documented – Whether the annotation will be included in JavaDoc
@Retention – When to use the annotation
@Target – where the annotation is used
@Inherited – whether subclasses are allowed to inherit the annotation
@Documented, this annotation means whether to put the description of this class or method in our java document when we generate javaDoc. Generally, it is useless if you don't use the project documentation tool that comes with Java to generate documents.
@Retention, this annotation represents the life cycle of the annotation we define. Here are its various assignments and descriptions of its functions:
RetentionPolicy.SOURCE: During the compilation phase throw away. These annotations no longer have any meaning after compilation, so they are not written into the bytecode. @Override, @SuppressWarnings all belong to this type of annotations.
RetentionPolicy.CLASS: Discarded when the class is loaded. Useful in processing bytecode files. Annotations use this method by default
RetentionPolicy.RUNTIME: It will never be discarded, and the annotation is retained during runtime, so the reflection mechanism can be used to read the annotation information. Our custom annotations usually use this method
@Target, which indicates where the annotation is used to mark. The default is to mark any element, and the value of ElementType can be assigned to it:
ElementType.CONSTRUCTOR: used to describe the constructor
ElementType.FIELD: member variables, objects, properties (including enum instances)
ElementType.LOCAL_VARIABLE: used to describe local variables
ElementType.METHOD: used to describe methods
ElementType.PACKAGE: used to describe the package
ElementType.PARAMETER: used to describe the parameters
We can customize an annotation:
import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.RetentionPolicy.RUNTIME; /** * 水果名称注解 */ @Target(FIELD) @Retention(RUNTIME) @Documented public @interface FruitName { String value() default ""; }
The above meta-annotation is for custom annotation services.
3. Summary
The above is the detailed content of An article to help you understand annotations in Java. For more information, please follow other related articles on the PHP Chinese website!