Java Learning Road-Annotation
Overview
What is Annotation? Annotation is a special "comment" placed before the classes, methods, fields, and parameters of the Java source code.
Comments will be directly ignored by the compiler, and comments can be packaged into class files by the compiler. Therefore, comments are a kind of "metadata" used as annotations.
Like what we always add when we rewrite the parent class method, @Override
it is also a kind of annotation in terms of form, but in essence this annotation does nothing.
There are three types of annotations in Java programs:
- Annotations used by the compiler;
.class
Notes used by the tool to process files;- Comments that can be read during program runtime.
When we define an annotation, we can also define configuration parameters. Configuration parameters can include: basic types, String, Class, and enumerated arrays .
The entire usage process of annotations mainly includes three parts:
1. Meta annotation
There are some annotations in Java that can modify other annotations. These annotations are called meta annotations.
The Java standard library has defined some meta-annotations, we only need to use meta-annotations, and usually do not need to write meta-annotations ourselves.
@Target
The most commonly used meta-annotations are @Target
. Use @Target
can define where the Annotation
source code can be applied:
- Class or interface
ElementType.TYPE
:; - Field:
ElementType.FIELD
; - Method
ElementType.METHOD
:; - Construction method
ElementType.CONSTRUCTOR
:; - Method
ElementType.PARAMETER
parameters: . - If no parameters are passed, it can be used everywhere.
@Retention
The life cycle @Retention
defined by meta annotations Annotation
:
- Only
RetentionPolicy.SOURCE
compile: ; - Only .class
RetentionPolicy.CLASS
file: ; - Runtime:
RetentionPolicy.RUNTIME
.
If it @Retention
does not exist, the Annotation
default is CLASS
.
Under normal circumstances, our annotations need to be processed by reflection, so we usually add them RetentionPolicy.RUNTIME
.
@Repeatable
@Repeatable
Meta-annotation can define Annotation
whether it can be repeated.
@Inherited
@Inherited
The meta annotation defines whether the subclass can inherit the definition of the parent class Annotation
.
@Inherited
Only valid for @Target(ElementType.TYPE)
type annotation
, and only for class
inheritance, for interface
inheritance is invalid.
Two, definition and annotation
There are three steps to completely customize an annotation:
1. @interface
Annotate with definitions
@interface MyAnnotation {
}
2. Add parameters and default values
@interface MyAnnotation {
int min() default 0;
int max() default 255;
}
The default value is not necessary, but adding the default value is a good habit, which can save us a lot of unnecessary trouble.
3. Configure annotations using meta annotations
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
int min() default 0;
int max() default 255;
}
Annotation attributes
- Eight basic data types;
- String;
- Enumeration class
- Class;
- Annotation type;
- One-dimensional array of the above type.
value
Properties: If the property is only one annotation, and called value
, then use the annotation can not specify the property name, because the default is to value
carry out the assignment; if there are multiple annotation attributes, you must specify the correspondence between the properties.
Array: If there is only one array in the annotation attribute, it can be omitted {}
.
Three, processing annotations
In Java programs, we generally use reflection to process annotations.
import java.lang.annotation.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Demo {
public static void main(String[] args) throws NoSuchFieldException, NoSuchMethodException {
// 获取对类的注解
Class<MyClass> clazz = MyClass.class;
MyAnnotation annotationClass = clazz.getAnnotation(MyAnnotation.class);
System.out.println(annotationClass);
// 获取对类属性的注解
Field count = clazz.getField("count");
MyAnnotation annotationFiled = count.getAnnotation(MyAnnotation.class);
System.out.println(annotationFiled);
// 获取对方法的注解
Method test = clazz.getMethod("test");
MyAnnotation annotationMethod = test.getAnnotation(MyAnnotation.class);
System.out.println(annotationMethod);
}
}
@MyAnnotation(min = 1, max = 100)
class MyClass {
@MyAnnotation(min = 5, max = 150)
public int count=100;
@MyAnnotation(min = 10, max = 200)
public void test() {
}
}
// 自定义注解
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
int min() default 0;
int max() default 255;
}