An overview of the

Annotations are a concept introduced in Java5. You can think of annotations as labels that attach a label to a specified method, class, variable, parameter, package, and so on.

 

The @override annotation tells the compiler that the method is the overridden parent.

@test, @deprecated, and so on

Custom annotations

It’s also easy to define the syntax for your own annotations:

 

You can then use the annotation.

Note that annotations don’t inherit, and the default inherits the Annotation interface.

In Java, there are special annotations called meta-annotations, which are simply annotations that can be annotated to indicate the scope of the annotation, etc. There are the following meta-annotations:

1.@Retention

Retention means Retention, which marks the survival time of annotations, and its value is as follows:

  • Retentionpolicy. SOURCE: Annotations are only retained at the SOURCE stage and are discarded and ignored when the compiler compiles.
  • Retentionpolicy.class: Annotations are only reserved until compilation, and are not loaded into the JVM.
  • Retentionpolicy.runtime: Annotations can be kept until the application is run, and they are loaded into the JVM, so they can be retrieved while the application is running.

2.@Documented

A DOCacademia is a containment of elements from a annotation into a Javadoc

3.@Target

Target means target. It specifies where the annotation should be used, such as method, class, etc. It has the following values:

  • Elementtype. ANNOTATION_TYPE: Annotates annotations
  • Elementtype. CONSTRUCTOR: Annotate the CONSTRUCTOR
  • Elementtype. FIELD: Annotates attributes
  • Elementtype. LOCAL_VARIABLE: Annotates local variables
  • Elementtype. METHOD: Annotates methods
  • Elementtype. PACKAGE: Annotates a PACKAGE
  • Elementtype. PARAMETER: Annotates parameters within a method
  • Elementtype. TYPE: Annotates a TYPE, such as class, interface, enumeration

Use commas (,) to separate multiple entries. For example:

@target (value={elementtype.annotation_type, elementtype.constructor})Copy the code

4.@Inherited

Inherited means Inherited. If a superclass is annotated with @Inherited annotations, its subclasses will inherit the annotations of the superclass if they are not annotated with any annotations.

5.@Repeatable

Repeatable means Repeatable, that is, the annotation can be repeated multiple times.

 

So that’s meta-annotations in Java.

Annotation attributes:

Annotations have only member variables. Attributes are declared in annotations in the form of a method with no parameters. The method name is the name of the variable, and the return value is the type of the variable

Attributes are declared as follows:

// int id int id(); // String MSG variable String MSG (); // int specifies the id of type. The default value is -1, that is, the variable can be assigned a value in the declaration.Copy the code

As noted below:

 

In addition, if the annotation contains only one value attribute, it can be used without specifying the attribute name, as shown below:

 

If there are no attributes in the annotation, the parentheses can be omitted when using the annotation, as shown below:

 

That’s how you customize annotations

Annotation extraction

Now that you have a custom annotation and need to use it, let’s take a look at annotation extraction

The base class used to describe annotations in Java is the Annotation

To extract annotations, use reflection, the Class object’s method as follows:

  • isAnnotationPresent(Class
    ): verifies that an Annotation is applied to a Class object
  • GetAnnotation (Class): Get the annotation object
  • GetAnnotations (): retrieves an array of annotation objects, including annotations inherited from their parent class
  • GetDeclaredAnnotations () : Retrieves an array of annotations, excluding any inherited from a parent class

Add the following comments:

 

The operations are as follows:

 

Running results:

 

Note that @Retention(retentionPolicy.runtime) is required if an annotation needs to be retrieved at RUNTIME.

This is the definition and extraction of annotations

Java preconfigured annotations

  1. @deprecated: Marks obsolete methods
  2. Override: Marks the method that overrides the parent class
  3. @SuppressWarnings: Block warnings
  4. FunctionallInterface: FunctionallInterface annotations