Elements.getTypeElement() returns TypeElement with empty annotations

I’m working on annotation processor and in general it works as expected. I’ve successfully extracted annotated elements by calling RoundEnvironment.getElementsAnnotatedWith(). Then I extracts annotation mirrors by calling Element.getAnnotationMirrors() to process annotation parameters. I work with annotation mirrors instead of annotation directly to provide graceful error handling with the help of the messager.

The problem appears when I try to do the same, but with the element obtained via Elements util class. If I extract exactly the same element by calling Elements.getTypeElement() and then call Element.getAnnotationMirrors() it returns an empty array. It seems like element’s metadata is not initialized at all. Note, that it happens only for types that are not part of the round environment (i.e. are not processing right now)

I haven’t found any evidences in javadocs is it intended by design or not. Maybe it is just a bug. Is there a workaround?

Leave a Comment