This is the second day of my participation in the August More text Challenge. For details, see:August is more challenging

annotations

Annotations, “annotations,” are also called metadata. A code level description. It is a feature introduced in JDK1.5 and later and is at the same level as classes, interfaces, and enumerations. It can be declared in front of packages, classes, fields, methods, local variables, method parameters, etc., to explain and annotate these elements. (From Baidu Encyclopedia)

How do I declare an annotation

An annotation that defines an annotation is different from a class and is decorated with @interface, and the annotation that is decorated on top of the “annotation” is called a meta-annotation.

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE_PARAMETER)
@Retention(RetentionPolicy.CLASS)
public @interface CustonAnnotation {
    String name(a) default "Shixf";
}
Copy the code

When the definition is complete, place the cursor on the annotation name with Ctrl+U. (IDEA opens the interface method shortcut key of the parent class) All annotations default Annotation interface.


public interface Annotation {
 
    boolean equals(Object obj);
 
    int hashCode(a);
 
    String toString(a);
 
    Class<? extends Annotation> annotationType();
}
Copy the code

Yuan notes

  • Meta-annotations: Other annotation declarations can also be used when an annotation is defined. Annotations that annotate annotation types are called meta-anntotain.
  • @target: Limits the types of Java elements to which annotations can be applied. The target annotation specifies one of the following element types as its value.
public enum ElementType {
    Class, interface (including annotation types), or enumeration declarations */
    TYPE,

    /** Applies to member variables (including enumerations) */
    FIELD,

    /** Limitation applies to methods */
    METHOD,

    /** method parameter */
    PARAMETER,

    /** applies to the constructor */
    CONSTRUCTOR,

    /** Apply local variables to methods */
    LOCAL_VARIABLE,

    /** Applies to the annotation type */
    ANNOTATION_TYPE,

    /** apply to packages */
    PACKAGE,

    /** * Type parameter declaration *@since1.8 * /
    TYPE_PARAMETER,

    /** * Use of a type *@since1.8 * /
    TYPE_USE
}
Copy the code

In particular, TYPE_PARAMETER can be applied to generics as introduced in Jdk1.8. I haven’t seen it used anywhere yet.Note: This note is stated above.


  • @Retention: Retention level for annotations
Public enum RetentionPolicy {/** * tokens are retained only at SOURCE level and are ignored by the compiler */ SOURCE, /** tokens are retained by the compiler at compile time, But the Java virtual machine ignores it */ CLASS, and the /** * tag's annotations are retained by the JVM, so the RUNTIME environment can use it */ RUNTIME}Copy the code

There are also @Documented and @inherited metannotations. The former is used to be Documented by the Javadoc tool, and the latter indicates that a child is allowed to inherit an annotation defined in its parent class. I won’t go into too much detail here. No explanation doesn’t mean it’s not important.

Application scenarios of annotations

According to the @Retention meta-annotation, annotations can be used in three scenarios

  • SOURCE: available at SOURCE level for IDE syntax checking, APT, etc. (use SOURCE level annotations in classes, not in compiled classes)

For example, @Dimension in Android

@Documented
@Retention(SOURCE)
@Target({METHOD,PARAMETER,FIELD,LOCAL_VARIABLE,ANNOTATION_TYPE})
public @interface Dimension {
    @Unit
    int unit() default PX;

    int DP = 0;
    int PX = 1;
    int SP = 2;

    @IntDef({PX, DP, SP})
    @Retention(SOURCE)
    @interface Unit {}
}
Copy the code

Originally, I thought @idres in findViewById was using the SOURCE level, originally I used the CLASS level, take a small notebook notebook, research.

  • CLASS: Annotations defined as CLASS are retained in the CLASS file but are ignored by the virtual machine (that is, they cannot be reflected at run time). The scenarios where this annotation is fully applicable are bytecode operations such as AspectJ and hot fix Roubust.
  • RUNTIME: Annotations are retained at RUNTIME, meaning that we can use reflection techniques to retrieve all the information in the annotations at RUNTIME.