What is the use of Java annotations?

Introduction to Annotations

Annotations can be added to the class, member variables, methods, method parameters.

Annotations are used to complete functions, and each annotation can complete some functions.

Common notes

@Override checks whether the method is a method that overrides the parent class

@FunctionalInterface checks whether the interface is a functional interface

The @Deprecated annotation method is outdated and is not recommended

Custom annotation

Note that there is an @ in front of the interface, which is a custom annotation, not an interface.

public @interface Student{
    
    
​	String name();    //属性没有默认值,使用时必须指定值。int age() default 0 ;  //有默认值,使用时,可以不给,也可以给。
​	String[] hobbies();  //数组格式,按照数组格式赋值,只有一个时,可以省略大括号。
}

//这样使用。
@Student(name="小老犇",hobbies={
    
    "放牛","吃草"})
public void abc(){
    
    sout("注解使用")}

There are three data types of attributes in the annotation:

  • Eight basic data types (byte short int long float double boolean char)

  • Class, String, enumeration, annotation

  • Type arrays of all the above types.

Meta annotation

Meta annotations are also annotations

Meta annotations are used to modify the annotations

Meta-annotations should be added to the annotations to limit the annotations

@Target

Restrict the use of annotations.
Insert picture description here
@Target principle

There is only one attribute in @Target called value. This attribute is ElementType[]

ElementType is an enumeration. Enumeration is a special class. Each attribute in the enumeration is its own type. The attributes in the enumeration can be directly called by the enumeration name.
Insert picture description here

@Retention

Limit the life cycle of annotations.

@Retention principle

@Retention has only one attribute called value, this attribute is of RetentionPolicy type, which is also an enumeration type.
Insert picture description here
Insert picture description here
Find a frequently used annotation point to enter and take a look.
Insert picture description here

Parse annotation

Analyze annotations, based on reflection. When parsing annotations, you need to use Class objects, Method objects, and Field objects, and use these objects to get annotations

These objects need to be obtained using reflection.

Get the content (such as attributes) in the annotation and process it.

All annotations of the Annotation interface implement this interface by default

AnnotatedElement interface, which defines many method operation annotations

Related API

getAnnotations() Get all annotations

getAnnotation(Class annotationClass) Get the class of which annotation is passed by the specified annotation, and whose annotation is obtained

boolean isAnnotationPresent(Class annotationClass) Determines whether the specified annotation exists, and the parameter is the annotation Class.

Who calls the above API depends on whether the annotation annotates a class, a method, or a member variable:

If it is a class, call it with the Class object of the instance class. Get the annotations on the entity class.

The method is called with the Method object.

If it is a member variable, use the Field object to call.

Guess you like

Origin blog.csdn.net/numbbe/article/details/109280606