添加@MirrorFor和@AliasFor注解

This commit is contained in:
huangchengxing
2022-07-13 22:31:48 +08:00
parent e4a7576d7f
commit 491c53e7dd
19 changed files with 505 additions and 91 deletions

View File

@@ -1,5 +1,6 @@
package cn.hutool.core.annotation;
import cn.hutool.core.util.ReflectUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -7,6 +8,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
/**
* 合成注解{@link SyntheticMetaAnnotation}的测试用例
@@ -53,6 +55,15 @@ public class SyntheticMetaAnnotationTest {
Assert.assertThrows(IllegalArgumentException.class, () -> new SyntheticMetaAnnotation(grandParentAnnotation));
}
@Test
public void linkTest() {
Method method = ReflectUtil.getMethod(AnnotationForLinkTest.class, "value");
SyntheticAnnotation syntheticAnnotation = new SyntheticMetaAnnotation(method.getAnnotation(AliasFor.class));
Link link = syntheticAnnotation.syntheticAnnotation(Link.class);
Assert.assertEquals(AnnotationForLinkTest.class, link.annotation());
Assert.assertEquals("name", link.attribute());
}
@Test
public void mirrorAttributeTest() {
AnnotationForMirrorTest annotation = ClassForMirrorTest.class.getAnnotation(AnnotationForMirrorTest.class);
@@ -108,6 +119,31 @@ public class SyntheticMetaAnnotationTest {
Assert.assertEquals("Foo", childAnnotation.value());
}
@Test
public void aliasForAndMirrorTest() {
AnnotationForMirrorThenAliasForTest annotation = ClassForAliasForAndMirrorTest.class.getAnnotation(AnnotationForMirrorThenAliasForTest.class);
SyntheticAnnotation synthetic = new SyntheticMetaAnnotation(annotation);
MetaAnnotationForMirrorThenAliasForTest metaAnnotation = synthetic.syntheticAnnotation(MetaAnnotationForMirrorThenAliasForTest.class);
Assert.assertEquals("test", metaAnnotation.name());
Assert.assertEquals("test", metaAnnotation.value());
AnnotationForMirrorThenAliasForTest childAnnotation = synthetic.syntheticAnnotation(AnnotationForMirrorThenAliasForTest.class);
Assert.assertEquals("test", childAnnotation.childValue());
}
@Test
public void multiAliasForTest() {
AnnotationForMultiAliasForTest annotation = ClassForMultiAliasForTest.class.getAnnotation(AnnotationForMultiAliasForTest.class);
SyntheticAnnotation synthetic = new SyntheticMetaAnnotation(annotation);
MetaAnnotationForMultiAliasForTest1 metaAnnotation1 = synthetic.syntheticAnnotation(MetaAnnotationForMultiAliasForTest1.class);
Assert.assertEquals("test", metaAnnotation1.name());
Assert.assertEquals("test", metaAnnotation1.value1());
MetaAnnotationForMultiAliasForTest2 metaAnnotation2 = synthetic.syntheticAnnotation(MetaAnnotationForMultiAliasForTest2.class);
Assert.assertEquals("test", metaAnnotation2.value2());
AnnotationForMultiAliasForTest childAnnotation = synthetic.syntheticAnnotation(AnnotationForMultiAliasForTest.class);
Assert.assertEquals("test", childAnnotation.value3());
}
// 注解结构如下:
// AnnotatedClass -> @ChildAnnotation -> @ParentAnnotation -> @GrandParentAnnotation
// -> @GrandParentAnnotation
@@ -143,9 +179,11 @@ public class SyntheticMetaAnnotationTest {
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@interface AnnotationForMirrorTest {
@Link(attribute = "name")
//@Link(attribute = "name")
@MirrorFor(attribute = "name")
String value() default "";
@Link(attribute = "value")
//@Link(attribute = "value")
@MirrorFor(attribute = "value")
String name() default "";
}
@AnnotationForMirrorTest("Foo")
@@ -164,10 +202,9 @@ public class SyntheticMetaAnnotationTest {
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@interface AnnotationForAliasForTest {
@Link(
@AliasFor(
annotation = MetaAnnotationForAliasForTest.class,
attribute = "name",
type = RelationType.ALIAS_FOR
attribute = "name"
)
String value() default "";
}
@@ -197,4 +234,53 @@ public class SyntheticMetaAnnotationTest {
@AnnotationForceForAliasForTest("Foo")
static class ClassForForceAliasForTest2 {}
@interface AnnotationForLinkTest {
@AliasFor(attribute = "name", annotation = AnnotationForLinkTest.class)
String value() default "value";
String name() default "name";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@interface MetaAnnotationForMirrorThenAliasForTest {
@MirrorFor(attribute = "value")
String name() default "";
@MirrorFor(attribute = "name")
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@MetaAnnotationForMirrorThenAliasForTest("Meta")
@interface AnnotationForMirrorThenAliasForTest {
@AliasFor(attribute = "name", annotation = MetaAnnotationForMirrorThenAliasForTest.class)
String childValue() default "value";
}
@AnnotationForMirrorThenAliasForTest(childValue = "test")
static class ClassForAliasForAndMirrorTest{}
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@interface MetaAnnotationForMultiAliasForTest1 {
@MirrorFor(attribute = "value1")
String name() default "";
@MirrorFor(attribute = "name")
String value1() default "";
}
@MetaAnnotationForMultiAliasForTest1
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@interface MetaAnnotationForMultiAliasForTest2 {
@AliasFor(attribute = "name", annotation = MetaAnnotationForMultiAliasForTest1.class)
String value2() default "";
}
@MetaAnnotationForMultiAliasForTest2
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.TYPE })
@interface AnnotationForMultiAliasForTest {
@AliasFor(attribute = "value2", annotation = MetaAnnotationForMultiAliasForTest2.class)
String value3() default "value";
}
@AnnotationForMultiAliasForTest(value3 = "test")
static class ClassForMultiAliasForTest{}
}