mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +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>注解合成规则如下:
|
* <p>注解合成规则如下:
|
||||||
* 若{@code AnnotatedEle}按顺序从上到下声明了A,B,C三个注解,且三注解存在元注解如下:
|
* 若{@code AnnotatedEle}按顺序从上到下声明了A,B,C三个注解,且三注解存在元注解如下:
|
||||||
* <pre>
|
* <pre>
|
||||||
* A -> MA1 -> MA2
|
* A -> M3
|
||||||
* B -> MB1 -> MB2
|
* B -> M1 -> M2 -> M3
|
||||||
* C -> MC1
|
* C -> M2 -> M3
|
||||||
* </pre>
|
* </pre>
|
||||||
* 此时入参{@code annotationType}类型为{@code MB1},则最终将优先返回基于根注解B合成的合成注解
|
* 此时入参{@code annotationType}类型为{@code M2},则最终将优先返回基于根注解B合成的合成注解
|
||||||
*
|
*
|
||||||
* @param annotatedEle {@link AnnotatedElement},可以是Class、Method、Field、Constructor、ReflectPermission
|
* @param annotatedEle {@link AnnotatedElement},可以是Class、Method、Field、Constructor、ReflectPermission
|
||||||
* @param annotationType 注解类
|
* @param annotationType 注解类
|
||||||
@ -499,7 +499,7 @@ public class AnnotationUtil {
|
|||||||
* 若{@code AnnotatedEle}按顺序从上到下声明了A,B,C三个注解,且三注解存在元注解如下:
|
* 若{@code AnnotatedEle}按顺序从上到下声明了A,B,C三个注解,且三注解存在元注解如下:
|
||||||
* <pre>
|
* <pre>
|
||||||
* A -> M1 -> M2
|
* A -> M1 -> M2
|
||||||
* B -> M3 -> M1
|
* B -> M3 -> M1 -> M2
|
||||||
* C -> M2
|
* C -> M2
|
||||||
* </pre>
|
* </pre>
|
||||||
* 此时入参{@code annotationType}类型为{@code M1},则最终将返回基于根注解A与根注解B合成的合成注解。
|
* 此时入参{@code annotationType}类型为{@code M1},则最终将返回基于根注解A与根注解B合成的合成注解。
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
package cn.hutool.core.annotation.scanner;
|
package cn.hutool.core.annotation.scanner;
|
||||||
|
|
||||||
import cn.hutool.core.annotation.AnnotationUtil;
|
import cn.hutool.core.annotation.AnnotationUtil;
|
||||||
import cn.hutool.core.collection.CollStreamUtil;
|
|
||||||
import cn.hutool.core.collection.CollUtil;
|
import cn.hutool.core.collection.CollUtil;
|
||||||
import cn.hutool.core.util.ClassUtil;
|
import cn.hutool.core.util.ClassUtil;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.lang.reflect.AnnotatedElement;
|
import java.lang.reflect.AnnotatedElement;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Deque;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.function.BiConsumer;
|
import java.util.function.BiConsumer;
|
||||||
import java.util.function.Predicate;
|
import java.util.function.Predicate;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@ -84,6 +81,7 @@ public class MetaAnnotationScanner implements AnnotationScanner {
|
|||||||
@Override
|
@Override
|
||||||
public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
|
public void scan(BiConsumer<Integer, Annotation> consumer, AnnotatedElement annotatedEle, Predicate<Annotation> filter) {
|
||||||
filter = ObjectUtil.defaultIfNull(filter, t -> true);
|
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));
|
final Deque<List<Class<? extends Annotation>>> deque = CollUtil.newLinkedList(CollUtil.newArrayList((Class<? extends Annotation>)annotatedEle));
|
||||||
int distance = 0;
|
int distance = 0;
|
||||||
do {
|
do {
|
||||||
@ -96,7 +94,14 @@ public class MetaAnnotationScanner implements AnnotationScanner {
|
|||||||
for (final Annotation metaAnnotation : metaAnnotations) {
|
for (final Annotation metaAnnotation : metaAnnotations) {
|
||||||
consumer.accept(distance, metaAnnotation);
|
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++;
|
distance++;
|
||||||
} while (includeSupperMetaAnnotation && !deque.isEmpty());
|
} while (includeSupperMetaAnnotation && !deque.isEmpty());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user