RetentionPolicy用于修饰注解能否被JVM所读取
来源:互联网 发布:淘宝有好货写手 编辑:程序博客网 时间:2024/06/08 17:36
import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Retention;@Retention(RetentionPolicy.RUNTIME)public @interface MyAnnotation{String hello() default "mylove";String world();}@MyAnnotation(hello="beijing",world="shanghai")//注解用语修饰类本身public class MyTest{@MyAnnotation(hello="nanjing",world="chengdu")@Deprecated@SuppressWarnings("unchecked")public void output(){System.out.println("output something");}} import java.lang.reflect.*;import java.lang.annotation.*;public class MyReflection{public static void main(String args[])throws Exception{MyTest mytest=new MyTest();Class<MyTest> classType=MyTest.class;Method method=classType.getMethod("output",new Class[]{});if(method.isAnnotationPresent(MyAnnotation.class)){method.invoke(mytest,new Object[]{});MyAnnotation myannotation=method.getAnnotation(MyAnnotation.class);String hello=myannotation.hello();String world=myannotation.world();System.out.println(hello+" , "+world);}Annotation [] annotations=method.getAnnotations();for(Annotation annotation:annotations){System.out.println(annotation.annotationType().getName());}}}
0 0
- RetentionPolicy用于修饰注解能否被JVM所读取
- 注解之RetentionPolicy,ElementType
- 注解之RetentionPolicy,ElementType
- 自定义注解之源码注解(RetentionPolicy.SOURCE)
- Java注解-注解处理器(编译期|RetentionPolicy.SOURCE)
- 自定义注解之运行时注解(RetentionPolicy.RUNTIME)
- 自定义注解之编译时注解(RetentionPolicy.CLASS)(一)
- Java注解(3)-注解处理器(编译期|RetentionPolicy.SOURCE)
- 自定义注解之编译时注解(RetentionPolicy.CLASS)(一)
- Java注解(2)-注解处理器(运行时|RetentionPolicy.RUNTIME)
- 自定义注解之运行时注解(RetentionPolicy.RUNTIME)
- OC-探究private修饰的属性能否被继承
- 深入理解编译注解(五)RetentionPolicy.SOURCE 和 RetentionPolicy.CLASS区别讨论
- private能否修饰顶层类?
- 自定义注解之编译时注解(RetentionPolicy.CLASS)(二)——JavaPoet
- 自定义注解之编译时注解(RetentionPolicy.CLASS)(三)—— 常用接口介绍
- 自定义注解之编译时注解(RetentionPolicy.CLASS)(二)——JavaPoet
- C++用于修饰的关键字
- 如何将写在对话框上的内容转到一个picture的控件下面
- Problem 13:Large sum
- 新人如何面对倚老卖老的同事
- ActiveMQ入门实例
- 深入浅出TCP之半关闭与CLOSE_WAIT
- RetentionPolicy用于修饰注解能否被JVM所读取
- wince下serlocaltime失效的原因
- js截取字符串
- UNDO相关问题总结(三)
- Highcharts基本柱图
- leetcode-Merge Two Sorted Lists
- hdoj 1885 Key Task(BFS + 状压)
- Z-stack之OSAL初始化流程
- android如何实现开机自动启动Service或app