- 项目中使用到需要查询注解对应的实体类的位置做个记录
-
使用递归查询该类及其父类和接口是否有对应注解
-
- AnnotationUtils 是spring提供的:比较懒就用了
-
org.springframework.core.annotation.AnnotationUtils
-
isAnnotationDeclaredLocally: 是在本地声明的注释
-
人话:这个类有指定注解
-
-
- 本人是个菜鸡有什么不对的地方欢迎大佬指出
/**
* use recursion find the class that contains annotationType in all classes
*
* @param annotationType annotationType
* @param clazz clazz
* @param classes classes
*/
public void findAnnotationDeclaring(Class<? extends Annotation> annotationType, Class<?> clazz,
List<Class<?>> classes) {
Assert.notNull(annotationType, "Annotation type must not be null");
if (clazz == null || Object.class == clazz) {
return;
}
if (AnnotationUtils.isAnnotationDeclaredLocally(annotationType, clazz)) {
classes.add(clazz);
}
//Superclass
if (!Object.class.equals(clazz.getSuperclass())) {
findAnnotationDeclaring(annotationType, clazz.getSuperclass(), classes);
}
//Interface
if (ArrayUtils.isNotEmpty(clazz.getInterfaces())) {
for (Class<?> clazzInterface : clazz.getInterfaces()) {
findAnnotationDeclaring(annotationType, clazzInterface, classes);
}
}
}