Annotation and reflection annotation

Annotation

1. Function of Annotation

  1. It is not the procedure itself, which can be explained. (this is no different from the comment)
  2. It can be read by other programs (such as compiler, etc.).

2. Format of Annotation

  1. Comments exist in the code as "@ comment name". You can also add some parameter values, such as @ SuppressWarnings(valut = "unchecked").

3. Use

  1. It can be attached to package, class, method, field, etc. to add additional auxiliary information. We can access these source data through reflection mechanism programming.

Built in annotation

  1. @Override: defined in Java In lang. override, this annotation is only applicable to rhetoric, indicating that one method declaration intends to override another method declaration in the superclass.
  2. @Deprecated: defined in Java In lang. deprecated, this annotation can be used for rhetoric, attributes, and classes to indicate that programmers are not encouraged to use such elements, usually because it is dangerous or there are better choices.
  3. @SuppressWarnings: defined in Java Lang. SuppressWarnings, used to suppress warning messages at compile time. But you need to add a parameter to use it normally:
    • @SuppressWarnings("all")
    • @SuppressWarnings("unchecked")
    • @SuppressWarnings(value = {"unchecked", "deprecation"})
    • Wait

Meta annotation

  1. The function of meta annotation is to annotate other annotations. Java defines four standard meta annotation types to describe other annotation types:
    • @Target: used to describe the scope of use of annotations (i.e. where the described annotations can be used)
    • @Retention: indicates the level at which the annotation information needs to be saved, which is used to describe the annotation life cycle (source < < class < < runtime))
    • @Documented: indicates that the annotation is included in the javadoc
    • @Inherited: indicates that the subclass can inherit the annotation in the parent class
  2. Use reference
import java.lang.annotation.*;

@MyAnnotation
public class Test {

   //@MyAnnotation
   public void test() {

   }
}

//Define an annotation
//Target indicates where our annotations can be used
//@Target(value = ElementType.METHOD) / / can only be valid on Methods
@Target(value = {ElementType.METHOD, ElementType.TYPE}) //Can act on methods and classes

//Retention indicates where our annotation is still valid
//runtion > class > sources
@Retention(value = RetentionPolicy.RUNTIME)

//Documented indicates whether our annotations are generated in JAVAdoc
@Documented

//
//The Inherited subclass can inherit the annotation of the parent class
@Inherited

//Custom annotation MyAnnotation
@interface MyAnnotation{

}

Custom annotation

  1. When using @ inerface custom annotation, it automatically inherits Java Lang. annotation interface.
  2. analysis:
    • @Interface is used to declare an annotation. Format: public @ interface annotation name {definition content} (public can be removed if necessary in class)
    • Each method actually declares a configuration parameter.
    • The return value type is the type of the parameter (the return value can only be the basic type, Class, String, enum).
    • You can declare the default value of the parameter through default.
    • If there is only one parameter member, the general parameter name is value.
    • An annotation element must have a value. When defining an annotation element, we often use an empty string and 0 as the default value.
  3. Use reference
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

//Custom annotation
public class Test01 {
    //The annotation can display the assignment. If there is no default value, we must assign a value to the annotation, or an error will be reported
    @MyAnnotation1(schools = "Fudan")
    public void test(){}

    @MyAnnotation2("Jiaotong University") //value can be omitted
    public void test2(){}
}

//Target scope
@Target({ElementType.TYPE, ElementType.METHOD}) //Act on classes and methods
//In which range is it valid
@Retention(RetentionPolicy.RUNTIME) //Valid in operation
@interface MyAnnotation1{
    //Annotated parameters: parameter type + parameter name ()
    String name() default ""; //Add default value is empty
    int age() default 0; //The default value is 0
    int id() default -1; //If the default value is - 1, it means that it does not exist

    String[] schools(); //No default
}

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    String value(); //If the annotation has only one value, you can use value
}

Tags: Java

Posted by lookee on Wed, 11 May 2022 05:46:49 +0300