Self study notes -- Annotation

Keywords: Java Back-end


Work for about a year. Before reviewing, I found that many basic knowledge learning was not solid enough. From today on, I will start the weekly self-study task and record it. This note is a self-study note, which can also be read and viewed by everyone.

What is annotation?
Java Annotation, also known as Java Annotation, is an Annotation mechanism introduced by JDK5.0.
Classes, methods, variables, parameters and packages in the Java language can be labeled. Unlike Javadoc, Java annotations can obtain annotation content through reflection. When the compiler generates class files, annotations can be embedded in bytecode. The Java virtual machine can retain the annotation content and obtain the annotation content at run time. Of course, it also supports custom Java annotations.

Annotation classification:
Built in annotation, meta annotation and user-defined annotation

Annotation details

Java defines a set of annotations. There are 7 in total, 3 in java.lang and the remaining 4 in java.lang.annotation.

Built in annotation:

The annotations that act on the code are

@Override - checks whether the method is an overridden method. If it is found that its parent class or the referenced interface does not have this method, a compilation error will be reported.
@Deprecated - marks obsolete methods. If this method is used, a compilation warning will be reported. (the marked methods are not recommended and are displayed as test();)
@SuppressWarnings - instructs the compiler to ignore warnings declared in annotations. (suppression warning)

Meta annotation:
Annotations (or meta annotations) used in other annotations are:

@Retention - identifies how the annotation is saved, whether it is only in the code, incorporated into the class file, or can be accessed through reflection at run time. (indicates when the current annotation exists, which is divided into source < class < runtime))
@Documented - marks whether these annotations are included in the user document.
@Target - marks which Java member this annotation should be.
Used to control the effective range of custom annotations.

@Target(value = {ElementType.TYPE})
/**Used to describe a Class, interface (including annotation type), or enum declaration */
/** Field declaration (includes enum constants) */
/**Method declaration used to describe the method */
/**Used to describe the parameter Formal parameter declaration */
/**Used to describe Constructor declaration */
/**Used to describe Local variable declaration */
/**Interface Annotation type declaration */
/**Used to describe Package declaration */
* Used to label the type parameter Type parameter declaration
* @since 1.8
*Can mark any type name Use of a type
* @since 1.8

@Inherited - marks the annotation class to which the annotation is inherited (the default annotation does not inherit from any subclasses)

Starting from Java 7, three additional annotations have been added:

@SafeVarargs - Java 7 starts to support, ignoring any warnings generated by method or constructor calls that use parameters as generic variables.
@Functional interface - Java 8 began to support, identifying an anonymous function or functional interface.
@Repeatable - Java 8 began to support, identifying that an annotation can be used multiple times on the same declaration.

Custom annotation:

First step
Building the basic structure

    @interface test{


Step 2
Add scope and scope object

    @interface test{


Step 3
Add parameter

    @interface test{
		String name() default "Default value";
        int age() default 24;

        String[] wife() default {"pleasing", "little dragon maiden", "Zhang Jianing", "and more"};    

Step 4
The fourth step requires us to call this annotation, but we usually use reflection when calling, so I will improve the use of annotation in the reflection part!!!

Reference connection:
Western open source-B station course
Rookie tutorial - interface section
Articles in the station

Posted by haku87 on Sun, 21 Nov 2021 16:49:26 -0800