java retention注解

时间:2023-03-08 16:52:42

Retention注解

Retention(保留)注解说明,这种类型的注解会被保留到那个阶段. 有三个值:
1.RetentionPolicy.SOURCE —— 这种类型的Annotations只在源代码级别保留,编译时就会被忽略
2.RetentionPolicy.CLASS —— 这种类型的Annotations编译时被保留,在class文件中存在,但JVM将会忽略
3.RetentionPolicy.RUNTIME —— 这种类型的Annotations将被JVM保留,所以他们能在运行时被JVM或其他使用反射机制的代码所读取和使用.
示例5演示了 RetentionPolicy.RUNTIME 的声明:

Java注解的示例1:

复制代码代码如下:
@Retention(RetentionPolicy.RUNTIME)
public @interface Test_Retention {
   String doTestRetention();
}

在这个示例中, @Retention(RetentionPolicy.RUNTIME)注解表明 Test_Retention注解将会由虚拟机保留,以便它可以在运行时通过反射读取.

结论

本文向你展示了如何通过使用JDK5的注解功能使开发更容易. 注解不直接影响程序的语义. 开发和部署工具可以以某种方式阅读这些注解并处理它们,使用包含注解的程序可以替代额外的Java源文件、XML文档或其他古老的构件.  使用注解可以使用更少的代码完成同样的事情,并且有更好的编译时错误检测机制. 注解的目的是花更少的时间在那些死硬无用的细节中,更多地关注业务逻辑规则.

注解的定义:

注解的定义使用关键词 @interface ,并在上面一行注明@Rentention(arg) 或者@Target(args) , 样例如下:

@Rentention(RetentionPolicy.RUNTIME)

public @interface Annotation01 {

//定义公共的final静态属性

.....

//定以公共的抽象方法

......

}

常见基本内置注解

@Override  当我们想重写一个方法时,在方法上加@Override,当我们方法的名字出错时,编译器就会报错。 定义如下:

@Retention(RetentionPolicy.SOURCE )

Java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类。
@Override,@Deprecated,@SuppressWarnings为常见的3个注解。
注解相当于一种标记,在程序中加上了注解就等于为程序加上了某种标记,以后,
JAVAC编译器,开发工具和其他程序可以用反射来了解你的类以及各种元素上有无任何标记,看你有什么标记,就去干相应的事。

Java用  @interface Annotation{ } 定义一个注解 @Annotation,一个注解是一个类。
@Override,@Deprecated,@SuppressWarnings为常见的3个注解。
注解相当于一种标记,在程序中加上了注解就等于为程序加上了某种标记,以后,
JAVAC编译器,开发工具和其他程序可以用反射来了解你的类以及各种元素上有无任何标记,看你有什么标记,就去干相应的事。

注解@Override用在方法上,当我们想重写一个方法时,在方法上加@Override,当我们方法
的名字出错时,编译器就会报错,如图:

java retention注解
       注解@Deprecated,用来表示某个类的属性或方法已经过时,不想别人再用时,在属性和方法
上用@Deprecated修饰,如图:

java retention注解

注解@SuppressWarnings用来压制程序中出来的警告,比如在没有用泛型或是方法已经过时的时候,
 如图:

java retention注解

