A list,

Annotations were introduced in Java 1.5 to provide additional information about your code and are now widely used. In addition to built-in Java annotations, we can also customize annotations.

Here is a simple example of a custom annotation:

@target (elementtype.type) // Annotations for types (classes, interfaces, annotations, enumerations) @retention (retentionPolicy.runtime) The @inherited // annotation will be applied to the @documented // subclass of the class to generate a Javadoc file public @interface JsAnnotation {String DEFAULT_VALUE ="JS"; /** * Create this value method so that annotations can be passed directly, such as @jsannotation ("msg"* * @)return
     */
    String value() default DEFAULT_VALUE;

}
Copy the code

Key points of custom annotations:

  • The @interface keyword defines annotations
  • Annotations can be modified by other annotations, the most important of which are meta-annotations
  • Annotations, like interfaces, can define constants and methods internally
  • Methods defined by annotations have some limitations: methods cannot have parameters; Return values can only be primitive types, strings, classes, enumerations, annotations, and arrays of the above types. You can include default values

2. Introduction of meta-notes

A meta-annotation is the annotation that defines the annotation.

Including @target, @Retention, @Inherited, and @documented

1. @ Target

Describe the purpose of the annotation.

The source code is:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
public @interface Target {
    ElementType[] value();
}
Copy the code

The annotation method returns the value ElementType[], which is an enumeration type. The enumeration value is the value that the @target annotation can take on.

Value, which eliminates the need to specify a method name when using annotations.

Desirable values are:

The values meaning
ElementType.PACKAGE Annotations apply to packages
ElementType.TYPE Annotations apply to types (classes, interfaces, annotations, enumerations)
ElementType.ANNOTATION_TYPE Annotations act on annotations
ElementType.CONSTRUCTOR Annotations apply to constructors
ElementType.METHOD Annotations apply to methods
ElementType.PARAMETER Annotations apply to method parameters
ElementType.FIELD Annotations apply to attributes
ElementType.LOCAL_VARIABLE Annotations apply to local variables

Note: The default can be applied to any of these targets.

2, @ Retention

Describe the annotation lifecycle.

The source code is:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
public @interface Retention {
    RetentionPolicy value();
}
Copy the code

The annotation method returns a value of the enumerated type RetentionPolicy, which is the available value for the @Retention annotation.

Desirable values are:

The values meaning
RetentionPolicy.SOURCE Source reserved, compile time can be processed
RetentionPolicy.CLASS The Class file is reserved for Class loading
RetentionPolicy.RUNTIME Reserved at run time and processed at run time

Default retentionPolicy.class value.

3, @ Documented

The description annotation can be documented and is a markup annotation.

When javadoc is generated, there is no annotation, but if the annotation is qualified with @documented, the resulting document contains that annotation.

The source code is:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
public @interface Documented {
}
Copy the code

4, @ Inherited

Markup annotations. When an annotation is applied to a class using the @Inherited annotation, the annotation will be applied to the children of that class.

The source code is:

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE})
public @interface Inherited {
}
Copy the code

Custom annotation examples

1. Define annotations

@target (elementtype.type) // Annotations for types (classes, interfaces, annotations, enumerations) @retention (retentionPolicy.runtime) The @inherited // annotation will be applied to the @documented // subclass of the class to generate a Javadoc file public @interface JsAnnotation {String DEFAULT_VALUE ="JS"; /** * @author peida */ enum Color {BULE, RED, GREEN}"msg"* * @)return*/ String value() default DEFAULT_VALUE; /** * This method returns an enumeration type, when using annotations such as @jsannotation (color= color.blue) ** @return
     */
    Color color() default Color.BULE;

    int num() default -1;
}
Copy the code

Where: three methods are defined, all of which have default return values. Defines a Color enumeration (because annotated methods can return enumeration types)

Note here:

  • Value method: Pass the notation directly when using annotations, such as @jsannotation (” MSG “)
  • Normal methods: When using annotations, such as @jsannotation (color= color.blue)

2. Use annotations

@JsAnnotation("jia shuai")
public class AnnotationTest {
    
}
Copy the code

or

@JsAnnotation(num = 100)
public class AnnotationTest {
    
}
Copy the code

or

@JsAnnotation(color = Color.BLUE)
public class AnnotationTest {
    
}
Copy the code

3. Get annotations

public static void main(String[] args) {
    AnnotationTest test = new AnnotationTest();
    Class tClass = test.getClass();
    JsAnnotation jsAnnotation = (JsAnnotation) tClass.getAnnotation(JsAnnotation.class);

    System.out.println(jsAnnotation.value());
    System.out.println(jsAnnotation.color());
    System.out.println(jsAnnotation.num());
}
Copy the code

More exciting content to follow my wechat public number – Android motor vehicle