简体   繁体   English

在Spring中以编程方式解析AliasFor批注值

[英]Resolving AliasFor annotation value programatically in Spring

I have an annotation 我有一个注释

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface A {
    Class<?> value();
}

and another annotation that uses @AliasFor 和另一个使用@AliasFor注释

@A (Void.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface B {

    @AliasFor(annotation = A.class)
    Class<?> value();
}

which is used on class 在课堂上使用

@B(D.class)
public class C implements D {
}

If I have an instance of C , how can I programatically resolve A.value() to Class<D> ? 如果我有C的实例,如何以编程方式将A.value()解析为Class<D> I'm trying to synthesise the annotation with AnnotationUtils but when I retrieve the value I'm constantly getting Class<Void> . 我正在尝试使用AnnotationUtils综合注释,但是当我检索值时,我一直在获取Class<Void>

After some digging around, the answer is that I've used the wrong class. 经过一番挖掘后,答案是我使用了错误的课程。 Instead of AnnotationUtils it can be done with AnnotatedElementUtils : 可以使用AnnotatedElementUtils代替AnnotationUtils

@Test
public void shouldFindAliasedValue() {

    Class<?> actual = AnnotatedElementUtils.findMergedAnnotation(C.class, A.class).value();

    then(actual).isEqualTo(D.class);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM