java元註解

來源:互聯網
上載者:User

標籤:工具   retention   main   元註解   run   註解   讀取   範圍   catch   

http://www.jb51.net/article/55371.htmhttp://www.cnblogs.com/peida/archive/2013/04/24/3036689.html Retention註解 Retention(保留)註解說明,這種類型的註解會被保留到那個階段. 有三個值:1.RetentionPolicy.SOURCE —— 這種類型的Annotations只在原始碼層級保留,編譯時間就會被忽略2.RetentionPolicy.CLASS —— 這種類型的Annotations編譯時間被保留,在class檔案中存在,但JVM將會忽略3.RetentionPolicy.RUNTIME —— 這種類型的Annotations將被JVM保留,所以他們能在運行時被JVM或其他使用反射機制的代碼所讀取和使用. Java註解的樣本1:代碼如下: @Retention(RetentionPolicy.RUNTIME) public @interface Test_Retention {    String doTestRetention(); } 在這個樣本中, @Retention(RetentionPolicy.RUNTIME)註解表明 Test_Retention註解將會由虛擬機器保留,以便它可以在運行時通過反射讀取. Documented 註解 Documented 註解表明這個註解應該被 javadoc工具記錄. 預設情況下,javadoc是不包括註解的. 但如果聲明註解時指定了 @Documented,則它會被 javadoc 之類的工具處理, 所以註解類型資訊也會被包括在產生的文檔中. 樣本6進一步示範了使用 @Documented: Java註解的樣本2:  複製代碼 代碼如下:   @Documented public @interface Test_Documented {    String doTestDocument(); }接下來,像下面這樣修改TestAnnotations類:代碼如下:public class TestAnnotations {    public static void main(String arg[]) {       new TestAnnotations().doSomeTestRetention();       new TestAnnotations().doSomeTestDocumented();    }    @Test_Retention (doTestRetention="保留註解資訊測試")    public void doSomeTestRetention() {       System.out.printf("測試註解類型 ‘Retention‘");    }    @Test_Documented(doTestDocument="Hello document")    public void doSomeTestDocumented() {       System.out.printf("測試註解類型 ‘Documented‘");    } } 現在,如果你使用 javadoc命令產生 TestAnnotations.html檔案從可以看到,文檔中沒有 doSomeTestRetention() 方法的 annotation-type資訊()方法. 但是, doSomeTestDocumented() 方法的文檔提供了註解的描述資訊. 這是因為 @Documented標籤被加到了Test_Documented註解上. 之前的註解Test_Retention並沒有指定 @Documented 標記(tag). http://blog.csdn.net/snow_crazy/article/details/39381695Inherited作用是,使用此註解聲明出來的自訂註解,在使用此自訂註解時,如果註解在類上面時,子類會自動繼承此註解,否則的話,子類不會繼承此註解。這裡一定要記住,使用Inherited聲明出來的註解,只有在類上使用時才會有效,對方法,屬性等其他無效。  http://www.cnblogs.com/peida/archive/2013/04/24/3036689.html

@Target:

   @Target說明了Annotation所修飾的物件範圍:Annotation可被用於 packages、types(類、介面、枚舉、Annotation類型)、類型成員(方法、構造方法、成員變數、枚舉值)、方法參數和本地變數(如迴圈變數、catch參數)。在Annotation類型的聲明中使用了target可更加明晰其修飾的目標。

   作用:用於描述註解的使用範圍(即:被描述的註解可以用在什麼地方)

java元註解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.