Java annotation definition and usage

1, What is annotation

  • Annotation is a new technology introduced from JDK 5.0

  • Function of Annotation:

    • It is not the procedure itself, which can be explained. This is no different from comments
    • It can be read by other programs (such as compiler, etc.)
  • Format of Annotation:

    • Annotations exist in the code with the @ 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. These metadata can be accessed through reflection mechanism programming.

2, Built in annotation

1,@Override

Defined in java.lang.Override, this annotation is only applicable to rhetorical methods, indicating that one method declaration overrides another method declaration in the superclass

2,@Deprecated

It is defined in java.lang.Deprecated and represents obsolete; 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

It is defined in java.lang.SuppressWarnings to suppress warnings during compilation. It is different from the first two annotations. You need to add a parameter to use it correctly. These parameters have been defined, and you can use them selectively

@SuppressWarnings("all")

@SuppressWarnings("unchecked")

@SuppressWarnings(value={"unchecked","deprecation"})

public class Demo01 extends Object{
    @Override//override method 
    public String toString() {
        return super.toString();
    }
    @Deprecated//Indicates a deprecated method
    public static void test(){
    }
    @SuppressWarnings("all")//Suppress warning
    public static void test01(){
        int age;
    }
    public static void main(String[] args) {
        test();
        test01();
    }
}

3, Meta annotation

1. Role of meta annotation

Is responsible for annotating other annotations

Java defines four standard meta annotation types, which are used to provide descriptions of other annotation types

These types and the classes they support can be found in the java.lang.annotation package

@ Target , @Retention,@Documented , @Inherited

2,@Target

Used to describe the scope of use of annotations (i.e. where the described annotations can be used)

3,@Retention

Indicates the level at which the annotation information needs to be saved, which is used to describe the lifecycle of the annotation (source < class < runtime)

4,@Document

Note that the annotation will be included in the javadoc

5,@Inherited

Note that the subclass can inherit the annotation in the parent class

import java.lang.annotation.*;
@MyAnnotation
public class Demo02 {
    void test(){
    }
}
//Define an annotation
//Target indicates where our annotation can be used
@Target(value = {ElementType.METHOD, ElementType.TYPE})
//Retention indicates where our annotation is still valid.
// runtime>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
@interface MyAnnotation{ }

4, Custom annotation

  1. When using = = @ interface = = custom annotation, it automatically inherits the java.lang. Annotation.annotation interface
  2. 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 no default value, it needs to be assigned in the annotation
    • If there is only one parameter member and the general parameter name is value, value can be omitted. When the name is not value, it cannot 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
import java.lang.annotation.*;
public class Demo03 {
    public static void main(String[] args) {
        
    }
    //Annotations can be assigned explicitly. If there is no default value, we must assign a value to the annotation
    @MyAnnotation2(name = "Zhao Dabao",age = 12)
    public void test(){}
    @MyAnnotation3("baobao")//There is only one parameter and the parameter name is value
    public void test1(){}
}

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

@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation3{
    //There is only one parameter. When the parameter name is value, the parameter name can be omitted
    String value();
}

Keywords: Java Annotation

Added by jola on Mon, 22 Nov 2021 16:20:03 +0200