是否有任何标准注释来指示Java中的nothrow语义? [英] Is there any standard annotation to indicate nothrow semantics in Java?

查看:72
本文介绍了是否有任何标准注释来指示Java中的nothrow语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想证明Java中的接口方法不允许传播异常,并进行某种形式的静态分析,以验证该方法的实现能够捕获并处理可能传播给它的任何异常。类似于 @NoThrow

I'd like to document an interface method in Java as not being allowed to propagate exceptions and have some sort of static analysis verifying that implementations of this method catch and handle any exceptions that may have propagated to it. Something along the lines of @NoThrow.

例如,我想写: / p>

For example, I'd like to be able to write:

interface SomeServiceProviderInterface {
   @NoThrow
   @NonNull
   SomeResult someComputation();
}

...并保证实现遵守该接口协定。是否已经有注释和静态分析工具?如果不是,有谁知道这是否可以通过注释处理器来实现(它可以查看代码是否包含try ... catch块吗?)和/或对实现此类操作有任何建议或建议?谢谢!

... and have some guarantee that implementations obey this interface contract. Is there an annotation and static analysis tool that already does this? If not, does anyone know if this is achievable with an annotation processor (can it see whether the code includes a try...catch block?) and/or have any pointers or advice on how to go about implementing such a thing? Thanks!

推荐答案

不可能有这样的注释,因为无法保证方法不会引发异常。这是因为任何方法都可能随时抛出 VirtualMachineError 。特别是,方法可能抛出 OutOfMemoryError ,即使它不直接或间接分配内存本身(使用 new 运算符)。这不仅是理论上的问题:如果垃圾回收线程花费的时间太长,一些并发的垃圾回收器会这样做。

There can be no such annotation, because it is impossible to guarantee that a method does not throw exceptions. This is because any method may throw a VirtualMachineError at anytime. In particular, a method could throw an OutOfMemoryError even if it does not directly or indirectly allocate memory itself (using a new operator). This is not a merely theoretical concern: some concurrent garbage collectors will do this if a garbage collection thread takes too long.

这篇关于是否有任何标准注释来指示Java中的nothrow语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