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

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

View File

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

View File

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

View File

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