Closing Filer when compilation finishes
I'm writing an annotation processor that needs to write a text file containing the name of every class it encounters with a certain annotation. To do this, I need to keep a text file open over the entire run of my annotation processing. Every time process() is called, there is the potential to need to make new entries. Thus I cannot call close() on my print stream when the process() call is done. However, I do need to call close() before apt exists or I risk not having all the data I wrote flushed to the file.
What can I do to open my text file only once, but be able to append entries to it over the entire length of the compilation's run?
You gave the solution to your problem yourself, you just missed it.Call flush() after each write, at the end all your data will be there!Todd_Musheno@yahoo.com
[snip]
> What can I do to open my text file only once, but be
> able to append entries to it over the entire length
> of the compilation's run?
The apt AnnotationProcessorEnvironment.addListener API is intended to support the functionality you describe. You register your processor as a listener, then inspect the RoundState object when handling the RoundCompleteEvent. If finalRound() is true, you should close the file, etc.
To use this mechanism, you need to re-register the processor on each round.
JSR 269 handles this situation more cleanly by using a slightly different round model with an additional cleanup round.