Java注解:JDK的元注解(Meta Annotation)


Java 除了提供 3 个基本的Annotation之外,还提供了4个元注解,就是只能用于修饰注解的注解

  1. @Retentoin
  2. @Target
  3. @Documented
  4. @Inherited

1、了解和使用@Retention

**@Retention:**只能用于修饰一个Annotation定义,用于指定该Annotation可以保留多长时间

查看**@Retentino**的定义
在这里插入图片描述
这里从枚举类RetentionPoblicy的定义来看可选择value:

package java.lang.annotation;

public enum RetentionPolicy {
    
    
    /**
     * Annotations are to be discarded by the compiler.
     * 编译器会丢弃掉这类注解
     */
    SOURCE,

    /**
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     * 这类注解会保存在class文件中,运行时,JVM不再保留这类注释,是默认值
     */
    CLASS,

    /**
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     * @see java.lang.reflect.AnnotatedElement
     *  这类注解会保存在class文件中,运行时,JVM会保留这类注释,可以通过反射获取这类注解
     */
    RUNTIME
}

2、了解和使用@Target

@Target:也是用于修饰一个Annotation定义,指定被修饰的Annotation能用于修饰哪些程序元素。

查看**@Target**的定义
在这里插入图片描述
JDK中ElementType枚举类的定义,查看value的可选值和使用:

public enum ElementType {
    
    
    /** Class, interface (including annotation type), or enum declaration
	 * 可用于修饰类、接口(注解)、或者枚举
	 */
    TYPE,

    /** Field declaration (includes enum constants) 
    * 只能修饰成员变量
    */
    FIELD,

    /** Method declaration 
    * 只能用于修饰方法
    */
    METHOD,

    /** Formal parameter declaration 
    *只能用于修饰参数
    */
    PARAMETER,

    /** Constructor declaration 
	* 只能用于修饰构造器
	*/
    CONSTRUCTOR,

    /** Local variable declaration 
    * 只能用于修饰局部变量
    */
    LOCAL_VARIABLE,

    /** Annotation type declaration 
    * 只能用于修饰注解(Annotation)
    */
    ANNOTATION_TYPE,

    /** Package declaration 
    * 	只能用于修饰包
    */
    PACKAGE,

    /**
     * Type parameter declaration
     *	类型参数申明
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     * 类型的使用
     * @since 1.8
     */
    TYPE_USE
}

3、使用和使用@Documented

@Documented: 用于指定该元素Annotation修饰的类将被javadoc工具提取成文档,如果定义Annotation时使用@Documented修饰,则使用该Annotation修饰的程序元素的API文档中就会包含该注解说明

查看**@Documented**的定义

在这里插入图片描述

4、了解和使用@Inherited

@Inherited:指定被他修饰的Annotation将具有继承性,如果某个类使用了被@Inherited修饰的注解A,其子类将自动具有A注解

在这里插入图片描述
示例:
父类(超类):

package learn.demo.annotations;

@InheritedTest(value = "Test")
public class Human {
    
    
   // ...
}

子类:

package learn.demo.annotations;

public class Worker extends Human{
    
    
  // ...  
}

	/**
     * 测试 @Inherited注解
     * */
    @Test
    public void TestInherited(){
    
    
        Annotation[] humanAns = Human.class.getAnnotations();
        System.out.println("Human类使用的注解");
        for (Annotation an: humanAns){
    
    
            System.out.println(an);
        }
        System.out.println("Worker类使用的注解");
        Annotation[] workerAns = Worker.class.getAnnotations();
        for (Annotation an: workerAns){
    
    
            System.out.println(an);
        }
    }

执行结果:说明子类继承父类的@InheritedTes注解
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Hicodden/article/details/110602417