Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding annotations with Java Annotation Processor

I know that the Annotation Processor is normally used to consume annotations and react to them. I, however, have a use case where this "reaction" involves adding other annotations. Can this be done within the processor itself? If so, how?

like image 254
Eric Avatar asked Nov 05 '22 19:11

Eric


1 Answers

The short answer is yes, and you don't have anything specific to do.

An annotation processor is used to create new source files, not to modify existing ones. So when you say "adding other annotations", I guess you mean "creating new classes that hold annotations".

Annotation processing is done in rounds. At each round, the process method of your annotation processor is called.

If a processor generate new source files, another round of annotation processing starts

  • newly generated source files are parsed and annotations are processed as before
  • processors invoked on previous rounds are also invoked on all subsequent rounds

So basically : you have nothing to do, it already works ;-) .

like image 69
Pierre-Yves Ricau Avatar answered Nov 09 '22 04:11

Pierre-Yves Ricau