spring:解决findMergedRepeatableAnnotations获取可重复的元注解(meta-annotation)结果不正确问题

spring-core的注解工具提供的方法 AnnotatedElementUtils.findMergedRepeatableAnnotations用于从AnnotatedElement 对象获取可重复的注解。但如果注解本身也是可以定义在其他注解之上的元注解(meta-annotation),且该注解也是可重复注解。这个方法就可能会失效。这就是我最近在使用spring注解工具时遇到的问题。示例如下。

注解定义

先定义一组注解:

java 复制代码
	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
	@Repeatable(LevelAs.class)
	public @interface LevelA {
		String value();
	}
	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.METHOD })
	public @interface LevelAs {
		LevelA[] value();
	}

	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.ANNOTATION_TYPE,ElementType.METHOD })
	@LevelA("shadows")
	@Repeatable(LevelBs.class)
	public @interface LevelB {
		@AliasFor(annotation = LevelA.class,attribute = "value")
		String value();
	}
	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.METHOD })
	public @interface LevelBs {
		LevelB[] value();
	}

	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.METHOD })
	@LevelB("shadows")
	public @interface LevelC {
		@AliasFor(annotation = LevelB.class,attribute = "value")
		String value();
	}

上面定义了@LevalA,@LevalB,@LevalC三个注解,其中@LeveA,@LevelB是可以重复注解(参见 @LevelAs,@LevelBs),而且还是元注解。@LeveA@LevelB的元注解,@LevelB@LevelC的元注解。它们三个层级关系是这样的。

cpp 复制代码
@LevelA				可重复 @LevelAs
	└─@LevelB		可重复 @LevelBs
		└─@LevelC

如下定义了测试这些注解的类

java 复制代码
	public static class TestClass{
		@LevelB("hello")
		@LevelB("world")
		@LevelA("top1")
		@LevelA("top2")
		@LevelC("jerry")
		public void foo() {};
		@LevelB("hello")
		@LevelA("top1")
		@LevelA("top2")
		@LevelC("jerry")
		public void tar() {};
	}

findMergedRepeatableAnnotations

如下调用AnnotatedElementUtils.findMergedRepeatableAnnotations方法读取TestClass.tar上定义的所有@LevelA注解

