Java annotated study notes

  • 2021-08-31 08:07:34
  • OfStack

Brief introduction

Java annotation is an annotation mechanism introduced by JDK 1.5, which does not change the compiler's compilation method. Java compiler will generate the same Java virtual machine instructions for code with and without annotations. In practical application, annotation is only a kind of identification, and the specific operation needs to be analyzed and processed by other tools.

Annotation grammar

Annotations are defined using @ interface, and all annotations are implicitly extended from the java. lang. annotation. Annotation interface.

The following MyFirstAnnotation is a custom annotation with two parameters name and value, both of which default to empty strings. It is also marked with two meta-annotations: @ Retention and @ Target:


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyFirstAnnotation {
  String name() default "";

  String value() default "";
}

Annotation parameter types are limited and must be limited to the following types:

Basic type String Class enum Type Annotation type An array of the types described above

For example, if you do not use the above, a compilation error will occur:


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyFirstAnnotation {
  String name() default "";

  Integer value() default 1;// Wrapper type is not good either, compile error 
}

@ Target and @ Retention these annotations, which act on annotations, are called meta-annotations. Commonly used meta-annotations are as follows:

1. @ Target, used to identify where annotations can be annotated, and receives 1 ElementType [] parameter. Parameter values can refer to ElementType enumeration class:


public enum ElementType {
  /**  Class, interface ( Include annotation types ) Or enumeration declaration  */
  TYPE,

  /**  Field declaration ( Include enum Constant ) */
  FIELD,

  /**  Method declaration  */
  METHOD,

  /**  Parameter declaration  */
  PARAMETER,

  /**  Constructor declaration  */
  CONSTRUCTOR,

  /**  Local variable declaration  */
  LOCAL_VARIABLE,

  /**  Annotation type declaration  */
  ANNOTATION_TYPE,

  /**  Package declaration  */
  PACKAGE,

  /**
   *  Type parameter declaration 
   *
   * @since 1.8
   */
  TYPE_PARAMETER,

  /**
   *  Any type name 
   *
   * @since 1.8
   */
  TYPE_USE
}

A compilation error occurs when the specified @ Target and annotation use location do not match. As shown below, @ Target (ElementType. METHOD) means that MyFirstAnnotation annotations can only be used to annotate methods, and annotations on classes cause compilation errors:


// Compilation error 
@MyFirstAnnotation
public class Demo {

  // Correct 
  @MyFirstAnnotation
  public void doSomeThing(){
  }

}

2. @ Retention, which is used to identify how long annotations can be kept. One RetentionPolicy parameter is received, and the parameter value can refer to RetentionPolicy enumeration class:


public enum RetentionPolicy {
  /**
   *  Indicates that annotations only exist in .java Is not preserved to the compiled source code file of .class In a file 
   */
  SOURCE,

  /**
   *  Indicates that annotations can be preserved to .class File, but will not be Java Loaded by virtual machine 
   */
  CLASS,

  /**
   *  Indicates that annotations can be preserved to .class File and loaded by the virtual machine 
   */
  RUNTIME
}

3. @ Documented, indicating that this annotation can appear in javadoc.

4. @ Inherited, which means that when this annotation is used for a class, it can be automatically inherited by its subclasses.

5. @ Repeatable, which means that this annotation can be applied multiple times in the same location.

By default, adding multiple duplicate comments in the same location will cause compilation errors:


public class Demo {
  // Compilation error 
  @MyFirstAnnotation(name = " Zhang 3")
  @MyFirstAnnotation(name = " Li 4")
  @MyFirstAnnotation(name = " Wang 5")
  public void doSomeThing(){
  }

}

You can make @ MyFirstAnnotation reusable by adding @ Repeatable meta annotations.
First, you need to create a container annotation @ MyFirstAnnotations. The container annotation @ MyFirstAnnotations must have a parameter value and its type is MyFirstAnnotation []:


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyFirstAnnotations {
  MyFirstAnnotation[] value();
}

Then add the @ Repeatable annotation and specify the container annotation.


@Repeatable(MyFirstAnnotations.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyFirstAnnotation {
  String name() default "";

  String value() default "";
}

Note that the annotation parameter cannot be null, and the default value cannot be null.

Custom annotation test

Defines the annotation @ RepeatMethod with the int type parameter value.


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

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RepeatMethod {

  int value() default 1;

}

Apply annotations to the doSomeThing method. If the annotation parameter to be specified is value, and no other parameter value is specified, you can use abbreviation to omit the parameter name and equal sign.


public class Demo {
  @RepeatMethod(5)
  public void doSomeThing(){
    System.out.println("---- Annotation test ----");
  }
}

Obtain annotation information through reflection, and then do corresponding processing. For example, suppose that the annotation of @ RepeatMethod is to call the annotated method repeatedly, and the parameter value specifies the number of times to call the method repeatedly:


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyFirstAnnotation {
  String name() default "";

  Integer value() default 1;// Wrapper type is not good either, compile error 
}
0

Output:


@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyFirstAnnotation {
  String name() default "";

  Integer value() default 1;// Wrapper type is not good either, compile error 
}
1

These are the details of Java annotation study notes. For more information about Java annotation, please pay attention to other related articles on this site!


Related articles: