java annotation

 java用  @interface Annotation{ } 定義一個註解 @Annotation,一個註解是一個類。
@Override,@Deprecated,@SuppressWarnings爲常見的3個註解。
註解至關於一種標記,在程序中加上了註解就等於爲程序加上了某種標記,之後,
JAVAC編譯器,開發工具和其餘程序能夠用反射來了解你的類以及各類元素上有無任何標記,看你有什麼標記,就去幹相應的事。java

      註解@Override用在方法上,當咱們想重寫一個方法時,在方法上加@Override,當咱們方法
的名字出錯時,編譯器就會報錯,如圖:數組


       註解@Deprecated,用來表示某個類的屬性或方法已通過時,不想別人再用時,在屬性和方法
上用@Deprecated修飾,如圖:框架

 

  註解@SuppressWarnings用來壓制程序中出來的警告,好比在沒有用泛型或是方法已通過時的時候,
 如圖:ide

  

註解@Retention能夠用來修飾註解,是註解的註解,稱爲元註解。
Retention註解有一個屬性value,是RetentionPolicy類型的,Enum RetentionPolicy是一個枚舉類型,
這個枚舉決定了Retention註解應該如何去保持,也可理解爲Rentention 搭配 RententionPolicy使用。RetentionPolicy有3個值:CLASS  RUNTIME   SOURCE
用@Retention(RetentionPolicy.CLASS)修飾的註解,表示註解的信息被保留在class文件(字節碼文件)中當程序編譯時,但不會被虛擬機讀取在運行的時候;
用@Retention(RetentionPolicy.SOURCE )修飾的註解,表示註解的信息會被編譯器拋棄,不會留在class文件中,註解的信息只會留在源文件中;
用@Retention(RetentionPolicy.RUNTIME )修飾的註解,表示註解的信息被保留在class文件(字節碼文件)中當程序編譯時,會被虛擬機保留在運行時,
因此他們能夠用反射的方式讀取。RetentionPolicy.RUNTIME 能夠讓你從JVM中讀取Annotation註解的信息,以便在分析程序的時候使用.工具

 

[java]  view plain
    1. package com.self;  
    2. import java.lang.annotation.Retention;  
    3. import java.lang.annotation.RetentionPolicy;  
    4.   
    5. @Retention(RetentionPolicy.RUNTIME)  
    6. public @interface MyTarget  
    7. { }  
    8. 定義個一註解@MyTarget,用RetentionPolicy.RUNTIME修飾;  
    9. package com.self;  
    10. import java.lang.reflect.Method;  
    11. public class MyTargetTest  
    12. {  
    13.  @MyTarget  
    14.  public void doSomething()  
    15.  {  
    16.   System.out.println("hello world");  
    17.  }  
    18.    
    19.  public static void main(String[] args) throws Exception  
    20.  {  
    21.   Method method = MyTargetTest.class.getMethod("doSomething",null);  
    22.   if(method.isAnnotationPresent(MyTarget.class))//若是doSomething方法上存在註解@MyTarget,則爲true  
    23.   {  
    24.    System.out.println(method.getAnnotation(MyTarget.class));  
    25.   }  
    26.   }  
    27. }  
    28. 上面程序打印:@com.self.MyTarget(),若是RetentionPolicy值不爲RUNTIME,則不打印。  
    29.   
    30.  @Retention(RetentionPolicy.SOURCE )  
    31. public @interface Override  
    32.   
    33. @Retention(RetentionPolicy.SOURCE )  
    34. public @interface SuppressWarnings  
    35.   
    36. @Retention(RetentionPolicy.RUNTIME )  
    37. public @interface Deprecated  
    38. 由上能夠看出,只有註解@Deprecated在運行時能夠被JVM讀取到  
    39.   
    40. 註解中能夠定義屬性,看例子:  
    41. @Retention(RetentionPolicy.RUNTIME)  
    42. public @interface MyAnnotation  
    43. {  
    44.  String hello() default "gege";  
    45.   String world();  
    46.   int[] array() default { 2, 4, 5, 6 };  
    47.   EnumTest.TrafficLamp lamp() ;  
    48.   TestAnnotation lannotation() default @TestAnnotation(value = "ddd");  
    49.   Class style() default String.class;  
    50. }  
    51. 上面程序中,定義一個註解@MyAnnotation,定義了6個屬性,他們的名字爲:  
    52. hello,world,array,lamp,lannotation,style.  
    53. 屬性hello類型爲String,默認值爲gege  
    54. 屬性world類型爲String,沒有默認值  
    55. 屬性array類型爲數組,默認值爲2,4,5,6  
    56. 屬性lamp類型爲一個枚舉,沒有默認值  
    57. 屬性lannotation類型爲註解,默認值爲@TestAnnotation,註解裏的屬性是註解  
    58. 屬性style類型爲Class,默認值爲String類型的Class類型  
    59.   
    60. 看下面例子:定義了一個MyTest類,用註解@MyAnnotation修飾,註解@MyAnnotation定義的屬性都賦了值  
    61. @MyAnnotation(hello = "beijing", world="shanghai",array={},lamp=TrafficLamp.RED,style=int.class)  
    62. public class MyTest  
    63. {  
    64.  @MyAnnotation(lannotation=@TestAnnotation(value="baby"), world = "shanghai",array={1,2,3},lamp=TrafficLamp.YELLOW)  
    65.  @Deprecated  
    66.  @SuppressWarnings("")  
    67.  public void output()  
    68.  {  
    69.   System.out.println("output something!");  
    70.  }  
    71. }  
    72.  接着經過反射讀取註解的信息:  
    73. public class MyReflection  
    74. {  
    75.  public static void main(String[] args) throws Exception  
    76.  {  
    77.   MyTest myTest = new MyTest();  
    78.     Class<MyTest> c = MyTest.class;  
    79.     Method method = c.getMethod("output", new Class[] {});  
    80.        //若是MyTest類名上有註解@MyAnnotation修飾,則爲true  
    81.   if(MyTest.class.isAnnotationPresent(MyAnnotation.class))  
    82.   {  
    83.    System.out.println("have annotation");  
    84.   }  
    85.    if (method.isAnnotationPresent(MyAnnotation.class))  
    86.    {  
    87.    method.invoke(myTest, null); //調用output方法  
    88.    //獲取方法上註解@MyAnnotation的信息  
    89.      MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);  
    90.     String hello = myAnnotation.hello();  
    91.    String world = myAnnotation.world();  
    92.    System.out.println(hello + ", " + world);//打印屬性hello和world的值  
    93.    System.out.println(myAnnotation.array().length);//打印屬性array數組的長度  
    94.    System.out.println(myAnnotation.lannotation().value()); //打印屬性lannotation的值  
    95.    System.out.println(myAnnotation.style());  
    96.    }  
    97.     //獲得output方法上的全部註解,固然是被RetentionPolicy.RUNTIME修飾的  
    98.      Annotation[] annotations = method.getAnnotations();  
    99.       for (Annotation annotation : annotations)  
    100.   {  
    101.    System.out.println(annotation.annotationType().getName());  
    102.   }  
    103.    }  
    104. }  
    105. 上面程序打印:  
    106. have annotation  
    107. output something!  
    108. gege, shanghai  
    109. 3  
    110. baby  
    111. class java.lang.String  
    112. com.heima.annotation.MyAnnotation  
    113. java.lang.Deprecated  
    114.   
    115. 若是註解中有一個屬性名字叫value,則在應用時能夠省略屬性名字不寫。  
    116. 可見,@Retention(RetentionPolicy.RUNTIME )註解中,RetentionPolicy.RUNTIME是註解屬性值,屬性名字是value,  
    117. 屬性的返回類型是RetentionPolicy,以下:  
    118. public @interface MyTarget  
    119. {  
    120.     String value();  
    121. }  
    122. 能夠這樣用:  
    123.   @MyTarget("aaa")  
    124.  public void doSomething()  
    125.  {  
    126.   System.out.println("hello world");  
    127.  }  
    128.    
    129. 註解@Target也是用來修飾註解的元註解,它有一個屬性ElementType也是枚舉類型,  
    130. 值爲:ANNOTATION_TYPE CONSTRUCTOR  FIELD LOCAL_VARIABLE METHOD PACKAGE PARAMETER TYPE  
    131. @Target(ElementType.METHOD) 修飾的註解表示該註解只能用來修飾在方法上。  
    132. @Target(ElementType.METHOD)  
    133. @Retention(RetentionPolicy.RUNTIME)  
    134. public @interface MyTarget  
    135. {  
    136.  String value() default "hahaha";  
    137. }  
    138. 如把@MyTarget修飾在類上,則程序報錯,如:  
    139. @MyTarget  
    140. public class MyTargetTest  
    141. 註解大都用在開發框架中吧,好了有關注解就學習那麼多了,謝謝。  
相關文章
相關標籤/搜索