13

When extending AbstractProcessor, there is a possibility to override init(...), but there is no "opposite" method, which would be called after all rounds were processed.

This is a problem: when you have to append information collected during each round to same file, you just can't ever close the file, because you never get to know when the last round was. So, the file is never closed and remains empty.

Using a shutdown hook doesn't work either, the hook is never called.

Any ideas?

java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103

2 Answers2

21

The Processor.process method includes an argument of type RoundEnvironment. Perhaps the RoundEnvironment.processingOver method can be of help.

kschneid
  • 5,626
  • 23
  • 31
4

It seems that my understanding of "rounds" in the context of annotation processing were wrong:

As stated here,

[...] On each round, a processor may be asked to process a subset of the annotations found on the source and class files produced by a prior round. The inputs to the first round of processing are the initial inputs to a run of the tool; these initial inputs can be regarded as the output of a virtual zeroth round of processing. [...]

Since in my use case I am either not producing any new class files, or I produce them but don't need to process them, it should be enought just to "count" rounds and do actual work only in the first one (and doing clean up work, such as closing files, at the end of it).

java.is.for.desktop
  • 10,748
  • 12
  • 69
  • 103
  • 7
    But other annotation processors may produce files that are then processed by yours. Thus you should not rely on that assumption. – Erik Hofer Aug 31 '16 at 08:19