Java: Messager 与运行时异常

Java: Messager vs runtime exception

在注释处理中,我正在寻找有关何时使用 MessagerDiagnostic.Kind.ERROR 以及何时使用运行时异常的一般指南。我知道使用 Messager 会在环境中引发错误。

让我们考虑这个例子。我有一个放在接口之上的注释。它的处理器的工作是确保其中的所有方法都具有特定类型的唯一标识符。如果处理器遇到违规,它应该使用 Messager,还是应该抛出运行时异常?

Messager 的一个缺点是,如果该违规导致程序和其他处理器出现其他问题,则错误消息会在其他错误消息中丢失并污染日志记录。

我认为 Processor 文档中的这一段是一个很好的通用指南。

If a processor throws an uncaught exception, the tool may cease other active annotation processors. If a processor raises an error, the current round will run to completion and the subsequent round will indicate an error was raised. Since annotation processors are run in a cooperative environment, a processor should throw an uncaught exception only in situations where no error recovery or reporting is feasible.