[Java] Explicación práctica de la metaanotación de @interface @Retention, @Target, @Documented, @Inherited, @Repeatable

Tabla de contenido

El significado de las metaanotaciones

@Retención

@Objetivo

@Documentado

@Heredado

@Repetible


@Retención @Objetivo @Documentado @Heredado @Repetible


El significado de las metaanotaciones

        Se utiliza para describir la ubicación, el alcance, la herencia y la extracción de documentos de las anotaciones.


@Retención

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

//  @Retention(RetentionPolicy.SOURCE) //  MyAnno注解在源代码中有效
//  @Retention(RetentionPolicy.CLASS) //  MyAnno注解在类文件中有效
@Retention(RetentionPolicy.RUNTIME) //   MyAnno注解在运行时有效

public @interface MyAnno {
    public String value() default "";
}
@MyAnno("我是一个注解")
public class AnnoTest {
    public static void main(String[] args) {
        AnnoTest  at= new AnnoTest();
        MyAnno annotate = at.getClass().getAnnotation(MyAnno.class);
        System.out.println(annotate.value());
    }
}

输出:我是一个注解

@Objetivo

Indica en qué elementos se puede utilizar esta anotación.

ElementType.ANNOTATION_TYPE: Anotación

ElementType.CONSTRUCTOR: Constructor

ElementType.FIELD: campos, constantes de enumeración

ElementType.LOCAL_VARIABLE: variable local

ElementType.METHOD: Método

ElementType.PACKAGE: 包

ElementType.PARAMETER: parámetro formal

ElementType.TYPE: Clase, interfaz, anotación, enumeración

ElementType.TYPE_PARAMETER: parámetro de tipo

ElementType.TYPE_USE: Uso de tipo 

@Target({ElementType.METHOD,ElementType.FIELD})
public @interface MyAnno {
    public String value() default "";
}

@Documentado

La información de anotaciones se puede extraer en la documentación de la API mediante la herramienta javadoc.

@Documented
public @interface MyAnno {
    public String value() default "";
}

Ruta IDE: Herramientas - Generar JavaDoc


@Heredado

Las anotaciones modificadas se heredan.

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
 
@Inherited
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
	public String value() default "";
}
@MyAnnotation("父亲")
public class Father {}
public class Child extends Father{
	public static void main(String[] args) {
		Class<Child> child=Child.class;
		MyAnnotation anno = child.getAnnotation(MyAnnotation.class);
		System.out.println(anno.value());
	}
}
输出:父亲

@Repetible

La anotación declarada se puede anotar repetidamente en el elemento correspondiente, y esta anotación repetible se puede incluir mediante el valor de otra anotación.

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME) //
@Target(ElementType.TYPE)
public @interface MyAnnos {
    MyAnno[] value() ;
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Repeatable(MyAnnos.class)
public @interface MyAnno {
    String value() default "";
}
import com.prv.anno.MyAnno;

@MyAnno("anno")
@MyAnno("lala")
public class AnnoTest {
    public static void main(String[] args) {
        MyAnno[] fileTypes = new AnnoTest().getClass().getAnnotationsByType(MyAnno.class);
        for (MyAnno myAnno : fileTypes) {
            System.out.println(myAnno.value());
        }
    }
}
输出:
      anno
      lala

Supongo que te gusta

Origin blog.csdn.net/xudahai513/article/details/126898673
Recomendado
Clasificación