Java註解:@Retention和@Target註解的說明以及使用方法

本文目錄

一、註解說明

二、@Target註解

三、@Retention註解

四、自定義註解


一、註解說明

註解(也被稱爲元數據)爲我們在代碼中添加信息提供了一種形式化的方法,使我們可以在稍後某個時刻非常方便地使用這些數據。

定義註解時,會需要一些元註解(meta--annotation),如@Target@ Retention,這兩個特性是我們必須要定義清楚的,一個是Target(註解目標),另一個就是Retention(註解生命週期,也叫聲明週期)。

@Target用來定義你的註解將應用於什麼地方(例如是一個方法或者一個域)。

@ Retention用來定義該註解在哪一個級別可用,在源代碼中(SOURCE)類文件中(CLASS)或者運行時(RUNTIME)

在註解中,一般都會包含一些元素以表示某些值。當分析處理註解時,程序或工具可以利用這些值。註解的元素看起來就像接口的方法,唯一的區別是你可以爲其指定默認值。

沒有元素的註解稱爲標記註解(marker annotation),例如我在文章尾部自定義的註解。

Java目前只內置了三種標準註解(下一篇文章介紹),以及四種元註解。元註解專職負責註解其他的註解。

二、@Target註解

下面是註解@Target的源碼,這裏要一個ElementType[]數組。

 /**
  * @since 1.5
  * @jls 9.6.4.1 @Target
  * @jls 9.7.4 Where Annotations May Appear
  */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}

而ElementType是枚舉類型的,一共有10種取值類型,分別是(TYPE, FIELD, METHOD, PARAMETER, CONSTRUCTOR, LOCAL_VARIABLE, ANNOTATION_TYPE, PACKAGE, TYPE_PARAMETER, TYPE_USE)以下是ElementType的源碼:

 /**
  * @author  Joshua Bloch
  * @since 1.5
  * @jls 9.6.4.1 @Target
  * @jls 4.1 The Kinds of Types and Values
  */
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_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE
}

三、@Retention註解

如下是@Retention註解的源碼信息,它要求一個RetentionPolicy類型的取值。我們再看看RetentionPolicy是個什麼類型對象。

/** 
 * @author  Joshua Bloch
 * @since 1.5
 * @jls 9.6.3.2 @Retention
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

點擊進入RetentionPolicy對象之後,發現它是一個枚舉類型的,有三種取值(SOURCE, CLASS, RUNTIME),以下是RetentionPolicy枚舉源碼:

/**
 * @author  Joshua Bloch
 * @since 1.5
 */
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,

    /**
     * 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
     */
    RUNTIME
}

四、自定義註解

看完源碼之後,發現自定義註解其實也不難,比葫蘆畫瓢,我們也自定義註解試一下。

@Target({ElementType.METHOD, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.CLASS)
public @interface TestAnnotation {

}

定義完之後,就可以使用自己定義的註解了,例如:

@TestAnnotation
public class Sort {

    public static void main(String[] args) {
        System.out.println("自定義註解使用中.....");
    }

}

 

【參考資料】

Thinking in Java(Fouth Edition) ---- Java編程思想(第四版):第20章 註解

Java註解:三種標準註解和四種元註解以及註解的元素:https://blog.csdn.net/weixin_44299027/article/details/105920418

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章