!711 修复元注解扫描器在注解出现循环引用时无限递归的问题

Merge pull request !711 from Createsequence/v5-dev
This commit is contained in:
Looly 2022-07-18 01:56:52 +00:00 committed by Gitee
commit 40fab305dd
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
2 changed files with 15 additions and 10 deletions

View File

@ -461,11 +461,11 @@ public class AnnotationUtil {
* <p>注解合成规则如下
* {@code AnnotatedEle}按顺序从上到下声明了ABC三个注解且三注解存在元注解如下
* <pre>
* A -&gt; MA1 -&gt; MA2
* B -&gt; MB1 -&gt; MB2
* C -&gt; MC1
* A -&gt; M3
* B -&gt; M1 -&gt; M2 -&gt; M3
* C -&gt; M2 -&gt; M3
* </pre>
* 此时入参{@code annotationType}类型为{@code MB1}则最终将优先返回基于根注解B合成的合成注解
* 此时入参{@code annotationType}类型为{@code M2}则最终将优先返回基于根注解B合成的合成注解
*
* @param annotatedEle {@link AnnotatedElement}可以是ClassMethodFieldConstructorReflectPermission
* @param annotationType 注解类
@ -499,7 +499,7 @@ public class AnnotationUtil {
* {@code AnnotatedEle}按顺序从上到下声明了ABC三个注解且三注解存在元注解如下
* <pre>
* A -&gt; M1 -&gt; M2
* B -&gt; M3 -&gt; M1
* B -&gt; M3 -&gt; M1 -&gt; M2
* C -&gt; M2
* </pre>
* 此时入参{@code annotationType}类型为{@code M1}则最终将返回基于根注解A与根注解B合成的合成注解

View File

@ -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());