An annotation processor will provide you with two TypeElement, one for the annotated object and one the annotation itself.
Let’s assume one needs to know the Class type of the annotated object during annotation processing to generate code. One can extract the qualified name with TypeElement.getQualifiedName(). However, any attempt to use Class.forName(fullyQualifiedName) throws a ClassNotFoundException.
It probably means the class is not on the path of the annotation processing code. Is it ever possible to retrieve such a class during annotation processing without having to put all annotated code in the classpath of the processing library?
A workaround is to generate something like:
Class c = Class.forName("thefullyqualifiedname");
and use c in the generated code instead, but it is not clean.
The best workaround I could come up with is the following:
which produces:
It does not give explicit access to the class type of the annotated class, but at least, it can be generated in the code.