Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the class name of a TypeElement inside and annotation processor

I'm writing an annotation processor that runs within javac. It scans annotated classes and produces a resource file that contains class names. These names will by used at runtime to be able to get the Class<?> thanks to Class.forName(String).

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

Tips: I need neither the simple name (e.g. Bar), nor the qualified name (e.g. pgk1.pkg2.Foo.Bar).

like image 919
Pierre Avatar asked Nov 14 '12 14:11

Pierre


1 Answers

The annotation processor is initialized by the compiler and receives a ProcessingEnvironment instance.

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

like image 169
Pierre Avatar answered Sep 25 '22 00:09

Pierre