修复当注解存在循环引用时报错的问题

This commit is contained in:
huangchengxing 2022-09-21 18:18:55 +08:00
parent 96d6c39a14
commit de06d8777c
5 changed files with 21 additions and 12 deletions

View File

@ -10,7 +10,6 @@ import java.lang.annotation.Inherited;
import java.lang.reflect.AnnotatedElement;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Stream;
/**
* <p>注解元素映射用于包装一个{@link AnnotatedElement}然后将被包装的元素上
@ -314,11 +313,15 @@ public class MetaAnnotatedElement<T extends AnnotationMapping<Annotation>> imple
}
// 保存该注解并将其需要处理的元注解也加入队列
mappings.put(mapping.annotationType(), mapping);
Stream.of(AnnotationUtil.getDeclaredAnnotations(mapping.annotationType()))
.map(annotation -> createMapping(mapping, annotation))
.filter(Objects::nonNull)
.filter(m -> isNeedMapping(mappings, m))
.forEach(deque::addLast);
for (final Annotation annotation : AnnotationUtil.getDeclaredAnnotations(mapping.annotationType())) {
if (mappings.containsKey(annotation.annotationType())) {
continue;
}
final T m = createMapping(mapping, annotation);
if (Objects.nonNull(m) && isNeedMapping(mappings, m)) {
deque.addLast(m);
}
}
}
}
}

View File

@ -10,7 +10,6 @@ import java.lang.reflect.AnnotatedElement;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* <p>支持可重复注解的增强{@link AnnotatedElement}
@ -347,11 +346,15 @@ public class RepeatableMetaAnnotatedElement<T extends AnnotationMapping<Annotati
continue;
}
collectedMappings.put(source.annotationType(), source);
Stream.of(AnnotationUtil.getDeclaredAnnotations(source.annotationType()))
.map(annotation -> mappingFactory.apply(source, annotation))
.filter(Objects::nonNull)
.filter(m -> isNeedMapping(collectedMappings, m))
.forEach(deque::addLast);
for (final Annotation annotation : AnnotationUtil.getDeclaredAnnotations(source.annotationType())) {
if (collectedMappings.containsKey(annotation.annotationType())) {
continue;
}
final T mapping = mappingFactory.apply(source, annotation);
if (Objects.nonNull(mapping) && isNeedMapping(collectedMappings, mapping)) {
deque.addLast(mapping);
}
}
}
return collectedMappings;
}

View File

@ -572,6 +572,7 @@ public class AnnotatedElementUtilTest {
// ================= interface =================
@Annotation6
@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
private @interface Annotation5 {}

View File

@ -170,6 +170,7 @@ public class MetaAnnotatedElementTest {
Assert.assertSame(source, element.getElement());
}
@Annotation4 // 循环引用
@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
private @interface None { }

View File

@ -199,6 +199,7 @@ public class RepeatableMetaAnnotatedElementTest {
@Retention(RetentionPolicy.RUNTIME)
private @interface Annotation3 { }
@Annotation4 // 循环引用
@Target(ElementType.TYPE_USE)
@Retention(RetentionPolicy.RUNTIME)
private @interface Annotation4 { }