mirror of
https://gitee.com/dromara/hutool.git
synced 2025-04-05 17:37:59 +08:00
!711 修复元注解扫描器在注解出现循环引用时无限递归的问题
Merge pull request !711 from Createsequence/v5-dev
This commit is contained in:
commit
40fab305dd
@ -461,11 +461,11 @@ public class AnnotationUtil {
|
||||
* <p>注解合成规则如下:
|
||||
* 若{@code AnnotatedEle}按顺序从上到下声明了A,B,C三个注解,且三注解存在元注解如下:
|
||||
* <pre>
|
||||
* A -> MA1 -> MA2
|
||||
* B -> MB1 -> MB2
|
||||
* C -> MC1
|
||||
* A -> M3
|
||||
* B -> M1 -> M2 -> M3
|
||||
* C -> M2 -> M3
|
||||
* </pre>
|
||||
* 此时入参{@code annotationType}类型为{@code MB1},则最终将优先返回基于根注解B合成的合成注解
|
||||
* 此时入参{@code annotationType}类型为{@code M2},则最终将优先返回基于根注解B合成的合成注解
|
||||
*
|
||||
* @param annotatedEle {@link AnnotatedElement},可以是Class、Method、Field、Constructor、ReflectPermission
|
||||
* @param annotationType 注解类
|
||||
@ -499,7 +499,7 @@ public class AnnotationUtil {
|
||||
* 若{@code AnnotatedEle}按顺序从上到下声明了A,B,C三个注解,且三注解存在元注解如下:
|
||||
* <pre>
|
||||
* A -> M1 -> M2
|
||||
* B -> M3 -> M1
|
||||
* B -> M3 -> M1 -> M2
|
||||
* C -> M2
|
||||
* </pre>
|
||||
* 此时入参{@code annotationType}类型为{@code M1},则最终将返回基于根注解A与根注解B合成的合成注解。
|
||||
|
@ -1,16 +1,13 @@
|
||||
package cn.hutool.core.annotation.scanner;
|
||||
|
||||
import cn.hutool.core.annotation.AnnotationUtil;
|
||||
import cn.hutool.core.collection.CollStreamUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ClassUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.AnnotatedElement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
@ -84,6 +81,7 @@ public class MetaAnnotationScanner implements AnnotationScanner {
|
||||
@Override
|
||||
public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
|
||||
filter = ObjectUtil.defaultIfNull(filter, t -> true);
|
||||
Set<Class<? extends Annotation>> accessed = new HashSet<>();
|
||||
final Deque<List<Class<? extends Annotation>>> deque = CollUtil.newLinkedList(CollUtil.newArrayList((Class<? extends Annotation>)annotatedEle));
|
||||
int distance = 0;
|
||||
do {
|
||||
@ -96,7 +94,14 @@ public class MetaAnnotationScanner implements AnnotationScanner {
|
||||
for (final Annotation metaAnnotation : metaAnnotations) {
|
||||
consumer.accept(distance, metaAnnotation);
|
||||
}
|
||||
deque.addLast(CollStreamUtil.toList(metaAnnotations, Annotation::annotationType));
|
||||
accessed.add(type);
|
||||
List<Class<? extends Annotation>> next = metaAnnotations.stream()
|
||||
.map(Annotation::annotationType)
|
||||
.filter(t -> !accessed.contains(t))
|
||||
.collect(Collectors.toList());
|
||||
if (CollUtil.isNotEmpty(next)) {
|
||||
deque.addLast(next);
|
||||
}
|
||||
}
|
||||
distance++;
|
||||
} while (includeSupperMetaAnnotation && !deque.isEmpty());
|
||||
|
Loading…
Reference in New Issue
Block a user