修复CombinationAnnotationElement造成递归循环

This commit is contained in:
Looly 2022-07-17 18:17:32 +08:00
parent 45c5d11449
commit 5c6e7cf507
3 changed files with 8 additions and 10 deletions

View File

@ -27,6 +27,7 @@
* 【socket 】 修复异常socket没有关闭问题pr#690@Gitee * 【socket 】 修复异常socket没有关闭问题pr#690@Gitee
* 【core 】 修复当时间戳为Integer时时间转换问题pr#2449@Github * 【core 】 修复当时间戳为Integer时时间转换问题pr#2449@Github
* 【core 】 修复bmp文件判断问题issue#I5H93G@Gitee * 【core 】 修复bmp文件判断问题issue#I5H93G@Gitee
* 【core 】 修复CombinationAnnotationElement造成递归循环issue#I5FQGW@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -1,19 +1,13 @@
package cn.hutool.core.annotation; package cn.hutool.core.annotation;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.TableMap; import cn.hutool.core.map.TableMap;
import java.io.Serializable; import java.io.Serializable;
import java.lang.annotation.Annotation; import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.reflect.AnnotatedElement; import java.lang.reflect.AnnotatedElement;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.function.Predicate; import java.util.function.Predicate;
/** /**
@ -126,7 +120,9 @@ public class CombinationAnnotationElement implements AnnotatedElement, Serializa
// 直接注解 // 直接注解
for (Annotation annotation : annotations) { for (Annotation annotation : annotations) {
annotationType = annotation.annotationType(); annotationType = annotation.annotationType();
if (AnnotationUtil.isNotJdkMateAnnotation(annotationType)) { // 跳过元注解和已经处理过的注解防止递归调用
if (AnnotationUtil.isNotJdkMateAnnotation(annotationType)
&& false == declaredAnnotationMap.containsKey(annotationType)) {
if(test(annotation)){ if(test(annotation)){
declaredAnnotationMap.put(annotationType, annotation); declaredAnnotationMap.put(annotationType, annotation);
} }
@ -145,7 +141,8 @@ public class CombinationAnnotationElement implements AnnotatedElement, Serializa
Class<? extends Annotation> annotationType; Class<? extends Annotation> annotationType;
for (Annotation annotation : annotations) { for (Annotation annotation : annotations) {
annotationType = annotation.annotationType(); annotationType = annotation.annotationType();
if (AnnotationUtil.isNotJdkMateAnnotation(annotationType)) { if (AnnotationUtil.isNotJdkMateAnnotation(annotationType)
&& false == declaredAnnotationMap.containsKey(annotationType)) {
if(test(annotation)){ if(test(annotation)){
annotationMap.put(annotationType, annotation); annotationMap.put(annotationType, annotation);
} }

View File

@ -16,14 +16,14 @@ public class AnnotationUtilTest {
public void getCombinationAnnotationsTest(){ public void getCombinationAnnotationsTest(){
final Annotation[] annotations = AnnotationUtil.getAnnotations(ClassWithAnnotation.class, true); final Annotation[] annotations = AnnotationUtil.getAnnotations(ClassWithAnnotation.class, true);
Assert.assertNotNull(annotations); Assert.assertNotNull(annotations);
Assert.assertEquals(3, annotations.length); Assert.assertEquals(2, annotations.length);
} }
@Test @Test
public void getCombinationAnnotationsWithClassTest(){ public void getCombinationAnnotationsWithClassTest(){
final AnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassWithAnnotation.class, AnnotationForTest.class); final AnnotationForTest[] annotations = AnnotationUtil.getCombinationAnnotations(ClassWithAnnotation.class, AnnotationForTest.class);
Assert.assertNotNull(annotations); Assert.assertNotNull(annotations);
Assert.assertEquals(2, annotations.length); Assert.assertEquals(1, annotations.length);
Assert.assertEquals("测试", annotations[0].value()); Assert.assertEquals("测试", annotations[0].value());
} }