注解@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注解的信息,以便在分析程序的时候使用.

    1. package com.self;
    2. import java.lang.annotation.Retention;
    3. import java.lang.annotation.RetentionPolicy;
    4. @Retention(RetentionPolicy.RUNTIME)
    5. public @interface MyTarget
    6. { }
    7. 定义个一注解@MyTarget,用RetentionPolicy.RUNTIME修饰;
    8. package com.self;
    9. import java.lang.reflect.Method;
    10. public class MyTargetTest
    11. {
    12. @MyTarget
    13. public void doSomething()
    14. {
    15. System.out.println("hello world");
    16. }
    17. public static void main(String[] args) throws Exception
    18. {
    19. Method method = MyTargetTest.class.getMethod("doSomething",null);
    20. if(method.isAnnotationPresent(MyTarget.class))//如果doSomething方法上存在注解@MyTarget,则为true
    21. {
    22. System.out.println(method.getAnnotation(MyTarget.class));
    23. }
    24. }
    25. }
    26. 上面程序打印:@com.self.MyTarget(),如果RetentionPolicy值不为RUNTIME,则不打印。
    27. @Retention(RetentionPolicy.SOURCE )
    28. public @interface Override
    29. @Retention(RetentionPolicy.SOURCE )
    30. public @interface SuppressWarnings
    31. @Retention(RetentionPolicy.RUNTIME )
    32. public @interface Deprecated
    33. 由上可以看出,只有注解@Deprecated在运行时可以被JVM读取到
    34. 注解中可以定义属性,看例子:
    35. @Retention(RetentionPolicy.RUNTIME)
    36. public @interface MyAnnotation
    37. {
    38. String hello() default "gege";
    39. String world();
    40. int[] array() default { 2, 4, 5, 6 };
    41. EnumTest.TrafficLamp lamp() ;
    42. TestAnnotation lannotation() default @TestAnnotation(value = "ddd");
    43. Class style() default String.class;
    44. }
    45. 上面程序中,定义一个注解@MyAnnotation,定义了6个属性,他们的名字为:
    46. hello,world,array,lamp,lannotation,style.
    47. 属性hello类型为String,默认值为gege
    48. 属性world类型为String,没有默认值
    49. 属性array类型为数组,默认值为2,4,5,6
    50. 属性lamp类型为一个枚举,没有默认值
    51. 属性lannotation类型为注解,默认值为@TestAnnotation,注解里的属性是注解
    52. 属性style类型为Class,默认值为String类型的Class类型
    53. 看下面例子:定义了一个MyTest类,用注解@MyAnnotation修饰,注解@MyAnnotation定义的属性都赋了值
    54. @MyAnnotation(hello = "beijing", world="shanghai",array={},lamp=TrafficLamp.RED,style=int.class)
    55. public class MyTest
    56. {
    57. @MyAnnotation(lannotation=@TestAnnotation(value="baby"), world = "shanghai",array={1,2,3},lamp=TrafficLamp.YELLOW)
    58. @Deprecated
    59. @SuppressWarnings("")
    60. public void output()
    61. {
    62. System.out.println("output something!");
    63. }
    64. }
    65. 接着通过反射读取注解的信息:
    66. public class MyReflection
    67. {
    68. public static void main(String[] args) throws Exception
    69. {
    70. MyTest myTest = new MyTest();
    71. Class<MyTest> c = MyTest.class;
    72. Method method = c.getMethod("output", new Class[] {});
    73. //如果MyTest类名上有注解@MyAnnotation修饰,则为true
    74. if(MyTest.class.isAnnotationPresent(MyAnnotation.class))
    75. {
    76. System.out.println("have annotation");
    77. }
    78. if (method.isAnnotationPresent(MyAnnotation.class))
    79. {
    80. method.invoke(myTest, null); //调用output方法
    81. //获取方法上注解@MyAnnotation的信息
    82. MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class);
    83. String hello = myAnnotation.hello();
    84. String world = myAnnotation.world();
    85. System.out.println(hello + ", " + world);//打印属性hello和world的值
    86. System.out.println(myAnnotation.array().length);//打印属性array数组的长度
    87. System.out.println(myAnnotation.lannotation().value()); //打印属性lannotation的值
    88. System.out.println(myAnnotation.style());
    89. }
    90. //得到output方法上的所有注解,当然是被RetentionPolicy.RUNTIME修饰的
    91. Annotation[] annotations = method.getAnnotations();
    92. for (Annotation annotation : annotations)
    93. {
    94. System.out.println(annotation.annotationType().getName());
    95. }
    96. }
    97. }
    98. 上面程序打印:
    99. have annotation
    100. output something!
    101. gege, shanghai
    102. 3
    103. baby
    104. class java.lang.String
    105. com.heima.annotation.MyAnnotation
    106. java.lang.Deprecated
    107. 如果注解中有一个属性名字叫value,则在应用时可以省略属性名字不写。
    108. 可见,@Retention(RetentionPolicy.RUNTIME )注解中,RetentionPolicy.RUNTIME是注解属性值,属性名字是value,
    109. 属性的返回类型是RetentionPolicy,如下:
    110. public @interface MyTarget
    111. {
    112. String value();
    113. }
    114. 可以这样用:
    115. @MyTarget("aaa")
    116. public void doSomething()
    117. {
    118. System.out.println("hello world");
    119. }
    120. 注解@Target也是用来修饰注解的元注解,它有一个属性ElementType也是枚举类型,
    121. 值为:ANNOTATION_TYPE CONSTRUCTOR  FIELD LOCAL_VARIABLE METHOD PACKAGE PARAMETER TYPE
    122. 如@Target(ElementType.METHOD) 修饰的注解表示该注解只能用来修饰在方法上。
    123. @Target(ElementType.METHOD)
    124. @Retention(RetentionPolicy.RUNTIME)
    125. public @interface MyTarget
    126. {
    127. String value() default "hahaha";
    128. }
    129. 如把@MyTarget修饰在类上,则程序报错,如:
    130. @MyTarget
    131. public class MyTargetTest
    132. 注解大都用在开发框架中吧,好了有关注解就学习那么多了,谢谢。  
      1. 本文转自http://blog.****.net/liuwenbo0920/article/details/7290586/