Home > Java > javaTutorial > body text

An article to help you understand annotations in Java

无忌哥哥
Release: 2018-07-23 10:45:14
Original
2143 people have browsed it

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.

1. What is annotation

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.

2. Implementation

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:

  1. 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.

  2. RetentionPolicy.CLASS: Discarded when the class is loaded. Useful in processing bytecode files. Annotations use this method by default

  3. 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:

  1. ElementType.CONSTRUCTOR: used to describe the constructor

  2. ElementType.FIELD: member variables, objects, properties (including enum instances)

  3. ElementType.LOCAL_VARIABLE: used to describe local variables

  4. ElementType.METHOD: used to describe methods

  5. ElementType.PACKAGE: used to describe the package

  6. ElementType.PARAMETER: used to describe the parameters

  7. ##ElementType.TYPE: used to describe the class, interface (Including annotation type) or enum declaration

  8. ##@Inherited defines the relationship between the annotation and the subclass. The @Inherited meta-annotation is a mark annotation. @Inherited elaborates on a certain annotation. Types are inherited. If an annotation type modified with @Inherited is used for a class, this annotation will be used for subclasses of that class.

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 "";
}
Copy after login

The above meta-annotation is for custom annotation services.

3. Summary

In short, an annotation is a mark used to mark code. We can implement different annotation methods by scanning different annotations. Through Java's dynamic proxy and reflection, we can Easily obtain the content of our annotation tags to operate the classes or methods we have written. In the next article, I will define a custom annotation and write its implementation.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!