From 84db7222f0c0267ad19a061b8f992f3eb9bff8a5 Mon Sep 17 00:00:00 2001
From: huangchengxing <841396397@qq.com>
Date: Wed, 21 Sep 2022 19:06:24 +0800
Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E6=96=B9=E6=B3=95=E5=90=8D?=
=?UTF-8?q?=E7=A7=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../RepeatableAnnotationCollector.java | 71 ++++++++++++++-----
.../RepeatableMetaAnnotatedElement.java | 2 +-
.../RepeatableAnnotationCollectorTest.java | 44 ++++++------
3 files changed, 77 insertions(+), 40 deletions(-)
diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/RepeatableAnnotationCollector.java b/hutool-core/src/main/java/cn/hutool/core/annotation/RepeatableAnnotationCollector.java
index 38579178d..1daae6cdb 100644
--- a/hutool-core/src/main/java/cn/hutool/core/annotation/RepeatableAnnotationCollector.java
+++ b/hutool-core/src/main/java/cn/hutool/core/annotation/RepeatableAnnotationCollector.java
@@ -32,9 +32,20 @@ public interface RepeatableAnnotationCollector {
}
/**
- * 当注解中有且仅有一个名为{@code value}的属性时,
+ *
当注解中有且仅有一个名为{@code value}的属性时,
* 若该属性类型为注解数组,且该数组对应的注解类型被{@link Repeatable}注解,
- * 则收集器将返回该属性中包括的可重复注解。
+ * 则收集器将返回该属性中包括的可重复注解。
+ * eg:
+ *
+ * // 容器注解
+ * {@literal @}interface Annotation {
+ * Item[] value() default {};
+ * }
+ * // 可重复注解
+ * {@literal @}Repeatable(Annotation.class)
+ * {@literal @}interface Item {}
+ *
+ * 解析任意{@code Annotation}注解对象,则可以获得{@code value}属性中的{@code Item}注解对象
*
* @return {@link RepeatableAnnotationCollector}实例
* @see Standard
@@ -55,9 +66,19 @@ public interface RepeatableAnnotationCollector {
}
/**
- * 当注解中存在有属性为注解数组,且该数组对应的注解类型被{@link Repeatable}注解时,
+ * 当注解中存在有属性为注解数组,且该数组对应的注解类型被{@link Repeatable}注解时,
* 认为该属性包含可重复注解。
- * 收集器将返回所有符合上述条件的属性中的可重复注解。
+ * 收集器将返回所有符合上述条件的属性中的可重复注解。
+ * eg:
+ *
+ * {@literal @}interface Annotation {
+ * Item1[] items1() default {};
+ * Item2[] items2() default {};
+ * }
+ *
+ * 解析任意{@code Annotation}注解对象,
+ * 则可以获得{@code items1}属性中的{@code Item1}注解对象,
+ * 以及{@code items2}属性中的{@code Item2}注解对象,
*
* @return {@link RepeatableAnnotationCollector}实例
*/
@@ -67,26 +88,39 @@ public interface RepeatableAnnotationCollector {
/**
* 若一个注解是可重复注解的容器注解,则尝试通过其属性获得获得包含的注解对象。
- * 若包含的注解对象也是可重复注解的容器注解,则继续解析直到获得所有非容器注解为止。
+ * 若包含的注解对象也是可重复注解的容器注解,则继续解析直到获得所有非容器注解为止。
+ * eg:
+ * 若存在嵌套关系{@code a -> b -> c},
+ * 则解析注解a,则将得到全部c注解。
+ * 如果注解不包含可重复注解,则返回a本身。
*
* @param annotation 容器注解
* @return 容器注解中的可重复注解,若{@code annotation}不为容器注解,则数组中仅有其本身一个对象
*/
- List getRepeatableAnnotations(final Annotation annotation);
+ List getFinalRepeatableAnnotations(final Annotation annotation);
/**
* 若一个注解是可重复注解的容器注解,则尝试通过其属性获得获得包含的注解对象。
* 若包含的注解对象也是可重复注解的容器注解,则继续解析直到获得所有非容器注解为止。
- * 当{@code accumulate}为{@code true}时,返回结果为全量的注解。
+ * eg:
+ * 若存在嵌套关系{@code a -> b -> c},则解析注解a,
+ * 将获得全部a、b、c注解。
+ * 如果注解不包含可重复注解,则返回a本身。
*
* @param annotation 容器注解
- * @param accumulate 是否累加
* @return 容器注解中的可重复注解,若{@code annotation}不为容器注解,则数组中仅有其本身一个对象
*/
- List getRepeatableAnnotations(final Annotation annotation, final boolean accumulate);
+ List getAllRepeatableAnnotations(final Annotation annotation);
/**
- * 若一个注解是可重复注解的容器注解,则尝试通过其属性获得获得包含的指定类型注解对象
+ * 若一个注解是可重复注解的容器注解,则尝试通过其属性获得获得包含的指定类型注解对象。
+ * eg:
+ * 若存在嵌套关系{@code a -> b -> c},则:
+ *
+ * - 解析注解a:可获得a、b、c;
+ * - 解析注解b:可获得b、c;
+ * - 解析注解c:只可获得c;
+ *
*
* @param annotation 容器注解
* @param annotationType 注解类型
@@ -112,7 +146,7 @@ public interface RepeatableAnnotationCollector {
* @return 空集合
*/
@Override
- public List getRepeatableAnnotations(final Annotation annotation) {
+ public List getFinalRepeatableAnnotations(final Annotation annotation) {
return Objects.isNull(annotation) ?
Collections.emptyList() : Collections.singletonList(annotation);
}
@@ -124,7 +158,7 @@ public interface RepeatableAnnotationCollector {
* @return 空集合
*/
@Override
- public List getRepeatableAnnotations(final Annotation annotation, final boolean accumulate) {
+ public List getAllRepeatableAnnotations(final Annotation annotation) {
return Objects.isNull(annotation) ?
Collections.emptyList() : Collections.singletonList(annotation);
}
@@ -159,22 +193,25 @@ public interface RepeatableAnnotationCollector {
* @return 容器注解中的可重复注解,若{@code annotation}不为容器注解,则数组中仅有其本身一个对象
*/
@Override
- public final List getRepeatableAnnotations(final Annotation annotation) {
- return getRepeatableAnnotations(annotation, false);
+ public final List getFinalRepeatableAnnotations(final Annotation annotation) {
+ return find(annotation, null, false);
}
/**
* 若一个注解是可重复注解的容器注解,则尝试通过其属性获得获得包含的注解对象。
* 若包含的注解对象也是可重复注解的容器注解,则继续解析直到获得所有非容器注解为止。
* 当{@code accumulate}为{@code true}时,返回结果为全量的注解。
+ * eg:
+ * 若存在嵌套关系{@code a -> b -> c},则解析注解a,
+ * 将获得全部a、b、c注解。
+ * 如果注解不包含可重复注解,则返回其本身。
*
* @param annotation 容器注解
- * @param accumulate 是否累加
* @return 容器注解中的可重复注解,若{@code annotation}不为容器注解,则数组中仅有其本身一个对象
*/
@Override
- public final List getRepeatableAnnotations(final Annotation annotation, final boolean accumulate) {
- return find(annotation, null, accumulate);
+ public List getAllRepeatableAnnotations(Annotation annotation) {
+ return find(annotation, null, true);
}
/**
diff --git a/hutool-core/src/main/java/cn/hutool/core/annotation/RepeatableMetaAnnotatedElement.java b/hutool-core/src/main/java/cn/hutool/core/annotation/RepeatableMetaAnnotatedElement.java
index bc10645c7..1c2894a5d 100644
--- a/hutool-core/src/main/java/cn/hutool/core/annotation/RepeatableMetaAnnotatedElement.java
+++ b/hutool-core/src/main/java/cn/hutool/core/annotation/RepeatableMetaAnnotatedElement.java
@@ -285,7 +285,7 @@ public class RepeatableMetaAnnotatedElement collectRepeatable(final Annotation annotation) {
- return repeatableCollector.getRepeatableAnnotations(annotation, true)
+ return repeatableCollector.getAllRepeatableAnnotations(annotation)
.stream()
.map(a -> new Aggregation(a, Objects.equals(a, annotation)))
.collect(Collectors.toList());
diff --git a/hutool-core/src/test/java/cn/hutool/core/annotation/RepeatableAnnotationCollectorTest.java b/hutool-core/src/test/java/cn/hutool/core/annotation/RepeatableAnnotationCollectorTest.java
index 7482d5b55..ac9386a57 100644
--- a/hutool-core/src/test/java/cn/hutool/core/annotation/RepeatableAnnotationCollectorTest.java
+++ b/hutool-core/src/test/java/cn/hutool/core/annotation/RepeatableAnnotationCollectorTest.java
@@ -55,13 +55,13 @@ public class RepeatableAnnotationCollectorTest {
RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.none();
Assert.assertSame(collector, RepeatableAnnotationCollector.none());
- Assert.assertEquals(0, collector.getRepeatableAnnotations(null).size());
+ Assert.assertEquals(0, collector.getFinalRepeatableAnnotations(null).size());
Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class);
- Assert.assertEquals(Collections.singletonList(annotation), collector.getRepeatableAnnotations(annotation));
+ Assert.assertEquals(Collections.singletonList(annotation), collector.getFinalRepeatableAnnotations(annotation));
Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class);
- Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3));
+ Assert.assertEquals(Collections.singletonList(annotation3), collector.getFinalRepeatableAnnotations(annotation3));
Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3, Annotation3.class));
Assert.assertTrue(collector.getRepeatableAnnotations(annotation3, Annotation1.class).isEmpty());
@@ -73,13 +73,13 @@ public class RepeatableAnnotationCollectorTest {
RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.none();
Assert.assertSame(collector, RepeatableAnnotationCollector.none());
- Assert.assertEquals(0, collector.getRepeatableAnnotations(null, true).size());
+ Assert.assertEquals(0, collector.getAllRepeatableAnnotations(null).size());
Annotation1 annotation = Foo.class.getAnnotation(Annotation1.class);
- Assert.assertEquals(Collections.singletonList(annotation), collector.getRepeatableAnnotations(annotation, true));
+ Assert.assertEquals(Collections.singletonList(annotation), collector.getAllRepeatableAnnotations(annotation));
Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class);
- Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3, true));
+ Assert.assertEquals(Collections.singletonList(annotation3), collector.getAllRepeatableAnnotations(annotation3));
}
@Test
@@ -92,11 +92,11 @@ public class RepeatableAnnotationCollectorTest {
.map(Annotation2::value)
.flatMap(Stream::of)
.collect(Collectors.toList());
- Assert.assertEquals(annotations, collector.getRepeatableAnnotations(annotation));
- Assert.assertEquals(ANNOTATION3S, collector.getRepeatableAnnotations(ANNOTATION1));
+ Assert.assertEquals(annotations, collector.getFinalRepeatableAnnotations(annotation));
+ Assert.assertEquals(ANNOTATION3S, collector.getFinalRepeatableAnnotations(ANNOTATION1));
Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class);
- Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3));
+ Assert.assertEquals(Collections.singletonList(annotation3), collector.getFinalRepeatableAnnotations(annotation3));
Assert.assertEquals(Collections.singletonList(ANNOTATION1), collector.getRepeatableAnnotations(ANNOTATION1, Annotation1.class));
Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class));
@@ -116,12 +116,12 @@ public class RepeatableAnnotationCollectorTest {
.map(Annotation2::value)
.flatMap(Stream::of)
.forEach(annotations::add);
- Assert.assertEquals(annotations, collector.getRepeatableAnnotations(annotation, true));
+ Assert.assertEquals(annotations, collector.getAllRepeatableAnnotations(annotation));
- Assert.assertEquals(ANNOTATIONS, collector.getRepeatableAnnotations(ANNOTATION1, true));
+ Assert.assertEquals(ANNOTATIONS, collector.getAllRepeatableAnnotations(ANNOTATION1));
Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class);
- Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3, true));
+ Assert.assertEquals(Collections.singletonList(annotation3), collector.getAllRepeatableAnnotations(annotation3));
}
@Test
@@ -132,12 +132,12 @@ public class RepeatableAnnotationCollectorTest {
.map(Annotation2::value)
.flatMap(Stream::of)
.collect(Collectors.toList());
- Assert.assertEquals(annotations, collector.getRepeatableAnnotations(annotation));
+ Assert.assertEquals(annotations, collector.getFinalRepeatableAnnotations(annotation));
- Assert.assertEquals(ANNOTATION3S, collector.getRepeatableAnnotations(ANNOTATION1));
+ Assert.assertEquals(ANNOTATION3S, collector.getFinalRepeatableAnnotations(ANNOTATION1));
Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class);
- Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3));
+ Assert.assertEquals(Collections.singletonList(annotation3), collector.getFinalRepeatableAnnotations(annotation3));
Assert.assertEquals(Collections.singletonList(ANNOTATION1), collector.getRepeatableAnnotations(ANNOTATION1, Annotation1.class));
Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class));
@@ -156,11 +156,11 @@ public class RepeatableAnnotationCollectorTest {
.map(Annotation2::value)
.flatMap(Stream::of)
.forEach(annotations::add);
- Assert.assertEquals(annotations, collector.getRepeatableAnnotations(annotation, true));
- Assert.assertEquals(ANNOTATIONS, collector.getRepeatableAnnotations(ANNOTATION1, true));
+ Assert.assertEquals(annotations, collector.getAllRepeatableAnnotations(annotation));
+ Assert.assertEquals(ANNOTATIONS, collector.getAllRepeatableAnnotations(ANNOTATION1));
Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class);
- Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3, true));
+ Assert.assertEquals(Collections.singletonList(annotation3), collector.getAllRepeatableAnnotations((annotation3)));
}
@Test
@@ -168,10 +168,10 @@ public class RepeatableAnnotationCollectorTest {
RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.full();
Assert.assertSame(collector, RepeatableAnnotationCollector.full());
- Assert.assertEquals(ANNOTATION3S, collector.getRepeatableAnnotations(ANNOTATION1));
+ Assert.assertEquals(ANNOTATION3S, collector.getFinalRepeatableAnnotations(ANNOTATION1));
Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class);
- Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3));
+ Assert.assertEquals(Collections.singletonList(annotation3), collector.getFinalRepeatableAnnotations(annotation3));
Assert.assertEquals(Collections.singletonList(ANNOTATION1), collector.getRepeatableAnnotations(ANNOTATION1, Annotation1.class));
Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class));
@@ -183,10 +183,10 @@ public class RepeatableAnnotationCollectorTest {
RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.full();
Assert.assertSame(collector, RepeatableAnnotationCollector.full());
- Assert.assertEquals(ANNOTATIONS, collector.getRepeatableAnnotations(ANNOTATION1, true));
+ Assert.assertEquals(ANNOTATIONS, collector.getAllRepeatableAnnotations(ANNOTATION1));
Annotation3 annotation3 = Foo.class.getAnnotation(Annotation3.class);
- Assert.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3, true));
+ Assert.assertEquals(Collections.singletonList(annotation3), collector.getAllRepeatableAnnotations(annotation3));
}
@Target(ElementType.TYPE_USE)