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:
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
- ##ElementType.TYPE: used to describe the class, interface (Including annotation type) or enum declaration ##@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 ""; }
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!

如何解决:Java注解错误:注解参数类型错误引言:在Java开发中,注解(Annotation)是一种元数据的形式,用于为程序元素(类、方法、字段等)添加额外的信息。然而,有时我们可能会遇到注解参数类型错误的问题,这会导致编译错误或运行时异常。本文将介绍解决Java注解参数类型错误的方法,并提供代码示例帮助读者更好地理解。理解注解参数类型错误:注解参数类型错

如何在Java中使用注解函数实现自定义注解注解(Annotation)是Java中的一种特殊的语法元素,它可以用来给代码添加元数据信息,以便在运行时进行解析和处理。Java提供了一些预定义的注解(如@Override、@Deprecated等),同时也支持用户自定义注解。在某些场景下,使用自定义的注解可以使代码更简洁、可读性更强。本文将介绍如何在Java中使

注解的作用域决定了它们应用于代码的哪些部分,而生命周期描述了它们在代码中的存在时间。作用域有元素级别、声明类型级别和代码块级别,生命周期分编译时、类加载时和运行时。注解的生命周期包括编译时被添加到class文件中,类加载时被JVM处理,运行时可通过反射访问。

Java中的注解处理器Java中的注解处理器是一种能够检测和处理Java代码中注解的工具。使用注解处理器可以增强编译时检查,生成额外的代码,甚至修改已有代码,从而提高代码的可读性、可维护性和可复用性。注解处理器通常是编写在Java中的,而不是在运行时解释和执行的。这为注解处理器提供了很多方便,如可以使用更丰富的Java类型系统、面向对象特性和

解决Java注解解析异常(AnnotationParsingException)的方法引言:在Java开发中,注解成为了一种非常重要的技术,它可以通过在源代码中加入元数据的方式,来描述程序中的各种信息。而在使用注解的过程中,有时我们可能会遇到AnnotationParsingException异常,这个异常代表了在解析注解时发生的错误。本文将介绍如何解决这个

如何在Java中使用注解函数进行自定义注解和元数据处理引言:在Java编程中,注解(Annotation)是一种特殊的语法结构,可以在代码中附加额外的元数据,并由编译器、解释器或其他工具进行处理。注解函数是一种特殊的注解,它可以用来标记函数、方法或方法参数,并且可以在运行时通过反射机制来访问和处理这些注解。本文将介绍如何在Java中使用注解函数进行自定义注解

深入理解Java注解开发的经验与建议随着Java语言的发展,注解(Annotation)成为了Java开发中不可或缺的一部分。作为一种元数据,注解可以为代码添加额外的描述信息,帮助开发者更好地理解代码逻辑。同时,注解还可以在编译时期和运行时期进行处理,实现自动化的功能。在日常的Java开发中,我们经常会使用到注解。然而,要深入理解和有效地应用注解,需要掌握一

如何解决:Java注解错误:未定义的注解在使用Java开发过程中,注解是一种十分常见的技术手段,可以用来给代码添加一些额外的信息或者行为。然而,有时候我们可能会遇到一个错误:未定义的注解。这个问题在编译或者运行时会导致程序无法正常工作,因此解决这个错误显得十分重要。本文将介绍一些解决未定义注解错误的方法,并提供一些代码示例。一、检查注解的导包当我们使用一个自


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.