Skip to content

Commit 3bafcf1

Browse files
committed
Refactor findMetaAnnotations to use collector
Replace the previous boolean/list-returning recursion with a collector-based approach: build a LinkedList, recurse into annotation types to accumulate matching meta-annotations, and return an unmodifiable list. Import newLinkedList and remove unused isNotEmpty. Update tests to use a new assertFindMetaAnnotation helper and adjust assertions accordingly. This simplifies recursion and ensures all matching meta-annotations are collected in a single list.
1 parent 38db859 commit 3bafcf1

2 files changed

Lines changed: 28 additions & 13 deletions

File tree

microsphere-java-core/src/main/java/io/microsphere/util/AnnotationUtils.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@
3838
import java.util.function.Predicate;
3939

4040
import static io.microsphere.collection.CollectionUtils.isEmpty;
41-
import static io.microsphere.collection.CollectionUtils.isNotEmpty;
4241
import static io.microsphere.collection.ListUtils.first;
42+
import static io.microsphere.collection.ListUtils.newLinkedList;
4343
import static io.microsphere.collection.Lists.ofList;
4444
import static io.microsphere.collection.MapUtils.immutableEntry;
4545
import static io.microsphere.collection.MapUtils.toFixedMap;
@@ -1761,9 +1761,9 @@ public static boolean isNativeAnnotationType(Class<? extends Annotation> annotat
17611761
* <p>If either the annotated element or the meta-annotation type is {@code null},
17621762
* this method will return {@code null}.</p>
17631763
*
1764-
* @param annotatedElement the element to search for meta-annotations on
1764+
* @param annotatedElement the element to search for meta-annotations on
17651765
* @param metaAnnotationType the type of meta-annotation to look for
1766-
* @param <A> the type of the meta-annotation to find
1766+
* @param <A> the type of the meta-annotation to find
17671767
* @return the first matching meta-annotation of the specified type, or {@code null} if none is found
17681768
*/
17691769
@Nullable
@@ -1822,23 +1822,32 @@ public static <A extends Annotation> A findMetaAnnotation(AnnotatedElement annot
18221822
* <p>If either the annotated element or the meta-annotation type is {@code null},
18231823
* this method will return an empty list.</p>
18241824
*
1825-
* @param annotatedElement the element to search for meta-annotations on
1825+
* @param annotatedElement the element to search for meta-annotations on
18261826
* @param metaAnnotationType the type of meta-annotation to look for
1827-
* @param <A> the type of the meta-annotation to find
1827+
* @param <A> the type of the meta-annotation to find
18281828
* @return a read-only list of all matching meta-annotations of the specified type, never {@code null}
18291829
*/
18301830
@Nonnull
18311831
@Immutable
18321832
public static <A extends Annotation> List<A> findMetaAnnotations(AnnotatedElement annotatedElement, Class<A> metaAnnotationType) {
1833-
return (List<A>) findDeclaredAnnotations(annotatedElement, annotation -> {
1833+
List<A> annotations = newLinkedList();
1834+
findMetaAnnotations(annotatedElement, metaAnnotationType, annotations);
1835+
return unmodifiableList(annotations);
1836+
}
1837+
1838+
static <A extends Annotation> void findMetaAnnotations(AnnotatedElement annotatedElement, Class<A> metaAnnotationType, List<A> annotations) {
1839+
findDeclaredAnnotations(annotatedElement, annotation -> {
18341840
Class<? extends Annotation> annotationType = annotation.annotationType();
18351841
if (isNativeAnnotationType(annotationType)) {
18361842
return false;
18371843
}
18381844
if (annotationType.equals(metaAnnotationType)) {
1845+
annotations.add((A) annotation);
18391846
return true;
18401847
}
1841-
return isNotEmpty(findMetaAnnotations(annotationType, metaAnnotationType));
1848+
// Recursively find meta-annotations on the annotation type
1849+
findMetaAnnotations(annotationType, metaAnnotationType, annotations);
1850+
return false;
18421851
});
18431852
}
18441853

microsphere-java-core/src/test/java/io/microsphere/util/AnnotationUtilsTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -631,14 +631,14 @@ void testIsCallerSensitivePresent() {
631631

632632
@Test
633633
void testFindMetaAnnotation() {
634-
assertNotNull(findMetaAnnotation(A.class, Monitored.class));
635-
assertNotNull(findMetaAnnotation(B.class, Monitored.class));
634+
assertFindMetaAnnotation(A.class, Monitored.class);
635+
assertFindMetaAnnotation(B.class, Monitored.class);
636636

637-
assertNotNull(findMetaAnnotation(A.class, ServiceMode.class));
638-
assertNotNull(findMetaAnnotation(B.class, ServiceMode.class));
637+
assertFindMetaAnnotation(A.class, ServiceMode.class);
638+
assertFindMetaAnnotation(B.class, ServiceMode.class);
639639

640-
assertNotNull(findMetaAnnotation(A.class, Template.class));
641-
assertNotNull(findMetaAnnotation(B.class, Template.class));
640+
assertFindMetaAnnotation(A.class, Template.class);
641+
assertFindMetaAnnotation(B.class, Template.class);
642642

643643
assertNull(findMetaAnnotation(AnnotationUtils.class, Monitored.class));
644644
assertNull(findMetaAnnotation(AnnotationUtils.class, ServiceMode.class));
@@ -657,6 +657,12 @@ void testFindMetaAnnotations() {
657657
assertFindMetaAnnotations(B.class, Template.class);
658658
}
659659

660+
private void assertFindMetaAnnotation(AnnotatedElement annotatedElement, Class<? extends Annotation> metaAnnotationType) {
661+
Annotation metaAnnotation = findMetaAnnotation(annotatedElement, metaAnnotationType);
662+
assertNotNull(metaAnnotation);
663+
assertSame(metaAnnotationType, metaAnnotation.annotationType());
664+
}
665+
660666
private void assertFindMetaAnnotations(AnnotatedElement annotatedElement, Class<? extends Annotation> metaAnnotationType) {
661667
List<? extends Annotation> metaAnnotations = findMetaAnnotations(annotatedElement, metaAnnotationType);
662668
assertEquals(1, metaAnnotations.size());

0 commit comments

Comments
 (0)