Annotation and reflection 01--what is an annotation

what is annotation

1. Annotation is a new technology introduced from JDK5.0
2. The function of annotation
(1) is not the program itself, it can explain the program (same as annotation)
(2) It can be read by other programs , such as compiler
3 . The format of the annotation
Annotation exists in the code with "@ annotation name", and some parameter values ​​can also be added, such as: @GetMapping("page-info")
4. Where the annotation is used
can be attached to package, class, method , field, etc., which is equivalent to adding additional auxiliary information to them, and we can program to access these metadata through the reflection mechanism

built-in annotation

1. @Override : Defined in java.lang.Override, this annotation is only applicable to rhetorical methods, indicating that a method declaration intends to override another method declaration in the superclass. If you annotate a method with this annotation type, the compiler is required to generate an error message unless at least one of the following conditions is met:
(1) The method will override or implement a method that lives in the supertype.
(2) The method has the same signature as an override of any public method declared in Object.
If you change toString to tostring, an error will be reported

/**
     * @Override 重写的注解
     * @return
     */
    @Override
    public String toString() {
    
    
        return super.toString();
    }

2. @Deprecated : Defined in java.lang.Deprecated, this annotation can be used to modify methods, properties, classes, and logos. Programmers are discouraged from using such elements, usually because they are dangerous or there are better options. The compiler issues a warning when using or overriding deprecated program elements in code that is not enabled.
insert image description hereWe can see that the test method is not recommended. Although it is not recommended, it does not mean that it cannot be used. After running, it is found that the program can still output.

3. @SuppressWarnings : Defined in java.lang.SuppressWarnings, used to suppress warning messages during compilation. Different from the previous two annotations, we need to add a parameter to use it correctly. These parameters are defined, and we can use them selectively.
(1) @SuppressWarnings("all")
(2) @SuppressWarnings("unchecked")
(3) @SuppressWarnings(value={"unchecked", "deprecation"})
(4) ...

Creating a new unused method or object will issue a warning, and the @SuppressWarnings annotation can suppress these warnings.
insert image description here
insert image description here

meta annotation

The role of meta-annotation is to annotate other annotations. Java defines four standard meta-annotation types, which are used to provide explanations for other annotation types. These types and their supporting classes can be found in the java.lang.annotation package.
(1) @Target : Used to describe the scope of use of annotations (that is, where the described annotations can be used)
(2) @Retention : Indicates at what level the annotation information needs to be saved, and is used to describe the life cycle of annotations.
(3) @Document: Indicates that the annotation will be included in javadoc.
(4) @Inherited: Indicates that the subclass can inherit the annotation of the parent class.

custom annotation

Using @interface custom annotations automatically inherits the java.lang.annotation.Annotation interface.
(1) @interface is used to declare an annotation, format: public @interface annotation name (definition content).
(2) Each of these methods actually declares a configuration parameter.
(3) The name of the method is the name of the parameter
(4) The return value type is the type of the parameter (the return value can only be the basic type, Class, String, enum).
(5) The default value of the parameter can be declared by default.
(6) If there is only one parameter member, the general parameter name is value.
(7) The annotation element must have a value. When we define the annotation element, an empty string is often used, and 0 is used as the default value.

//自定义注解
public class Test {
    
    
    //注解可以显示赋值,如果没有默认值,我们就必须给注解赋值,不然就会报错。
    @MyAnnotation(age = 19,name = "小王")
    public void test(){
    
    

    }

    @MyAnnotation1("李四")
    public void test2(){
    
    

    }
}

@Target({
    
    ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation{
    
    

    String name() default "";

    int age() default 0;

    int id() default -1; //如果默认值为-1,代表不存在

    String[] schools() default {
    
    "测试用例","清华大学"};
}

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

}

Guess you like

Origin blog.csdn.net/cang_ling/article/details/131954159