0

I'm working on an annotation processor and I would pass to my class a parameter representing the annotation to manage, something like: Class<? extends @interface> annotationClass

Unfortunately this does not compile. The only way I can compile is using a raw type Class but in this way when I try something like

roundEnvironment.getElementsAnnotatedWith(annotationClass)

the returned type is an Object instead of a javax.lang.model.element.Element

public Map<ClassName, List<ElementInfo>> initClassesToGenerate(Class annotationClass, ElementKind elementKind, RoundEnvironment roundEnvironment) {

        Map<ClassName, List<ElementInfo>> result = new HashMap<>();

        // init map

        for (Element annotatedElement : roundEnvironment.getElementsAnnotatedWith(annotationClass)) {

            TypeElement typeElement = (TypeElement) annotatedElement;
            ClassName className = ClassName.get(typeElement);
            if (!result.containsKey(className)) {
                result.put(className, new ArrayList<>());
            }
        }

        return result;
}
firegloves
  • 5,581
  • 2
  • 29
  • 50

1 Answers1

2

You can use <T extends Annotation>. You can then use Class<T> to reference classes of annotations.

cameron1024
  • 9,083
  • 2
  • 16
  • 36