JDK5.0新特性Annotation之@Retention注解

直接代码说话,说明信息详见注释。。

首先是自定义的使用@Retention标注的注解----MyAnnotation.java

package com.jadyer.annotation.retention; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String hello() default "haerbin"; String world(); } /*********************【@Retention用来告知编译程序如何处理注解】********************************************************/ //java.lang.annotation.Retention型态可以在定义Annotation型态时,指示编译程序如何对待自定义的Annotation型态 //预设上编译程序会将Annotation信息留在.class档案中,但不被虚拟机读取,仅用于编译程序或工具程序运行时提供信息 //在使用Retention型态时,需要提供java.lang.annotation.RetentionPolicy的枚举型态 //java.lang.annotation.RetentionPolicy.CLASS----默认值。编译程序将Annotation储存于class档中 //java.lang.annotation.RetentionPolicy.SOURCE---编译程序处理完Annotation信息后就完成任务 // 该注释将不被记录在类文件中,而只是存在于.java文件中 //java.lang.annotation.RetentionPolicy.RUNTIME--编译程序将Annotation储存于class档中,可由JVM读入 // 即编译器将把注释记录在类文件中,在运行时JVM将保留注释 // 故可搭配反射(Reflection)机制让JVM读取Annotation信息 /*********************【@SuppressWarnings用来告知编译程序来抑制警告】***************************************************/ //其中@SuppressWarnings注解的RetentionPolicy就是SOURCE,即仅在编译时告知编译程序来抑制警告 //如果@SuppressWarnings中定义不存在的警告类型,那么程序也不会报错 //如果@SuppressWarnings中定义了重名的警告类型,那么后面的将全部被忽略掉 /*****************************************************************************************************************/

然后是自定义的@MyAnnotation注解的测试类----MyTest.java

package com.jadyer.annotation.retention; public class MyTest { @Deprecated @SuppressWarnings("unchecked") @MyAnnotation(hello = "beijing", world = "chongqing") public void output() { System.out.println("output something"); } }

最后利用反射机制读取MyTest.java中的Annotation信息的类----MyReflection.java

package com.jadyer.annotation.retention; import java.lang.annotation.Annotation; import java.lang.reflect.Method; /** * 利用反射机制,读取MyTest.java中的Annotation信息 * @see 控制台打印结果,如下 * @see output something * @see beijing * @see chongqing * @see java.lang.Deprecated * @see com.jadyer.annotation.retention.MyAnnotation */ public class MyReflection { public static void main(String[] args) throws Exception { MyTest myTest = new MyTest(); Class<MyTest> c = MyTest.class; Method method = c.getMethod("output", new Class[]{}); //判断方法前面是否存在特定的Annotation if (method.isAnnotationPresent(MyAnnotation.class)) { //调用MyTest中的output()方法 method.invoke(myTest, new Object[]{}); //获得某一具体的Annotation MyAnnotation myAnnotation = method.getAnnotation(MyAnnotation.class); //对于Annotation中的元素,赋值时可以把它当作属性,取出时可以把它当作方法 String hello = myAnnotation.hello(); String world = myAnnotation.world(); System.out.println(hello); System.out.println(world); } Annotation[] annotations = method.getAnnotations(); for (Annotation annotation : annotations) { //不会打印出@SuppressWarnings注解,因为它的RetentionPolicy是SOURCE System.out.println(annotation.annotationType().getName()); } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值