Pierre
Pierre

Reputation: 565

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).

Upvotes: 6

Views: 3931

Answers (1)

Pierre
Pierre

Reputation: 565

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.

Upvotes: 9

Related Questions