Get the class name of the TypeElement element inside and the annotation processor

I am writing an annotation processor that works in Java. It scans annotated classes and creates a resource file containing class names. These names will be used at runtime to be able to get Class<?> Thanks to Class.forName(String) .

How to get class name (e.g. pgk1.pkg2.Foo $ Bar) from javax.lang.model.element.TypeElement .

Tips: I do not need a simple name (for example, Bar), or a qualified name (for example, pgk1.pkg2.Foo.Bar).

+5
source share
1 answer

An annotation processor is initialized by the compiler and receives an instance of ProcessingEnvironment .

The Name ProcessingEnvironment.getElementUtils().getBinaryName(TypeElement) returns a binary name that can be used to instantiate the class later.

+7
source

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


All Articles