Java from contact to abandonment -- Annotation

Day Twenty-Two

annotation

What is annotation

  • Annotation is from jdk5 0 began to introduce new technologies
  • Function of Annotation:
    • Not the program itself, you can explain the program. This is no different from ordinary comments.
    • It can be read by other programs (such as compilers). Annotations and reflection can achieve dynamic performance, which can be read through reflection.
  • Format of Annotation:
    • Annotations exist in the code as "@ annotation name". You can also add some parameter values, such as @ SuppressWarnings(value = "unchecked").
  • Where is Annotation used?
    • It can be attached to package, class, method, field, etc., which is equivalent to adding additional auxiliary information to them. We can access these metadata through reflection mechanism programming.
//What is annotation
public class Test01 {
    //@Override is a rewritten annotation
    @Override
    public String toString() {
        return super.toString();
    }
}

Built in annotation

  • @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.
  • @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.
  • @SuppressWarnings: defined in Java Lang. SuppressWarnings, used to suppress warnings at compile time.
    • Different from the previous two comments, you need to add a parameter to use it correctly. These parameters have been defined. We can use them selectively.
    • @SuppressWarnings("all")
    • @SuppressWarnings("unchecked")
    • @SuppressWarnings(value = {"unchecked","deprecation"})
    • Wait

Meta annotation

  • The function of meta annotation is to annotate other annotations. Java defines four standard meta annotation types, which are used to describe other annotation types.
  • These types and the classes they support are in Java Lang.annotation package (@ Target, @ Retention, @ Documented, @ Inherited)
    • @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. It is used to describe the annotation life cycle
      • (SOURCE<CLASS<RUNTIME)
    • @Documented: indicates that the annotation will be included in the javadoc
    • @Inherited: indicates that the subclass can inherit the annotation in the parent class
//Test meta annotation    
@MyAnnotation
public class Test01 {
    @MyAnnotation
    public void test(){
        
    }
}

//Define an annotation
//@Target indicates where annotations can be used
//The parameters inside can be a set or a
@Target(value = {ElementType.METHOD,ElementType.TYPE})

//@Retention indicates where the annotation is still valid
//RUNTIME>CLASS>SOURCES
@Retention(value = RetentionPolicy.RUNTIME)

//@Documented indicates whether annotations are generated in Javadoc
@Documented

//@Inherited means that the subclass can inherit the annotation of the parent class
@Inherited
@interface MyAnnotation{
}

Custom annotation

  • When you use @ interface to customize annotations, you automatically inherit Java lang.annotation. Annotation interface
  • analysis:
    • @Interface is used to declare an annotation. Format: public @interface annotation name {definition content}
    • Each of these methods actually declares a configuration parameter
    • The name of the method is the name of the 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, and value can be omitted above. Value can be omitted.
    • An annotation element must have a value. When defining an annotation element, we often use an empty string with 0 as the default value
//Custom annotation
public class Test01 {
    //Annotations can display assignments. If there is no default value, we must assign a value to the annotation
    @MyAnnotation(age = 22,name = "LH")
    public void test(){ }
    @MyAnnotation2(" ")
    public void test1(){ }
}

@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    //Annotated parameters: parameter type + parameter name + ();
    String name() default "";
    int age();
    int id() default -1;//If the default value is - 1, it means that it does not exist

    String[] phones() default {"Huawei, apple"};
}

@Target(value = {ElementType.METHOD,ElementType.TYPE})
@Retention(value = RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    String value();
}

Keywords: Java

Added by kotun on Wed, 22 Dec 2021 19:02:14 +0200