java 复制代码
	@Test
	public void test1() {
		try {
			Method method = TestClass.class.getMethod("tar");
			Set<LevelA> set = AnnotatedElementUtils.findMergedRepeatableAnnotations(method,LevelA.class);
			set.stream().forEach(a->{
				System.out.printf("%s\n", a);
			});
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}

确实可以获取正确的结果:

复制代码
@RepeatableAnnotTest$LevelA(value=top1)
@RepeatableAnnotTest$LevelA(value=top2)
@RepeatableAnnotTest$LevelA(value=hello)
@RepeatableAnnotTest$LevelA(value=jerry)

那是因为TestClass.tar上只定义了一个@LevelB注解,如果上面的测试代码只是把方法名换为定义了多个@LevelB的方法foo,结果就是这样的:

复制代码
@RepeatableAnnotTest$LevelA(value=top1)
@RepeatableAnnotTest$LevelA(value=top2)
@RepeatableAnnotTest$LevelA(value=jerry)

因为这里foo方法定义了超过一个@LevelB注解,实际定义在foo上的注解就成了@LeveBs({@LevelB("hello"),@LevelB("world")}),就导致AnnotatedElementUtils.findMergedRepeatableAnnotations方法不能正确获取。

分析AnnotatedElementUtils的源码,找到findMergedRepeatableAnnotations方法实际的核心执行代码如下:

java 复制代码
RepeatableContainers repeatableContainers = RepeatableContainers.of(annotationType, containerType);
return MergedAnnotations.from(element, SearchStrategy.TYPE_HIERARCHY, repeatableContainers);

RepeatableContainers

进一查看RepeatableContainers的代码发现它还有另一个方法standardRepeatables()

创建一个使用Java的@Repeatable注释进行搜索的RepeatableContainers实例。

java 复制代码
	/**
	 * Create a {@link RepeatableContainers} instance that searches using Java's
	 * {@link Repeatable @Repeatable} annotation.
	 * @return a {@link RepeatableContainers} instance
	 */
	public static RepeatableContainers standardRepeatables() {
		return StandardRepeatableContainers.INSTANCE;
	}

于是照着上面的代码将测试代码修改如下:

java 复制代码
	@Test
	public void test3() {
		try {
			Method method = TestClass.class.getMethod("foo");
			RepeatableContainers repeatableContainers = RepeatableContainers.standardRepeatables();
			MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY, repeatableContainers)
					.stream(LevelA.class)
					.forEach(a->{
						System.out.printf("%s\n", a.synthesize());
					});
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}

则结果正确:

复制代码
@LevelA(value=top1)
@LevelA(value=top2)
@LevelA(value=hello)
@LevelA(value=world)
@LevelA(value=jerry)

完整测试代码

java 复制代码
import static org.junit.Assert.*;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Set;
import org.junit.Test;
import org.springframework.core.annotation.AliasFor;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import org.springframework.core.annotation.RepeatableContainers;


public class RepeatableAnnotTest {
	@Test
	public void test1() {
		try {
			Method method = TestClass.class.getMethod("foo");
			Set<LevelA> set = AnnotatedElementUtils.findMergedRepeatableAnnotations(method,LevelA.class);
			set.stream().forEach(a->{
				System.out.printf("%s\n", a);
			});
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}
	@Test
	public void test2() {
		try {
			Method method = TestClass.class.getMethod("foo");
			RepeatableContainers repeatableContainers = RepeatableContainers.of(LevelA.class,null);
			MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY,
					repeatableContainers) 
					.stream(LevelA.class) 
					/* .stream() .sorted((o1,o2)->Integer.compare(o1.getDistance(), o2.getDistance()))*/
					.forEach(a->{
						System.out.printf("%s\n", a.synthesize());
					});
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void test3() {
		try {
			Method method = TestClass.class.getMethod("foo");
			RepeatableContainers repeatableContainers = RepeatableContainers.standardRepeatables();
			MergedAnnotations.from(method, SearchStrategy.TYPE_HIERARCHY, repeatableContainers)
					.stream(LevelA.class)
					.forEach(a->{
						System.out.printf("%s\n", a.synthesize());
					});
		} catch (Throwable e) {
			e.printStackTrace();
			fail();
		}
	}

	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.ANNOTATION_TYPE, ElementType.METHOD })
	@Repeatable(LevelAs.class)
	public @interface LevelA {
		String value();
	}
	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.METHOD })
	public @interface LevelAs {
		LevelA[] value();
	}

	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.ANNOTATION_TYPE,ElementType.METHOD })
	@LevelA("shadows")
	@Repeatable(LevelBs.class)
	public @interface LevelB {
		@AliasFor(annotation = LevelA.class,attribute = "value")
		String value();
	}
	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.METHOD })
	public @interface LevelBs {
		LevelB[] value();
	}

	@Retention(RetentionPolicy.RUNTIME)
	@Target({ ElementType.METHOD })
	@LevelB("shadows")
	public @interface LevelC {
		@AliasFor(annotation = LevelB.class,attribute = "value")
		String value();
	}
	public static class TestClass{
		@LevelB("hello")
		@LevelB("world")
		@LevelA("top1")
		@LevelA("top2")
		@LevelC("jerry")
		public void foo() {};
		@LevelB("hello")
		@LevelA("top1")
		@LevelA("top2")
		@LevelC("jerry")
		public void tar() {};
	}
}
相关推荐
海棠一号9 分钟前
JAVA理论第五章-JVM
java·开发语言·jvm
eternal__day26 分钟前
Spring Cloud 多机部署与负载均衡实战详解
java·spring boot·后端·spring cloud·负载均衡
颜淡慕潇30 分钟前
Redis 实现分布式锁:深入剖析与最佳实践(含Java实现)
java·redis·分布式
程序员秘密基地36 分钟前
基于vscode,idea,java,html,css,vue,echart,maven,springboot,mysql数据库,在线考试系统
java·vue.js·spring boot·spring·web app
何中应37 分钟前
【设计模式-5】设计模式的总结
java·后端·设计模式
吾日三省吾码1 小时前
Spring 团队详解:AOT 缓存实践、JSpecify 空指针安全与支持策略升级
java·spring·缓存
风象南1 小时前
SpringBoot的5种日志输出规范策略
java·spring boot·后端
咖啡啡不加糖1 小时前
深入理解MySQL死锁:从原理、案例到解决方案
java·数据库·mysql
zimoyin1 小时前
Compose Multiplatform 实现自定义的系统托盘,解决托盘乱码问题
java
啾啾Fun2 小时前
【Java微服务组件】分布式协调P4-一文打通Redisson:从API实战到分布式锁核心源码剖析
java·redis·分布式·微服务·lua·redisson