[Java annotations] How to customize annotations and meta-annotations in JDK

1. How to customize annotations

1.1 Format check at compile time (three basic annotations built into JDK)

        @Override: Restricted to override the parent class method, this annotation can only be used for methods 
        @Deprecated: used to indicate that the modified element (class, method, etc.) is outdated. Usually because the decorated structure is dangerous or a better option exists 
        @SuppressWarnings: Suppress compiler warnings

1.2 Custom annotations:

Refer to @SuppressWarnings definition

① The annotation is declared as: @interface

② Internally defined members, usually represented by value

③ The default value of the member can be specified, using the default definition

④ If the custom annotation has no members, it indicates that it is an identification function

 Example: The internal definition member defaults to "hello"

public @interface MyAnnotation {

    String value() default "hello";
}

use:

One: default

@MyAnnotation
class Person {
}

Two: designated members

@MyAnnotation(value = "hi")
class Person {
}
If the annotation has members, you need to specify the value of the member when using the annotation. The 
custom annotation must be accompanied by the information processing flow of the annotation (using reflection) to make sense

Two: Meta annotations in JDK

 2.1 What are meta-annotations

JDK's meta-Annotation is used to modify other Annotation definitions

2.2 JDK5.0 provides 4 standard meta-annotation types

> Retention

> Target

> Documented

> Inherited

2.3 The use of four meta-annotations in JDK 

One: @Retention

@Retention: It can only be used to modify an Annotation definition to specify the life cycle of the Annotation. @Rention contains a member variable of RetentionPolicy type. When using @Rention, you must specify a value for the value member variable:

RetentionPolicy.SOURCE : valid in the source file (that is, the source file is retained), the compiler directly discards the comments of this policy

RetentionPolicy.CLASS: Effective in class files (that is, class retention), when running Java programs, JVM will not retain annotations. It's the default value

RetentionPolicy.RUNTIME: Effective at runtime (that is, reserved at runtime), when running a Java program, the JVM will retain the annotation. The program can obtain this annotation through reflection

Check out the source code below

First turn on SuppressWarnings

You can see Retention: specify the life cycle of the Annotation modified

Let's open the enumeration class RetentionPolicy

 

We can see that there are

SOURCE

CLASS (default behavior)

RUNTIME: Only annotations declared as the RUNTIME statement cycle can be obtained through reflection

three variables

Two: @Target:

It is used to modify the definition of Annotation, and it is used to specify which program elements the modified Annotation can be used to modify. @Target also contains a member variable named value.

We open the enumeration class ElementType to see

 

Add in the custom annotation MyAnnotation

@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})

 and delete CONSTRUCTOR,

 

The test is as follows:

At this point we can find that the MyAnnotation modified constructor will report an error 

Simply put, Target: used to specify which program elements the modified Annotation can be used to modify

Custom annotations will specify two meta-annotations: Retention, Target

The following two are less used 

Three: @Documented:

It is used to specify that the Annotation class modified by the meta-Annotation will be extracted into documentation by the javadoc tool. By default, javadoc does not include annotations.

Annotations defined as Documented must set the Retention value to RUNTIME.

Four: @Inherited

@Inherited: Annotation modified by it will have inheritance. If a class uses Annotation modified by @Inherited, its subclasses will automatically have the annotation.

For example: if a custom annotation marked with @Inherited annotation is marked at the class level, the subclass can inherit the annotation at the parent class class level

In practice, less

How do we prove the inheritance of @Inherited?

We can obtain annotation information through reflection --- we will describe it when we reflect the content

Let's look at an example first:

Annotate MyAnnotation code as follows:

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

import static java.lang.annotation.ElementType.*;
import static java.lang.annotation.ElementType.LOCAL_VARIABLE;

@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE})
public @interface MyAnnotation {

    String value() default "hello";
}

test:

import org.junit.Test;

import java.lang.annotation.Annotation;

public class AnnotationTest {

    @Test
    public void testGetAnntation(){
        Class studentClass = Student.class;
        Annotation[] annotations = studentClass.getAnnotations();
        for (int i = 0; i < annotations.length; i++) {
            System.out.println(annotations[i]);
        }
    }
}

@MyAnnotation(value = "hi")
class Person {
    private String name;
    private int age;

    @SuppressWarnings("unused")
    int i = 0;

    @MyAnnotation
    public Person(){

    }
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void walk() {
        System.out.println("人说话");
    }

    public void eat() {
        System.out.println("人吃饭");
    }

}
interface Info{
    void show();
}
class Student extends Person implements Info{

    @Override
    public void walk() {
        System.out.println("学生走路");
    }

    @Override
    public void show() {

    }
}

The result of the operation is as follows:

If you remove @MyAnnotation(value = "hi") on the parent class Person of Student, the running result is as follows:

From this we can see the inheritance of @Inherited​​​​​​​​

 thanks for watching! ! !

Guess you like

Origin blog.csdn.net/qq_64976935/article/details/129598610