smallrye / jandex

Java Annotation Indexer

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

IndexView - add method/s to handle repeatable annotations

mkouba opened this issue · comments

org.jboss.jandex.IndexView.getAnnotations(DotName) currently does not return the instances of repeatable annotations and that's ok (repeatable annotations are compiled into the container annotations anyway). However, we should probably add some convenient method to handle repeatable annotations as well, ie. something similar to java.lang.reflect.AnnotatedElement.getAnnotationsByType(Class<T>). Of course, we can't modify the behavior of IndexView.getAnnotations() due to backwards compatibility.

Example:

@Repeatable(Routes.class)
public @interface Route {

    @interface Routes {
        Route[] value();
    }
}

// the boolean param can be used to specify whether to return repeatable annotations or not  
Collection<AnnotationInstance> annotations = index.getAnnotations(DotName.createSimple(Route.class.getName()), true); 

+1 to handling this automatically. I think we should be able to just ignore the container annotation at index-time if we know it's a container annotation and make the API return the right thing for index.getAnnotations(ROUTE_DOTNAME) as well as index.getAnnotatedClasses(ROUTE_DOTNAME).

If not, we need a new API for both use-cases (getting annotations from an annotated element, and getting the annotated element from an annotation name).

So while playing with a POC I've just realized that if we add IndexView.getAnnotations(DotName, IndexView) that would handle repeatable annotations we would have to copy the annotation instances and set the target from the container because nested AnnotationInstances return null from the org.jboss.jandex.AnnotationInstance.target() and thus would be unusable. @n1hility is that OK?