spring-core:注解扫描工具MergedAnnotations获取复合注解上的元注解

发布于:2024-04-16 ⋅ 阅读:(35) ⋅ 点赞:(0)

上一篇博客《sping-core:注解扫描工具AnnotatedElementUtils.hasMetaAnnotationTypes方法说明》介绍了通过AnnotatedElementUtils.hasMetaAnnotationTypes方法判断指定的AnnotatedElement对象(Class,Method,Field)是否是否用复合注解类型进行注解,而该复合注解是用annotationType指定的注解作元注解的。
那么既然能判断是否存在元注解,能不能获取元注解对象呢?
AnnotatedElementUtils也提供了静态方法getMetaAnnotationTypes(AnnotatedElement element, Class<? extends Annotation> annotationType)但它返回的是元注解的类名集合Set<String>

如何获取元注解对象呢?AnnotatedElementUtils并没有提供这个方法,但是参照getMetaAnnotationTypes(AnnotatedElement element, Class<? extends Annotation> annotationType)也能自己实现:

示例如下:

	/**
	 * 返回element指定对象上所有annotationType指定类型的元注解集合
	 * @param <A>
	 * @param element
	 * @param annotationType
	 */
	public static <A extends Annotation> Set<A>  getMetaAnnotations(AnnotatedElement element,Class<A> annotationType) {
		MergedAnnotations mergedAnnotations = MergedAnnotations.from(element, 
			SearchStrategy.INHERITED_ANNOTATIONS, RepeatableContainers.none());
		return mergedAnnotations.stream(annotationType)
			.filter(MergedAnnotation::isMetaPresent)
			.map(MergedAnnotation::synthesize)
			.collect(Collectors.toSet());
	}

示例:
元注解 @CellLogTag

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE,ElementType.METHOD})
public @interface CellLogTag {
}

使用 @CellLogTag的作元注解的复合注解 @TestLogTag

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@CellLogTag
public @interface TestLogTag {
}

调用示例:

	@Test
	public void test8GetMetaAnnotationTypes() {
		try {
			{
				Method method = ClassB.class.getMethod("m0");
				Set<CellLogTag> metaAnnots = getMetaAnnotations(method,CellLogTag.class);
				assertTrue(!metaAnnots.isEmpty());
				log("{}",metaAnnots);
			}
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}