search
HomeJavajavaTutorialJava Programming Thoughts Learning Class (7) Chapter 20 - Notes

Annotations (also known as metadata) provide us with adding information to our code A formal method is developed that allows us to use these data very conveniently at a later time.

1 Basic syntax

 The annotated method is no different from other methods. Annotations can be used with any modifier on a method, for example, public, static or void. From a syntax perspective, annotations are used almost exactly like modifiers.

1.1 Definition of annotations

The definition of an annotation looks a lot like the definition of an interface. In fact, like any other Java interface, the annotations will also be compiled into class files .

When defining annotations, you will need some meta-annotations , such as #@Target, @Retention .

In annotations, generally contain some elements to represent certain values ​​. Programs or tools can make use of these values ​​when parsing and processing annotations. Annotated elements look just like interface methods, the only difference is that you can specify a default value for them. There are restrictions on the types of elements.

An annotation without elements is called a marker annotation.

 All annotations inherit annotation# ##.

package net.mrliuli.annotations;import java.lang.annotation.*;@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface Test {} //(marker annotation)

1.2 Three standard annotations and four meta-annotations

The three standard annotations defined in

java.lang:

  • @Overrided

  • @Deprecated

  • @SuppressWarnings

 Four meta-annotations:

  • @Target indicates where the annotation can be used.

  • @Retension indicates at what level the annotation information needs to be saved.

  • @Documented Include this annotation in Javadoc.

  • @Inherited allows subclasses to inherit annotations from parent classes.

1.3 About annotations

  • The type of annotation elements is limited and cannot be any type. The compiler will Report an error.

  • Default value restrictions:

    @Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface SimulationNull{
        public int id() default -1;    public String description() default "";
    }

    • First, elements cannot have undefined values. That is, the element must either have a default value or provide the element's value when using the annotation.

    • Secondly, for elements of non-basic types, whether they are declared in source code or when defining default values ​​in annotation interfaces, they cannot be

      null as its value. This constraint makes it difficult for an annotation processor to represent the presence or absence of an element, because in each annotation declaration, all elements are present and have corresponding values. In order to get around this constraint, we can only define some special values ​​such as empty strings or negative numbers to indicate that an element does not exist:

  • Generate external files. Some frameworks require some additional information to work with your source code, and in this case annotations are best suited to express their value. Technologies like Enterprise JavaBeans require deployment description files. Web services, custom tag libraries, and object/relational mapping tools (such as Toplink and Hibernate) generally require XML description files, and these description files are separated from the source code.

2 Writing annotation processor

 In the process of using annotations, a very important part is to create and use

Annotation processor, are used to read the annotation .

  • The API of the reflection mechanism can construct an annotation processor

  • The external tool apt can parse Java source code with annotations

Related articles:

Java Programming Thought Learning Class Hours (5) Chapter 18-Java IO System

Java Programming Thought Learning Lesson (6) Chapter 19 - Enumeration Type

The above is the detailed content of Java Programming Thoughts Learning Class (7) Chapter 20 - Notes. For more information, please follow other related articles on the PHP Chinese website!

Statement
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
How to ensure that the listener of @Retryable annotation is called only when specified?How to ensure that the listener of @Retryable annotation is called only when specified?Apr 19, 2025 pm 03:09 PM

How to properly configure the listener for @Retryable annotation in SpringRetry? Using Spring...

How to implement a disaster recovery retry mechanism from serverB to serverC in Spring WebFlux?How to implement a disaster recovery retry mechanism from serverB to serverC in Spring WebFlux?Apr 19, 2025 pm 03:06 PM

Using SpringWebFlux to implement the disaster recovery retry mechanism of LLMGateway. When building an LLMgateway, Spring often needs to be used...

Curve integral variable replacement: How to convert $\int_0^1 \frac{y^2}{\sqrt{1-y^2}}dy$ to $\int_0^{\frac{\pi}{2}}\sin^2tdt$?Curve integral variable replacement: How to convert $\int_0^1 \frac{y^2}{\sqrt{1-y^2}}dy$ to $\int_0^{\frac{\pi}{2}}\sin^2tdt$?Apr 19, 2025 pm 03:03 PM

Questions about variable substitution in curve integral This article will explain in detail the steps of variable substitution in a curve integral calculation. The questioner is calculating...

WeChat applet application: Detailed explanation of the process, fees and relationship with the service account?WeChat applet application: Detailed explanation of the process, fees and relationship with the service account?Apr 19, 2025 pm 03:00 PM

Detailed explanation of the application process and fees of WeChat applets. My friend commissioned the development of WeChat applets. During the application process, I encountered some problems with fees and procedures,...

Java cache data loss: Why can't data be retrieved from cache?Java cache data loss: Why can't data be retrieved from cache?Apr 19, 2025 pm 02:57 PM

Analysis and optimization solutions for the reason why Java cache data cannot be obtained In Java projects, it is common to cache large amounts of data into memory for quick access...

If you want to become a better Java developer, what aspects of JVM are the most worth investing in?
or
Java Advanced: In-depth research on JVM, which core mechanisms are most worth exploring?If you want to become a better Java developer, what aspects of JVM are the most worth investing in? or Java Advanced: In-depth research on JVM, which core mechanisms are most worth exploring?Apr 19, 2025 pm 02:54 PM

In-depth Java: A virtual machine world worth exploring. Many Java developers hope to further improve their technology after mastering the basic syntax and commonly used frameworks...

How to solve the problem of data overwriting and style loss of merged cells when populating Excel templates with EasyExcel?How to solve the problem of data overwriting and style loss of merged cells when populating Excel templates with EasyExcel?Apr 19, 2025 pm 02:51 PM

Frequently Asked Questions When Filling Merged Cells with EasyExcel...

How to efficiently solve the field mapping in system docking through the MapStruct tool?How to efficiently solve the field mapping in system docking through the MapStruct tool?Apr 19, 2025 pm 02:48 PM

Field mapping challenges and solutions in system docking. During the system docking process, you often encounter the need to map the interface fields of one system to another...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.