3

I have an annotation

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

and another annotation that uses @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>? I'm trying to synthesise the annotation with AnnotationUtils but when I retrieve the value I'm constantly getting Class<Void>.

1 Answer 1

3

After some digging around, the answer is that I've used the wrong class. Instead of AnnotationUtils it can be done with AnnotatedElementUtils:

@Test
public void shouldFindAliasedValue() {

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

    then(actual).isEqualTo(D.class);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.