调整方法名称

This commit is contained in:
huangchengxing 2022-09-21 19:06:24 +08:00
parent de06d8777c
commit 84db7222f0
3 changed files with 77 additions and 40 deletions

View File

@ -32,9 +32,20 @@ public interface RepeatableAnnotationCollector {
} }
/** /**
* 当注解中有且仅有一个名为{@code value}的属性时 * <p>当注解中有且仅有一个名为{@code value}的属性时
* 若该属性类型为注解数组且该数组对应的注解类型被{@link Repeatable}注解 * 若该属性类型为注解数组且该数组对应的注解类型被{@link Repeatable}注解
* 则收集器将返回该属性中包括的可重复注解 * 则收集器将返回该属性中包括的可重复注解<br>
* eg:
* <pre><code>
* // 容器注解
* {@literal @}interface Annotation {
* Item[] value() default {};
* }
* // 可重复注解
* {@literal @}Repeatable(Annotation.class)
* {@literal @}interface Item {}
* </code></pre>
* 解析任意{@code Annotation}注解对象则可以获得{@code value}属性中的{@code Item}注解对象
* *
* @return {@link RepeatableAnnotationCollector}实例 * @return {@link RepeatableAnnotationCollector}实例
* @see Standard * @see Standard
@ -55,9 +66,19 @@ public interface RepeatableAnnotationCollector {
} }
/** /**
* 当注解中存在有属性为注解数组且该数组对应的注解类型被{@link Repeatable}注解时 * <p>当注解中存在有属性为注解数组且该数组对应的注解类型被{@link Repeatable}注解时
* 认为该属性包含可重复注解<br> * 认为该属性包含可重复注解<br>
* 收集器将返回所有符合上述条件的属性中的可重复注解 * 收集器将返回所有符合上述条件的属性中的可重复注解<br>
* eg:
* <pre><code>
* {@literal @}interface Annotation {
* Item1[] items1() default {};
* Item2[] items2() default {};
* }
* </code></pre>
* 解析任意{@code Annotation}注解对象
* 则可以获得{@code items1}属性中的{@code Item1}注解对象
* 以及{@code items2}属性中的{@code Item2}注解对象
* *
* @return {@link RepeatableAnnotationCollector}实例 * @return {@link RepeatableAnnotationCollector}实例
*/ */
@ -67,26 +88,39 @@ public interface RepeatableAnnotationCollector {
/** /**
* <p>若一个注解是可重复注解的容器注解则尝试通过其属性获得获得包含的注解对象 * <p>若一个注解是可重复注解的容器注解则尝试通过其属性获得获得包含的注解对象
* 若包含的注解对象也是可重复注解的容器注解则继续解析直到获得所有非容器注解为止 * 若包含的注解对象也是可重复注解的容器注解则继续解析直到获得所有非容器注解为止<br>
* eg:
* 若存在嵌套关系{@code a -> b -> c}
* 则解析注解<em>a</em>则将得到全部<em>c</em>注解<br>
* 如果注解不包含可重复注解则返回<em>a</em>本身
* *
* @param annotation 容器注解 * @param annotation 容器注解
* @return 容器注解中的可重复注解{@code annotation}不为容器注解则数组中仅有其本身一个对象 * @return 容器注解中的可重复注解{@code annotation}不为容器注解则数组中仅有其本身一个对象
*/ */
List<Annotation> getRepeatableAnnotations(final Annotation annotation); List<Annotation> getFinalRepeatableAnnotations(final Annotation annotation);
/** /**
* <p>若一个注解是可重复注解的容器注解则尝试通过其属性获得获得包含的注解对象 * <p>若一个注解是可重复注解的容器注解则尝试通过其属性获得获得包含的注解对象
* 若包含的注解对象也是可重复注解的容器注解则继续解析直到获得所有非容器注解为止<br> * 若包含的注解对象也是可重复注解的容器注解则继续解析直到获得所有非容器注解为止<br>
* {@code accumulate}{@code true}返回结果为全量的注解 * eg:
* 若存在嵌套关系{@code a -> b -> c}则解析注解<em>a</em>,
* 将获得全部<em>a</em><em>b</em><em>c</em>注解<br>
* 如果注解不包含可重复注解则返回<em>a</em>本身
* *
* @param annotation 容器注解 * @param annotation 容器注解
* @param accumulate 是否累加
* @return 容器注解中的可重复注解{@code annotation}不为容器注解则数组中仅有其本身一个对象 * @return 容器注解中的可重复注解{@code annotation}不为容器注解则数组中仅有其本身一个对象
*/ */
List<Annotation> getRepeatableAnnotations(final Annotation annotation, final boolean accumulate); List<Annotation> getAllRepeatableAnnotations(final Annotation annotation);
/** /**
* 若一个注解是可重复注解的容器注解则尝试通过其属性获得获得包含的指定类型注解对象 * <p>若一个注解是可重复注解的容器注解则尝试通过其属性获得获得包含的指定类型注解对象<br>
* eg:
* 若存在嵌套关系{@code a -> b -> c}
* <ul>
* <li>解析注解<em>a</em>可获得<em>a</em><em>b</em><em>c</em></li>
* <li>解析注解<em>b</em>可获得<em>b</em><em>c</em></li>
* <li>解析注解<em>c</em>只可获得<em>c</em></li>
* </ul>
* *
* @param annotation 容器注解 * @param annotation 容器注解
* @param annotationType 注解类型 * @param annotationType 注解类型
@ -112,7 +146,7 @@ public interface RepeatableAnnotationCollector {
* @return 空集合 * @return 空集合
*/ */
@Override @Override
public List<Annotation> getRepeatableAnnotations(final Annotation annotation) { public List<Annotation> getFinalRepeatableAnnotations(final Annotation annotation) {
return Objects.isNull(annotation) ? return Objects.isNull(annotation) ?
Collections.emptyList() : Collections.singletonList(annotation); Collections.emptyList() : Collections.singletonList(annotation);
} }
@ -124,7 +158,7 @@ public interface RepeatableAnnotationCollector {
* @return 空集合 * @return 空集合
*/ */
@Override @Override
public List<Annotation> getRepeatableAnnotations(final Annotation annotation, final boolean accumulate) { public List<Annotation> getAllRepeatableAnnotations(final Annotation annotation) {
return Objects.isNull(annotation) ? return Objects.isNull(annotation) ?
Collections.emptyList() : Collections.singletonList(annotation); Collections.emptyList() : Collections.singletonList(annotation);
} }
@ -159,22 +193,25 @@ public interface RepeatableAnnotationCollector {
* @return 容器注解中的可重复注解{@code annotation}不为容器注解则数组中仅有其本身一个对象 * @return 容器注解中的可重复注解{@code annotation}不为容器注解则数组中仅有其本身一个对象
*/ */
@Override @Override
public final List<Annotation> getRepeatableAnnotations(final Annotation annotation) { public final List<Annotation> getFinalRepeatableAnnotations(final Annotation annotation) {
return getRepeatableAnnotations(annotation, false); return find(annotation, null, false);
} }
/** /**
* <p>若一个注解是可重复注解的容器注解则尝试通过其属性获得获得包含的注解对象 * <p>若一个注解是可重复注解的容器注解则尝试通过其属性获得获得包含的注解对象
* 若包含的注解对象也是可重复注解的容器注解则继续解析直到获得所有非容器注解为止<br> * 若包含的注解对象也是可重复注解的容器注解则继续解析直到获得所有非容器注解为止<br>
* {@code accumulate}{@code true}返回结果为全量的注解 * {@code accumulate}{@code true}返回结果为全量的注解
* eg:
* 若存在嵌套关系{@code a -> b -> c}则解析注解<em>a</em>,
* 将获得全部<em>a</em><em>b</em><em>c</em>注解
* 如果注解不包含可重复注解则返回其本身
* *
* @param annotation 容器注解 * @param annotation 容器注解
* @param accumulate 是否累加
* @return 容器注解中的可重复注解{@code annotation}不为容器注解则数组中仅有其本身一个对象 * @return 容器注解中的可重复注解{@code annotation}不为容器注解则数组中仅有其本身一个对象
*/ */
@Override @Override
public final List<Annotation> getRepeatableAnnotations(final Annotation annotation, final boolean accumulate) { public List<Annotation> getAllRepeatableAnnotations(Annotation annotation) {
return find(annotation, null, accumulate); return find(annotation, null, true);
} }
/** /**

View File

@ -285,7 +285,7 @@ public class RepeatableMetaAnnotatedElement<T extends AnnotationMapping<Annotati
* 若当前注解是可重复注解的容器则将其平铺后把可重复注解加入{@link #aggregations} * 若当前注解是可重复注解的容器则将其平铺后把可重复注解加入{@link #aggregations}
*/ */
private List<Aggregation> collectRepeatable(final Annotation annotation) { private List<Aggregation> collectRepeatable(final Annotation annotation) {
return repeatableCollector.getRepeatableAnnotations(annotation, true) return repeatableCollector.getAllRepeatableAnnotations(annotation)
.stream() .stream()
.map(a -> new Aggregation(a, Objects.equals(a, annotation))) .map(a -> new Aggregation(a, Objects.equals(a, annotation)))
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -55,13 +55,13 @@ public class RepeatableAnnotationCollectorTest {
RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.none(); RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.none();
Assert.assertSame(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); 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); 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.assertEquals(Collections.singletonList(annotation3), collector.getRepeatableAnnotations(annotation3, Annotation3.class));
Assert.assertTrue(collector.getRepeatableAnnotations(annotation3, Annotation1.class).isEmpty()); Assert.assertTrue(collector.getRepeatableAnnotations(annotation3, Annotation1.class).isEmpty());
@ -73,13 +73,13 @@ public class RepeatableAnnotationCollectorTest {
RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.none(); RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.none();
Assert.assertSame(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); 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); 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 @Test
@ -92,11 +92,11 @@ public class RepeatableAnnotationCollectorTest {
.map(Annotation2::value) .map(Annotation2::value)
.flatMap(Stream::of) .flatMap(Stream::of)
.collect(Collectors.toList()); .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); 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(Collections.singletonList(ANNOTATION1), collector.getRepeatableAnnotations(ANNOTATION1, Annotation1.class));
Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class)); Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class));
@ -116,12 +116,12 @@ public class RepeatableAnnotationCollectorTest {
.map(Annotation2::value) .map(Annotation2::value)
.flatMap(Stream::of) .flatMap(Stream::of)
.forEach(annotations::add); .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); 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 @Test
@ -132,12 +132,12 @@ public class RepeatableAnnotationCollectorTest {
.map(Annotation2::value) .map(Annotation2::value)
.flatMap(Stream::of) .flatMap(Stream::of)
.collect(Collectors.toList()); .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); 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(Collections.singletonList(ANNOTATION1), collector.getRepeatableAnnotations(ANNOTATION1, Annotation1.class));
Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class)); Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class));
@ -156,11 +156,11 @@ public class RepeatableAnnotationCollectorTest {
.map(Annotation2::value) .map(Annotation2::value)
.flatMap(Stream::of) .flatMap(Stream::of)
.forEach(annotations::add); .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); 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 @Test
@ -168,10 +168,10 @@ public class RepeatableAnnotationCollectorTest {
RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.full(); RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.full();
Assert.assertSame(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); 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(Collections.singletonList(ANNOTATION1), collector.getRepeatableAnnotations(ANNOTATION1, Annotation1.class));
Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class)); Assert.assertEquals(ANNOTATION2S, collector.getRepeatableAnnotations(ANNOTATION1, Annotation2.class));
@ -183,10 +183,10 @@ public class RepeatableAnnotationCollectorTest {
RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.full(); RepeatableAnnotationCollector collector = RepeatableAnnotationCollector.full();
Assert.assertSame(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); 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) @Target(ElementType.TYPE_USE)