java中如何访问Annotation?
小妮浅浅
2021-04-06 17:11:23395浏览 · 0收藏 · 0评论
1、方法说明
方法一:TgetAnnotation(ClassannotationClass):返回程序元素中存在的指定类型的注释,如果没有注释,则返回null。
方法二:Annotation[]get安卓():返回程序元素上的所有注释。
方法三:booleanisAnnotationPresent(ClassannotationClass):判断程序元素中是否有指定类型的注释,如果有,则返回true,否则返回false。
方法四:Annotation[]getDeclaredAnnotations():返回所有直接存在于该元素中的注释。不像这个界面上的其他方法,这种方法会忽略继承的注释。(如果没有注释直接存在于这个元素中,则返回一个长度为零的数组。)这种方法的调用者可以随意修改返回的数组;这种方法不会影响其他调用者返回的数组。
定义注解使用java.lang.annotation.Annotation,操作注解使用java.lang.reflect.AnnotatedElement。
2、实例
/** * @author 刘俊重 * @Description 自定义类注解 */ @Target(ElementType.TYPE) //作用在类,枚举或接口上 @Retention(RetentionPolicy.RUNTIME) //运行时有效 @Documented //文档可见 public @interface CustomClassAnnotation { String value(); //获取注解名称 }
以上就是java中访问Annotation的方法,希望对大家有所帮助。更多Java学习指路:Java基础
关注公众号,随时随地在线学习
相关文章