【Java SE】Annotation

1# Annotation

Provides library support for the Java programming language annotation facility.

2# Key Class

2.1# java.lang

  • @Override
    Indicates that a method declaration is intended to override a method declaration in a supertype.
  • @Deprecated
    A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists.
    values:deprecation,unchecked妥色,fallthrough盏阶,path凉袱,serial矾兜,finally纷铣,all
  • @SuppressWarnings
    Indicates that the named compiler warnings should be suppressed in the annotated element (and in all program elements contained in the annotated element).
  • @SafeVarargs (since 1.7)
    A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter.
  • @FunctionalInterface (since 1.8)
    An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification.

2.2# java.lang.Annotation | Annotation Types

  • @Target
    Indicates the contexts in which an annotation type is applicable.
  • @Relention
    Indicates how long annotations with the annotated type are to be retained.
  • @Documented
    Indicates that annotations with a type are to be documented by javadoc and similar tools by default.
  • @Inherited
    Indicates that an annotation type is automatically inherited.
  • @Native (since 1.8)
    Indicates that a field defining a constant value may be referenced from native code.
  • @Repeatable (since 1.8)
    The annotation type java.lang.annotation.Repeatable is used to indicate that the annotation type whose declaration it (meta-)annotates is repeatable.

2.2# java.lang.Annotation | Enum

  • ElementType
    • TYPE:Class, interface (including annotation type), or enum declaration
    • FIELD:Field declaration (includes enum constants)
    • METHOD:Method declaration
    • PARAMETER:Formal parameter declaration
    • CONSTRUCTOR:Constructor declaration
    • LOCAL_VARIABLE:Local variable declaration
    • ANNOTATION_TYPE:Annotation type declaration
    • PACKAGE:Package declaration
    • TYPE_PARAMETER:Type parameter declaration
    • TYPE_USE:Use of a type
  • RetentionPolicy
    表示需要在什么級(jí)別保存該注釋信息设凹,用于描述注解的生命周期(即:被描述的注解在什么范圍內(nèi)有效)
    • SOURCE:Annotations are to be discarded by the compiler.在源文件中有效(即源文件保留)
    • CLASS:Annotations are to be recorded in the class file by the compiler but need not be retained by the VM at run time.在class文件中有效(即class保留)
    • RUNTIME: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.在運(yùn)行時(shí)有效(即運(yùn)行時(shí)保留)

3# Common Annotation Class

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
    ElementType[] value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    RetentionPolicy value();
}
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}

4# Annotation Elements Returns

  • int,float,boolean,byte,double,char,long,short
  • String
  • Class
  • enum
  • Annotation
  • Array type all of above
    PS. You can use 'default ' to statement the default value the element returns

5# Custom Annotation

5.1# Annotation Class

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Robot {
    public enum RobotSize {BIG, MED, MIN;}
    int robotcount() default 1;
    String robotName() default "ET";
    RobotSize robotSize() default RobotSize.MED;
}

5.2# applied to a method

@Robot(robotcount = 5, robotName = "XMAN", robotSize = Robot.RobotSize.BIG)
    public void buildRobot() {}

5.3# get the annotation info by reflection

public class RobotBuilder {
    public static void main(String[] args) {
        getAnnotationInfo(RobotBuilder.class);
    }

    private static void getAnnotationInfo(Class<RobotBuilder> robotBuilderClass) {
        Method[] methods = robotBuilderClass.getDeclaredMethods();
        for (Method method : methods) {
            Robot annotation = method.getAnnotation(Robot.class);
            if (annotation != null) {
                int robotcount = annotation.robotcount();
                String robotName = annotation.robotName();
                Robot.RobotSize robotSize = annotation.robotSize();
                System.out.println(">>>robotcount:"+robotcount);
                System.out.println(">>>robotName:"+robotName);
                System.out.println(">>>robotSize:"+robotSize);
            }
        }
    }

    @Robot(robotcount = 5, robotName = "XMAN", robotSize = Robot.RobotSize.BIG)
    public void buildRobot() {}
}

output

>>>robotcount:5
>>>robotName:XMAN
>>>robotSize:BIG
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末碱工,一起剝皮案震驚了整個(gè)濱河市沼琉,隨后出現(xiàn)的幾起案子北苟,更是在濱河造成了極大的恐慌,老刑警劉巖打瘪,帶你破解...
    沈念sama閱讀 217,542評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件友鼻,死亡現(xiàn)場(chǎng)離奇詭異傻昙,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)彩扔,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,822評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門妆档,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人虫碉,你說(shuō)我怎么就攤上這事贾惦。” “怎么了敦捧?”我有些...
    開(kāi)封第一講書人閱讀 163,912評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵须板,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我兢卵,道長(zhǎng)习瑰,這世上最難降的妖魔是什么? 我笑而不...
    開(kāi)封第一講書人閱讀 58,449評(píng)論 1 293
  • 正文 為了忘掉前任济蝉,我火速辦了婚禮杰刽,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘王滤。我一直安慰自己贺嫂,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,500評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布雁乡。 她就那樣靜靜地躺著第喳,像睡著了一般。 火紅的嫁衣襯著肌膚如雪踱稍。 梳的紋絲不亂的頭發(fā)上曲饱,一...
    開(kāi)封第一講書人閱讀 51,370評(píng)論 1 302
  • 那天,我揣著相機(jī)與錄音珠月,去河邊找鬼扩淀。 笑死,一個(gè)胖子當(dāng)著我的面吹牛啤挎,可吹牛的內(nèi)容都是我干的驻谆。 我是一名探鬼主播,決...
    沈念sama閱讀 40,193評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼庆聘,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼胜臊!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起伙判,我...
    開(kāi)封第一講書人閱讀 39,074評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤象对,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后宴抚,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體勒魔,經(jīng)...
    沈念sama閱讀 45,505評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡甫煞,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,722評(píng)論 3 335
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了沥邻。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片危虱。...
    茶點(diǎn)故事閱讀 39,841評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖唐全,靈堂內(nèi)的尸體忽然破棺而出埃跷,到底是詐尸還是另有隱情,我是刑警寧澤邮利,帶...
    沈念sama閱讀 35,569評(píng)論 5 345
  • 正文 年R本政府宣布弥雹,位于F島的核電站,受9級(jí)特大地震影響延届,放射性物質(zhì)發(fā)生泄漏剪勿。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,168評(píng)論 3 328
  • 文/蒙蒙 一方庭、第九天 我趴在偏房一處隱蔽的房頂上張望厕吉。 院中可真熱鬧,春花似錦械念、人聲如沸头朱。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 31,783評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)项钮。三九已至,卻和暖如春希停,著一層夾襖步出監(jiān)牢的瞬間烁巫,已是汗流浹背。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 32,918評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工宠能, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留亚隙,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,962評(píng)論 2 370
  • 正文 我出身青樓违崇,卻偏偏與公主長(zhǎng)得像阿弃,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子亦歉,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,781評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容