Java 注解

JAVA注解

Java 自帶注解(系統(tǒng)注解)

  • @Override

表示重寫注解

  • @Deprecated

表示過時(shí)的方法

  • @Suppvisewarnings

忽略某些警告

注解的分類

按照運(yùn)行機(jī)制分類:

  • 源碼注解

    注解只是在源碼時(shí)存在,編譯成class時(shí)就不存在了。

  • 運(yùn)行時(shí)注解

    在運(yùn)行時(shí)注解仍然存在并且可能會影響程序邏輯。

  • 編譯時(shí)注解

    在源碼和class文件中都存在。

元注解:

元注解的作用就是負(fù)責(zé)注解其他注解。Java5.0定義了4個(gè)標(biāo)準(zhǔn)的meta-annotation類型,它們被用來提供對其它 annotation類型作說明。Java5.0定義的元注解:
   1.@Target
   2.@Retention
   3.@Documented
   4.@Inherited
  這些類型和它們所支持的類在java.lang.annotation包中可以找到。

  • @Target:

? @Target說明了Annotation所修飾的對象范圍:Annotation可被用于 packages、types(類、接口、枚舉、Annotation類型)、類型成員(方法、構(gòu)造方法、成員變量、枚舉值)、方法參數(shù)和本地變量(如循環(huán)變量、catch參數(shù))。在Annotation類型的聲明中使用了target可更加明晰其修飾的目標(biāo)。

? 作用:用于描述注解的使用范圍(即:被描述的注解可以用在什么地方)

取值(ElementType)有:

1.CONSTRUCTOR:用于描述構(gòu)造器
    2.FIELD:用于描述域
    3.LOCAL_VARIABLE:用于描述局部變量
    4.METHOD:用于描述方法
    5.PACKAGE:用于描述包
    6.PARAMETER:用于描述參數(shù)
    7.TYPE:用于描述類、接口(包括注解類型) 或enum聲明

  • @Retention:

@Retention定義了該Annotation被保留的時(shí)間長短:某些Annotation僅出現(xiàn)在源代碼中,而被編譯器丟棄;而另一些卻被編譯在class文件中;編譯在class文件中的Annotation可能會被虛擬機(jī)忽略,而另一些在class被裝載時(shí)將被讀?。ㄕ堊⒁獠⒉挥绊慶lass的執(zhí)行,因?yàn)锳nnotation與class在使用上是被分離的)。使用這個(gè)meta-Annotation可以對 Annotation的“生命周期”限制。

作用:表示需要在什么級別保存該注釋信息,用于描述注解的生命周期(即:被描述的注解在什么范圍內(nèi)有效)

取值(RetentionPoicy)有:

1.SOURCE:在源文件中有效(即源文件保留)
    2.CLASS:在class文件中有效(即class保留)
    3.RUNTIME:在運(yùn)行時(shí)有效(即運(yùn)行時(shí)保留)

  • @Documented:

? @Documented用于描述其它類型的annotation應(yīng)該被作為被標(biāo)注的程序成員的公共API,因此可以被例如javadoc此類的工具文檔化。Documented是一個(gè)標(biāo)記注解,沒有成員。

  • @Inherited:

? @Inherited 元注解是一個(gè)標(biāo)記注解,@Inherited闡述了某個(gè)被標(biāo)注的類型是被繼承的。如果一個(gè)使用了@Inherited修飾的annotation類型被用于一個(gè)class,則這個(gè)annotation將被用于該class的子類。

注意:@Inherited annotation類型是被標(biāo)注過的class的子類所繼承。類并不從它所實(shí)現(xiàn)的接口繼承annotation,方法并不從它所重載的方法繼承annotation。

當(dāng)@Inherited annotation類型標(biāo)注的annotation的Retention是RetentionPolicy.RUNTIME,則反射API增強(qiáng)了這種繼承性。如果我們使用java.lang.reflect去查詢一個(gè)@Inherited annotation類型的annotation時(shí),反射代碼檢查將展開工作:檢查class和其父類,直到發(fā)現(xiàn)指定的annotation類型被發(fā)現(xiàn),或者到達(dá)類繼承結(jié)構(gòu)的頂層。

順便舉個(gè)栗子

如何從任意一個(gè)對象中獲取想要的值?

有這樣一個(gè)注解聲明

package com.company;

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

/**
 * Created by zh on 16/6/5.
 */

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)

public @interface Banner {
    String value();
}

有這樣一個(gè)業(yè)務(wù)類

package com.company;

/**
 * Created by zh on 16/6/5.
 */
public class Item {
    @Banner("url")
    public String url;
    @Banner("title")
    public String title;

    public String other;
}

獲取Item對象指定值

package com.company;

import java.lang.reflect.Field;

public class Main {

    public static void main(String[] args) {
        // write your code here
        Item item = new Item();
        item.url = "http://www.mostring.com";
        item.title = "Hallo";
        printBanner(item);
        Bn b = new Bn("test","http://ola.so");
        printBanner(b);

    }

    public static void printBanner(Object object) {
        Class cls = object.getClass();
        Field[] fields = cls.getDeclaredFields();
        for (Field f : fields) {
            boolean isBannerAt = f.isAnnotationPresent(Banner.class);
            if (!isBannerAt) {
                continue;
            }
            Banner b = f.getAnnotation(Banner.class);
            f.setAccessible(true);
            try {
                Object vla = f.get(object);
                System.out.println(b.value() + "---" + vla);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容