reflections icon indicating copy to clipboard operation
reflections copied to clipboard

Inconsistency with 'meta' annotations

Open sifraser opened this issue 8 years ago • 1 comments

I was hoping I would be able to use this library to work with 'meta' (aggregated) annotations in a similar to Spring (https://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/htmlsingle/#beans-meta-annotations).

At first I was hopeful as getTypesAnnotatedWith() appears to successfully match classes with an indirect/meta annotation e.g.

@Target(ElementType.TYPE)
@Retention(RUNTIME)
@interface Meta {
    String value();
}

@Meta("a")
@Retention(RUNTIME)
@interface A {}

@Meta("b")
@Retention(RUNTIME)
@interface B {}

@A class A1 {}
@B class B1 {}
@A class A2 {}

public class Annotations {
    public static void main(String[] args) {
        for (Class<?> type: new Reflections().getTypesAnnotatedWith(Meta.class)) {
            System.out.println(type.getSimpleName());
        }
    }
}

Prints:

B1
A
A1
A2
B

Which is great as A1, A2 and B1 are all matching without an explicitly declared annotation.

My problem is that although I now know that A1, A2 and B1 have an indirect Meta annotation, I cannot actually get hold of a reference to it. I have tried numerous mechanisms including with ReflectionUtils e.g.

for (Annotation annotation : ReflectionUtils.getAnnotations(A1.class)) {
    System.out.println(annotation.annotationType().getSimpleName());
}

But everything just gives the explicitly declared annotations, i.e.:

A

(By everything, I mean that I have tried everything obvious - there is probably some way to process the store directly that I have not considered.)

So effectively I have two issues:

  • There is an inconsistency on which annotations a type 'has'
  • I cannot use meta annotations without additional boilerplate to explicitly check annotations of annotations

A correct solution would probably require some explicit handling of this form of annotation aggregation.

sifraser avatar Nov 08 '17 17:11 sifraser

This is a good job! And I have a question, do we need to add a switch to enable people to distinguish between explicit and implicit annotations in case that some people need the old features and the others need the this new feature?

dota17 avatar Aug 10 '20 11:08 dota17