简体   繁体   English

Java注释处理:我怎么知道一轮是最后一轮?

[英]Java annotation processing: how do I know if a round is the last one?

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. 扩展AbstractProcessor ,有可能覆盖init(...) ,但没有“对立”方法,在处理完所有轮次后将调用它。

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? 有任何想法吗?

The Processor.process method includes an argument of type RoundEnvironment . Processor.process方法包含RoundEnvironment类型的参数。 Perhaps the RoundEnvironment.processingOver method can be of help. 也许RoundEnvironment.processingOver方法可以提供帮助。

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. 这些初始输入可以被视为虚拟第0轮处理的输出。 [...] [...]

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). 因为在我的使用情况下,我要么不产生任何新的类文件,或者我产生他们,但并不需要对它们进行处理 ,应该足够多的只是“计数”轮,只有在第一个做实际工作(和做清理工作,例如关闭文件,结束时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM