Crazy God Theory - Notes

What is annotation

Annotation and reflection are the bottom layer of all frameworks
Annotations can be shown not only to programmers but also to computers
Notes; Annotation
Note: Comment

Annotation is from jdk5 0 began to introduce new technologies
Function of Annotation:
It is not the procedure itself, and the procedure can be explained (this is no different from comments) can be read by other programs (such as compilers, etc.)
Format of Annotation:
Comments exist in the code as "@ comment 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

Built in annotation

Meta annotation

package annotation;

import java.lang.annotation.*;

/**
 * Created by This life is vast on 2021/7/6 9:40
 */
@MyAnnotation//Custom annotations are used on classes
public class annoDemo {

    @MyAnnotation//Custom annotations are used on methods
    public void method1()
   {
       @MyAnnotation//Custom annotations are used on local variables
       int a=0;
   }
    @MyAnnotation//Custom annotations are used on member variables
   private static final int k=0;

    @MyAnnotation//Custom annotations are used on member variables
    public String id;

    @MyAnnotation//Custom annotations are used on constructors
    public annoDemo(String id) {
        this.id = id;
    }
}
//Target indicates where our annotation can be used
@Target(value={ElementType.METHOD,ElementType.TYPE,ElementType.FIELD,ElementType.CONSTRUCTOR,ElementType.LOCAL_VARIABLE})//It can be used on methods, member variables and constructors. TYPE represents class and interface, and the latter parameter description
//@Retention indicates where the annotation is still valid. There are three options: RUNTIME/CLASS/SOURCE
//RUNTIME > CLASS > SOURCE, which means that if you define RUNTIME, this annotation is also valid in CLASS and SOURCE
//However, if you define it as SOURCE, it is only valid in SOURCE
@Retention(value= RetentionPolicy.RUNTIME)

@Documented//Indicates whether our annotation is generated in javaDoc
@Inherited//Indicates that the subclass can inherit the annotation of the parent class
@interface  MyAnnotation
{

}

The following error is reported because the value of @ Target is not added with ElementType LOCAL_ VARIABLE

Test the function of javadoc

The following are automatically generated

Let's click annodemo html

The other code does not write @ Documented, so there is no annotation information in the generated html

Understanding of annotation Retention and Retention policy

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    RetentionPolicy value();
}
 
public enum RetentionPolicy {
    //The information of this annotation type will only be recorded in the source file and will be discarded by the compiler at compile time, that is to say
    //It will not be saved in the compiled class information
    SOURCE,
 
    //The compiler records annotations in the class file, but they are not loaded into the JVM. If an annotation declaration does not specify a scope, the system
    //The default value is Class
    CLASS,
 
    //The annotation information will be kept in the source file and class file, and will also be loaded into the Java JVM during execution, so it can be read reflectively.
    RUNTIME
}

Annotations can be divided into three categories according to their life cycle:
1,RetentionPolicy.SOURCE: the annotation is only kept in the source file. When the Java file is compiled into a class file, the annotation is abandoned;
2,RetentionPolicy.CLASS: the annotation is kept in the class file, but it is abandoned when the jvm loads the class file. This is the default life cycle;
3,RetentionPolicy.RUNTIME: the annotation is not only saved in the class file, but still exists after the jvm loads the class file;
These three life cycles correspond to: java source file (. java file) - > Class file - > bytecode in memory.

Custom annotation

The following error is reported. We can either pass a parameter when using the annotation or a name and a default value when defining the annotation

① Annotations can display assignments. If there is no default value, we must assign a value to the annotation

package annotation.annotation;

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

/**
 * Created by This life is vast on 2021/7/6 10:40
 */
public class MyAnno2 {
    @MyAnnotation2(age=1)
    public void test(){
    }
    @MyAnnotation3("111")//When the annotation has only one parameter member, you can copy it directly without writing the parameter name
    public void test2(){
    }
}
@Target(value={ElementType.TYPE,ElementType.METHOD})
@Retention(value= RetentionPolicy.RUNTIME)
@interface MyAnnotation2{
    String name() default "";
    int age();
    int od() default -1;//If the default value is - 1, it means that it does not exist
    String[] schols() default {"Tsinghua University"};
}
@interface MyAnnotation3{
    String value();
}

Barrage area

Custom annotations are actually very useful. You can write annotations for transactions or tools used by children in the future. If you need them later, you can add an annotation directly

Annotation is that some code in your programming can explain the meaning of the code through annotation, which is convenient to read and understand the code
Annotations in the project can be used to verify login and identity authentication

Recommended reading

Retention and RetentionPolicy of Java annotations

Runtime annotation of custom annotation (RetentionPolicy.RUNTIME)

This is a big man's note according to the annotation course of dark horse [Java basics] notes

Keywords: Java

Added by dhillarun on Fri, 21 Jan 2022 07:48:45 +0200