Java Learning Road-Annotation

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, @Overrideit 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;
  • .classNotes 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 @Targetcan define where the Annotationsource code can be applied:

  • Class or interface ElementType.TYPE:;
  • Field:ElementType.FIELD ;
  • Method ElementType.METHOD:;
  • Construction method ElementType.CONSTRUCTOR:;
  • Method ElementType.PARAMETERparameters: .
  • If no parameters are passed, it can be used everywhere.

@Retention

The life cycle @Retentiondefined by meta annotations Annotation:

  • Only RetentionPolicy.SOURCEcompile: ;
  • Only .class RetentionPolicy.CLASSfile: ;
  • Runtime:RetentionPolicy.RUNTIME .

If it @Retentiondoes not exist, the Annotationdefault is CLASS.

Under normal circumstances, our annotations need to be processed by reflection, so we usually add them RetentionPolicy.RUNTIME.

@Repeatable

@RepeatableMeta-annotation can define Annotationwhether it can be repeated.

@Inherited

@InheritedThe meta annotation defines whether the subclass can inherit the definition of the parent class Annotation.

@InheritedOnly valid for @Target(ElementType.TYPE)type annotation, and only for classinheritance, for interfaceinheritance is invalid.

Two, definition and annotation

There are three steps to completely customize an annotation:

1. @interfaceAnnotate 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.

valueProperties: 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 valuecarry 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;
}

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/112729298