what is annotation
Annotation is a new data type, which is very similar to interface and is at the same level as class, interface and enumeration.
Equivalent to a mark, starting with @, can declare the front of classes, properties, methods, parameters, etc. Used to describe, label, and implement specific functions for elements
Function:
1. Writing documents: generating documents according to annotations
2. Code analysis: analyzing the code using reflection through annotations
3. Compilation inspection: enabling the compiler to implement basic type checking through annotations, such as Override
JDK predefined annotations
in the java.lang package
annotation | effect |
---|---|
@Override | In front of the method, override the method of the parent class |
@Deprecated | In front of classes, attributes, and methods, it means outdated and not recommended |
@SuppressWarning | In front of classes, attributes, and methods, it means to close the warning message |
package cn.xxx.annotation;
import java.util.Date;
@SuppressWarnings("all")
public class AnnoDemo2 {
@Override
public String toString() {
return super.toString();
}
@Deprecated//过期注解
public void show1(){
//有缺陷
}
public void show2(){
//替代show1方法
}
public void demo(){
show1();
Date date = new Date();
}
}
custom annotation
Using @interface
+ meta-annotations
// 元注解
public @interface 注解名 {
// 抽象方法或者叫属性列表
}
Meta-annotations: Annotations tagged on annotations
meta annotation | effect |
---|---|
@Target | Define the scope of annotations, that is, which elements can be marked on, such as classes, methods, etc. When omitted, it can be before any element |
@Retention | Define the life cycle of annotations, SOURCE, CLASS, RUNTIME, keep in compilation, keep in the class, generally use RUNTIME |
@Documented | Whether to include annotations when generating documentation using javadoc |
@Inheried | Allow subclasses to inherit parent class annotations |
Annotations are finally compiled to:
public interface MyAnno extends java.lang.annotation.Annotation {
}
So the essence of annotation is an interface, which inherits the Annotation interface by default
package cn.xxx.annotation;
import java.lang.annotation.*;
@Target({
ElementType.TYPE,ElementType.METHOD,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface MyAnno3 {
}
Use annotations
grammar:
@注解名(属性名=属性值,属性名=属性值)
- When using annotations, you need to specify the return value for the abstract method in the annotation, also known as specifying the attribute value for the annotation.
- The attribute name is the name of the abstract method in the annotation
package cn.xxx.annotation;
public @interface MyAnno {
int value();
Person per();
MyAnno2 anno2();
String[] strs();
/*String name() default "张三";*/
/*String show2();
Person per();
MyAnno2 anno2();
String[] strs();*/
}
package cn.xxx.annotation;
@MyAnno(value=12,per = Person.P3,anno2 = @MyAnno2,strs={
"bbb","aaa"})
@MyAnno3
public class Worker {
@MyAnno3
public String name = "aaa";
@MyAnno3
public void show () {
}
}
parsing annotations
For annotations whose life cycle is RUNTIME. Parsing annotations is to obtain the attribute values defined in the annotations. There are three steps:
- Get the object of the location (Class, Method, Field) defined by the annotation
- Get the specified annotation
- getAnnotation(Class)
//其实就是在内存中生成了一个该注解接口的子类实现对象 public class ProImpl implements Pro{ public String className(){ return "cn.itcast.annotation.Demo1"; } public String methodName(){ return "show"; } }
- Call the abstract method in the annotation to get the configured attribute value.
Example:
// 1、获取类上的注解
Class cls = Student.class;
// 2、获取该类Student上的所有注解,不一定获取类上的注解,也可以获取方法上的注解,这时候就是 `method.getAnnotation()`
Annotation[] annotation = cls.getAnnotations();
// 仅获取该类上的Pro注解
Pro annotation = cls.getAnnotation(Pro.class);
// 3、获取注解上的属性方法
String className = annotation.className() // cn.xxx.annotation.Demo1
String className = annotation.methodName() // show
...
Summarize
An annotation is something similar to a label. For example, if you put a Check annotation on a function, it means that the function can be checked, then the corresponding check program can execute the corresponding program by recognizing the Check annotation. If there is no check program, then the annotation itself It doesn't make any sense, just like a comment.
1. Most of the time, we only use annotations, not custom annotations.
2. Who are annotations mainly for? 1. Compiler 2. Parsing program
3. Annotation is not part of the program, but something like a label