Before time ,one annotation attribute only supports one alias ,you can only do like this:
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test1 {
String test1() default "test1";
}
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test2 {
String test2() default "test2";
}
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Test1
@Test2
public @interface Test3 {
@AliasFor(annotation = Test1.class, attribute = "test1")
String test3() default "test3";
@AliasFor(annotation = Test2.class, attribute = "test2")
String test4() default "test4";
}
But now ,you can do this more easily:
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Test1
@Test2
public @interface Test3 {
@AliasFor(annotation = Test1.class, attribute = "test1")
@AliasFor(annotation = Test2.class, attribute = "test2")
String test3() default "test3";
}
Moreover, the mutual aliasing of different attributes in the same annotation can break the original limitation (although this kind of use scenario is less):
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test4 {
@AliasFor("test2")
@AliasFor("test3")
String test1() default "test";
@AliasFor("test1")
@AliasFor("test3")
String test2() default "test";
@AliasFor("test1")
@AliasFor("test2")
String test3() default "test";
}
Combine examples above, you can use @AliasFors like this:
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test5 {
@AliasFor("test2")
@AliasFor("test3")
String test1() default "test1";
@AliasFor("test1")
@AliasFor("test3")
String test2() default "test1";
@AliasFor("test1")
@AliasFor("test2")
String test3() default "test1";
}
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Test5
public @interface Test6 {
@AliasFor("test2")
@AliasFor("test3")
String test1() default "test2";
@AliasFor("test1")
@AliasFor("test3")
String test2() default "test2";
@AliasFor(annotation = Test5.class)
@AliasFor("test1")
@AliasFor("test2")
String test3() default "test2";
}
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Test6
public @interface Test7 {
@AliasFor(annotation = Test6.class)
String test3() default "test3";
}
@Test7(test3 = "override the method")
public static class Element4 {
}
@Test
public void test3() {
Test5 test5 = AnnotatedElementUtils.getMergedAnnotation(Element4.class, Test5.class);
Test6 test6 = AnnotatedElementUtils.getMergedAnnotation(Element4.class, Test6.class);
System.out.println(test5.toString());
System.out.println(test6.toString());
assertEquals("override the method", test6.test1());
assertEquals("override the method", test6.test2());
assertEquals("override the method", test6.test3());
assertEquals("override the method", test5.test1());
assertEquals("override the method", test5.test2());
assertEquals("override the method", test5.test3());
}
For more details, please read org.springframework.core.annotation.AlisforsTests
.
I have submitted the code earlier this month (attached link: https://github.com/spring-projects/spring-framework/pull/25857). I'd like to ask you masters, when is this feature expected to be available online?
Comment From: sbrannen
Please refrain from opening issues with the same content as an existing PR.
Closing this as a duplicate of #25857.