下面列出了org.springframework.core.annotation.AnnotationUtils#getAnnotations ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void scanCheckAnnos(AnnotatedElement annotatedElement, TagArrayList<Annotation> checkAnnos) {
Annotation[] annotations = AnnotationUtils.getAnnotations(annotatedElement);
if (CollectionUtil.isNotEmpty(annotations)) {
for (Annotation annotation : annotations) {
//在anno上查找,是否有anno标注为Check
Check check = AnnotationUtils.getAnnotation(annotation, Check.class);
if (check != null) {
Repeatable repeatable = AnnotationUtils.getAnnotation(annotation, Repeatable.class);
if (repeatable != null) {
Class<? extends Annotation> realCheckAnnoClazz = repeatable.value();
Set<? extends Annotation> realCheckAnnos = AnnotatedElementUtils.getMergedRepeatableAnnotations(annotatedElement,
realCheckAnnoClazz);
checkAnnos.addAll(realCheckAnnos);
} else {
checkAnnos.add(annotation);
}
}
}
}
}
@Override
public void doVisitEnd(Class<?> annotationClass) {
super.doVisitEnd(annotationClass);
List<AnnotationAttributes> attributes = this.attributesMap.get(this.annotationType);
if (attributes == null) {
this.attributesMap.add(this.annotationType, this.attributes);
}
else {
attributes.add(0, this.attributes);
}
Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>();
Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass);
if (!ObjectUtils.isEmpty(metaAnnotations)) {
for (Annotation metaAnnotation : metaAnnotations) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
recursivelyCollectMetaAnnotations(metaAnnotationTypeNames, metaAnnotation);
}
}
}
if (this.metaAnnotationMap != null) {
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
}
}
@Override
protected Collection<ConfigAttribute> findAttributes(Method method, Class<?> targetClass) {
Annotation[] annotations = AnnotationUtils.getAnnotations(method);
List<ConfigAttribute> attributes = new ArrayList<>();
// if the class is annotated as @Controller we should by default deny access to every method
if (AnnotationUtils.findAnnotation(targetClass, Controller.class) != null) {
attributes.add(DENY_ALL_ATTRIBUTE);
}
if (annotations != null) {
for (Annotation a : annotations) {
// but not if the method has at least a PreAuthorize or PostAuthorize annotation
if (a instanceof PreAuthorize || a instanceof PostAuthorize) {
return null;
}
}
}
return attributes;
}
@Override
public void visitEnd() {
super.visitEnd();
Class<?> annotationClass = this.attributes.annotationType();
if (annotationClass != null) {
List<AnnotationAttributes> attributeList = this.attributesMap.get(this.annotationType);
if (attributeList == null) {
this.attributesMap.add(this.annotationType, this.attributes);
}
else {
attributeList.add(0, this.attributes);
}
Set<Annotation> visited = new LinkedHashSet<Annotation>();
Annotation[] metaAnnotations = AnnotationUtils.getAnnotations(annotationClass);
if (!ObjectUtils.isEmpty(metaAnnotations)) {
for (Annotation metaAnnotation : metaAnnotations) {
if (!AnnotationUtils.isInJavaLangAnnotationPackage(metaAnnotation)) {
recursivelyCollectMetaAnnotations(visited, metaAnnotation);
}
}
}
if (this.metaAnnotationMap != null) {
Set<String> metaAnnotationTypeNames = new LinkedHashSet<String>(visited.size());
for (Annotation ann : visited) {
metaAnnotationTypeNames.add(ann.annotationType().getName());
}
this.metaAnnotationMap.put(annotationClass.getName(), metaAnnotationTypeNames);
}
}
}
@Test
public void delete_methodSecured() throws NoSuchMethodException {
Annotation[] annotations = AnnotationUtils.getAnnotations(CommunityServiceImpl.class.getMethod("delete", String.class));
List<Annotation> annotationsList = Arrays.asList(annotations);
Optional<Annotation> securedOpt = annotationsList.stream()
.filter(a -> a.annotationType().isAssignableFrom(Secured.class))
.findFirst();
assertTrue(securedOpt.isPresent());
Annotation securedAnnotation = securedOpt.get();
String[] values = (String[]) AnnotationUtils.getValue(securedAnnotation);
assertTrue(Arrays.asList(values).contains(Role.ADMIN));
assertTrue(Arrays.asList(values).contains(Role.SYSTEM));
}
@Test
public void findAll_byPageable_methodSecured() throws NoSuchMethodException {
Annotation[] annotations = AnnotationUtils.getAnnotations(CommunityServiceImpl.class.getMethod("findAll", Pageable.class));
List<Annotation> annotationsList = Arrays.asList(annotations);
Optional<Annotation> securedOpt = annotationsList.stream()
.filter(a -> a.annotationType().isAssignableFrom(Secured.class))
.findFirst();
assertTrue(securedOpt.isPresent());
Annotation securedAnnotation = securedOpt.get();
String[] values = (String[]) AnnotationUtils.getValue(securedAnnotation);
assertTrue(Arrays.asList(values).contains(Role.ADMIN));
}
@Test
public void getUserByEmail_methodSecured() throws NoSuchMethodException {
Annotation[] annotations = AnnotationUtils.getAnnotations(CommunityServiceImpl.class.getMethod("getUserByEmail", String.class));
List<Annotation> annotationsList = Arrays.asList(annotations);
Optional<Annotation> securedOpt = annotationsList.stream()
.filter(a -> a.annotationType().isAssignableFrom(Secured.class))
.findFirst();
assertTrue(securedOpt.isPresent());
Annotation securedAnnotation = securedOpt.get();
String[] values = (String[]) AnnotationUtils.getValue(securedAnnotation);
assertTrue(Arrays.asList(values).contains(Role.ADMIN));
}
/**
* 由于注解无继承关系,因此这里的设计为:A注解上打上B注解,就视为A注解继承了B注解。
* 由此循环遍历,当注解“继承”了ExtensionPoint,就视为ExtensionPoint。
* 注解的注解规避不了循环嵌套的问题,因此采用stack记录堆栈,避免死循环。
*/
private ExtensionPointImpl findExtensionPoint(
Class<? extends Annotation> clazz, LinkedList<Class<?>> stack) {
// 扩展点找到过,直接跳过
ExtensionPointImpl point = extensionPoints.get(clazz.getName());
if (point != null) {
return point;
}
// 扩展点的没有注解,跳过
Annotation[] annotations = AnnotationUtils.getAnnotations(clazz);
if (annotations == null || annotations.length == 0) {
return null;
}
// 记录循环层级
stack.add(clazz);
try {
List<String> types = new ArrayList<>();
for (Annotation annotation : annotations) {
if (annotation instanceof ListenerConfig) {
// 监听器注册
listenerManager.addListener((ListenerConfig) annotation);
} else if (annotation instanceof LocalExtensionPoint) {
// 有LocalExtensionPoint的注解,说明clazz就是扩展点,读取扩展点信息
point = ExtensionPointBuilder.newExtensionPoint(
clazz.getName(), (LocalExtensionPoint) annotation);
listenerManager.addListener(
(LocalExtensionPoint) annotation, clazz);
} else if (annotation instanceof GlobalExtensionPoint) {
// 有GlobalExtensionPoint的注解,说明clazz就是扩展点,读取扩展点信息
point = ExtensionPointBuilder.newExtensionPoint(
clazz.getName(), (GlobalExtensionPoint) annotation);
listenerManager.addListener(
(GlobalExtensionPoint) annotation, clazz);
} else {
// 若stack包含了注解类,说明递归循环嵌套了,直接忽略
Class<? extends Annotation> annotationType = annotation
.annotationType();
if (!annotationType.getName().startsWith(BASE_PACKAGE)
|| stack.contains(annotationType)) {
continue;
}
ExtensionPointImpl superPoint = findExtensionPoint(
annotationType, stack);
// 注解的注解是扩展点,则注解也是扩展点,复制父类属性,再用当前注解信息覆盖父类信息。
if (superPoint == null) {
continue;
}
if (point == null) {
point = ExtensionPointBuilder.newExtensionPoint(
clazz.getName(), superPoint, annotation);
}
// 继承父类的关系
types.addAll(superPoint.getAnnotationTypes());
}
}
// 设置关联关系并记录到extensionPoints
if (point != null) {
types.add(clazz.getName());
point.setAnnotationTypes(types);
extensionPoints.put(clazz.getName(), point);
}
} catch (Exception e) {
log.error("加载扩展点信息时发生错误:" + clazz.getName(), e);
}
// 循环层级退出
stack.removeLast();
return point;
}
/**
* 由于注解无继承关系,因此这里的设计为:A注解上打上B注解,就视为A注解继承了B注解。
* 由此循环遍历,当注解“继承”了ExtensionPoint,就视为ExtensionPoint。
* 注解的注解规避不了循环嵌套的问题,因此采用stack记录堆栈,避免死循环。
*/
private ExtensionPointImpl findExtensionPoint(
Class<? extends Annotation> clazz, LinkedList<Class<?>> stack) {
// 扩展点找到过,直接跳过
ExtensionPointImpl point = extensionPoints.get(clazz.getName());
if (point != null) {
return point;
}
// 扩展点的没有注解,跳过
Annotation[] annotations = AnnotationUtils.getAnnotations(clazz);
if (annotations == null || annotations.length == 0) {
return null;
}
// 记录循环层级
stack.add(clazz);
try {
List<String> types = new ArrayList<>();
for (Annotation annotation : annotations) {
if (annotation instanceof ListenerConfig) {
// 监听器注册
listenerManager.addListener((ListenerConfig) annotation);
} else if (annotation instanceof LocalExtensionPoint) {
// 有LocalExtensionPoint的注解,说明clazz就是扩展点,读取扩展点信息
point = ExtensionPointBuilder.newExtensionPoint(
clazz.getName(), (LocalExtensionPoint) annotation);
listenerManager.addListener(
(LocalExtensionPoint) annotation, clazz);
} else if (annotation instanceof GlobalExtensionPoint) {
// 有GlobalExtensionPoint的注解,说明clazz就是扩展点,读取扩展点信息
point = ExtensionPointBuilder.newExtensionPoint(
clazz.getName(), (GlobalExtensionPoint) annotation);
listenerManager.addListener(
(GlobalExtensionPoint) annotation, clazz);
} else {
// 若stack包含了注解类,说明递归循环嵌套了,直接忽略
Class<? extends Annotation> annotationType = annotation
.annotationType();
if (!annotationType.getName().startsWith(BASE_PACKAGE)
|| stack.contains(annotationType)) {
continue;
}
ExtensionPointImpl superPoint = findExtensionPoint(
annotationType, stack);
// 注解的注解是扩展点,则注解也是扩展点,复制父类属性,再用当前注解信息覆盖父类信息。
if (superPoint == null) {
continue;
}
if (point == null) {
point = ExtensionPointBuilder.newExtensionPoint(
clazz.getName(), superPoint, annotation);
}
// 继承父类的关系
types.addAll(superPoint.getAnnotationTypes());
}
}
// 设置关联关系并记录到extensionPoints
if (point != null) {
types.add(clazz.getName());
point.setAnnotationTypes(types);
extensionPoints.put(clazz.getName(), point);
}
} catch (Exception e) {
log.error("加载扩展点信息时发生错误:" + clazz.getName(), e);
}
// 循环层级退出
stack.removeLast();
return point;
}