Annotations classification

  • Take notes@Override\@DeprecatedEtc.
  • Yuan notes@Retention @Target @Inherited @Documented @RepeatableA note that embellishes a note
  • Custom annotations

Common notes

Common Java annotations are as follows:

1. @deprecated -- The tagged content is no longer recommended for use; <br/> 2. @override -- Can only call a method, indicating that the method overrides a method in its parent class; Documented -- Documented in Javadoc. 4. @Inherited -- Can only be used to annotate "Annotation type". Annotations annotated by this Annotation are Inherited; @Retention -- can only be used to annotate the Annotation type, and it is used to specify the Annotation's RetentionPolicy property; 6. @target -- Can only be used to annotate the Annotation type, and it is used to specify the Annotation ElementType attribute; 7. @SuppressWarnings - A warning generated by the annotated content, for which the compiler will keep silent; @interface - used to define an annotation;Copy the code

Among them, 4, 5, 6, 8 are mostly used for custom annotations, readers should remember.

Custom annotations

We use @interface to declare annotations as follows:

// Define an empty annotation
public @interface MyAnnotation{}
Copy the code

With some meta annotations, you can implement custom annotations according to your needs.

Common meta-annotations include @Retention, @target, @document, @Inherited, and @REPEATable.

A note of five meta notes

@Retention

  1. Retention refers to the Retention phase in which annotations are retained in source code (compile-time), bytecode (class-load), or runtime (run in the JVM).

    Use the enumeration RetentionPolicy in the @Retention annotation to indicate the annotation Retention period:

    • @Retention(retentionPolicy.source), annotations exist only in the SOURCE code and are not included in class bytecode files
    • @Retention(retentionPolicy.class), the default Retention policy. Annotations are present in the CLASS bytecode file but not available at runtime
    • @Retention(retentionPolicy.runtime), annotations are stored in class bytecode files and can be retrieved by reflection at RUNTIME
  2. If we are making custom annotations, then from the previous analysis, our custom annotations will not work if they are only stored in the source code or in the bytecode file, and the annotation can be obtained at runtime to achieve our purpose. So custom annotations must use @Retention(retentionPolicy.runtime) as follows:

@Retention(RetentionPolicy.RUNTIME)
public @interface MyTestAnnotation {

}
Copy the code

@Target

The @target meta-annotation can be a class, a method, a method parameter, or a method parameter. It is also used to express the type of the annotation by enumerating the class ElementType:

@target (ElementType.TYPE) applies to interfaces, classes, enumerations, annotations @target (ElementType.FIELD) applies to attribute fields, and enumerated constants @target (ElementType.METHOD) applies to methods @target (elementtype.parameter) uses the method argument @target (elementtype.constructor) uses the local variable @target (elementtype.local_variable) @target (elementType.annotation_type) on annotations (this attribute is used in the @Retention annotation) @target (elementType.package) on packages @target (elementType.type_parameter) applies to type generics, that is, generic methods, generic classes, generic interfaces (added in JDk1.8). Can be used to annotate any type except class (added in JDk1.8)Copy the code

The most commonly used TYPE is elementtype. TYPE, as follows:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface MyTestAnnotation {

}
Copy the code

@Documented

Document means Document in English. It provides the ability to include elements from annotations into Javadoc.

@Inherited

Inherited means inheritance, but this is very similar to the way we understand inheritance. An annotation annotated by @Inherited modifs a parent class. If the child class is not annotated by other annotations, the child class inherits the parent class.

@Repeatable

Repeatable means Repeatable in English. As the name implies, an annotation modified by this meta-annotation can apply to an object more than once, but each annotation can represent a different meaning.

Annotated source code analysis

Let’s take @Override as an example to analyze its source code. To view a normal class, hold down the CTRL key and click @Override to access its source code, as follows:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}
Copy the code

We see @ Override annotation is through @ interface annotations to define a common annotations, and we know that using the @ interface definition annotations, means that it implements the Java lang. The annotation. The annotation interface, That Annotation is an Annotation

Note: @Interface is mandatory when defining the Annotation, and it’s different from our usual Implemented approach to interfaces. The implementation details of the Annotation interface are all done by the compiler. After an annotation is defined with @interface, it cannot inherit from other annotations or interfaces.

Let’s analyze the Annotation class source code as follows:

public interface Annotation {
    boolean equals(Object var1);

    int hashCode(a);

    String toString(a);

    Class<? extends Annotation> annotationType();
}
Copy the code

From the above source code, we know that annotations themselves are subinterfaces of the Annotation interface. In other words, annotations can have properties and methods, but the properties in the interface are static and final, which makes no sense for annotations. The method we define the interface is just like the properties of annotations. This explains why annotations only have attribute member variables, which are actually interface methods. This is why member variables have parentheses. Unlike interfaces, we can assign values to member variables in parentheses.

Architecture of Java annotations

Based on the above source code analysis, we conclude that Java annotations are structured as follows:

Annotations are interface classes that inherit from the Annotation interface class

1. The association between an Annotation and a RetentionPolicy can be understood as: Every Annotation object has a unique RetentionPolicy attribute;

2. 1 Annotation and 1~ N ElementType associations can be understood as: for every 1 Annotation object, there can be several ElementType attributes;

Annotation has many implementation classes, including: Deprecated, Documented, Inherited, Override, and so on. Each implementation class of annotations is associated with one RetentionPolicy and 1 to n ElementTypes.

The role of annotations

Before we talk about annotations, let’s take a look at the differences between XML and annotations:

  • Note: this is distributed metadata that is tightly bound to source code.

    XML: Is centralized metadata with no binding to source code

    This part is mostly used in the development of configuration items in the Java background. We know that a few years ago the configuration items of the server were stored in an XML file, but spring 2.5 has started to use annotation-based configuration to replace configuration files.

Annotations can be used for many purposes. The above is just a simple example. In general, annotations serve the following four main purposes:

1. Generate documentation, generating Javadoc documentation from metadata identified in the code.

2. Compile checking, which allows the compiler to check and verify the metadata identified in the code at compile time.

3. Dynamic processing at compile time, through the metadata identified in the code at compile time, such as dynamically generated code.

4. Dynamic processing at run time, which is handled dynamically through metadata identified in the code, for example using reflection injection instances