Java annotation processing: is it possible to access "elements" without annotated classes?

Processing Java annotations (starting with Java 6) is a very good concept as it allows you to access a lot of class and method information through an interface Element(and others).

But, unfortunately, I had to find empirically that non-annotated classes are never passed to the custom annotation handler:

warning: No SupportedAnnotationTypes annotation found on
    my.TESTProcessor, returning an empty set.

Are my conclusions true? Or can I β€œtrick” the compiler to provide information about custom annotation handlers for non-annotated classes?

+3
source share
1 answer

Fine!

This really gives me all classes, not just annotated ones:

@SupportedAnnotationTypes("*")

The specification of this annotation reads:

[...] Finally, "*" by itself represents the set of all annotation types,
including the empty set. Note that a processor should not claim "*"
unless it is actually processing all files [...] 

Tested, working !

+4
source

Source: https://habr.com/ru/post/1773486/


All Articles