annotation详解二

在软件开发中,annotation注解和反射的使用是相对比较难以理解的,需要我们多练习实践,今天我从基础开始,一步一步实践一下annotation的各个相关方法。

AnnotationElement

java8新增了这个接口,将Annotation相关的几个基本方法封装在里面
- isAnnotationPresent 判断注解是否存在。
- getAnnotation(Class annotationClass) //获取对应类型的注解
- getAnnotations(); //获取全部注解
- getDeclaredAnnotations(); //获取全部注解,但忽略继承的注解
- getAnnotationsByType(Class annotationClass) //有默认实现,获取指定注解,获取不到从子类递归获取
- getDeclaredAnnotation(Class annotationClass) // 获取指定注解
- getDeclaredAnnotationsByType(Class annotationClass) // 有默认实现,获取指定注解,忽略继承的注解


class,method等方法都实现了AnnotationElement,因此他们有这些基本方法。
这里写图片描述

@Repeatable

表示在同一个位置重复相同的注解
- AnnotationElement中新增的getAnnotationsByType和getDeclaredAnnotationsByType就是对应于获取多个重复注解,当我们用了重复注解,用getAnnotation(xx)我们会发现是无法获取到对应的注解,原因在于Repeatable是指定一个一个容器。
这里写图片描述

猜你喜欢

转载自blog.csdn.net/u013179884/article/details/79903333