简体   繁体   English

Checker 框架 - 运行时保留

[英]Checker framework - runtime retention

So I'm trying to use checker framework (from https://checkerframework.org/ ), and I have following code:所以我正在尝试使用检查器框架(来自https://checkerframework.org/ ),我有以下代码:

import org.checkerframework.checker.index.qual.Positive;

public class Ceker {


  public static void main(String[] args) {
    double a = Double.parseDouble(args[0]);
    System.out.println(new Ceker().preconditions(a));
  }

  public int preconditions(@Positive double a){
    return (int) a;
  }

}

How I uderstood this works, is that I can run something like我如何理解这是有效的,是我可以运行类似的东西

javacheck -processor positiveness Ceker.java

and then checker will tell me that I do not have a rule to test if double a is always positive - so I go to main method and change it to:然后检查器会告诉我我没有规则来测试double a是否始终为正值 - 所以我转到 main 方法并将其更改为:

public static void main(String[] args) {
    double a = Double.parseDouble(args[0]);
    if(a<0){
      throw new IllegalArgumentException("a is not positive!");
    }
    System.out.println(new Ceker().preconditions(a));
  }

Now, my question is - can I just not add that additional checking code, and have that @Positive annotation automatically throw exception at the runtime if contract is violated?现在,我的问题是 - 如果违反合同,我可以不添加额外的检查代码,并让@Positive注释在运行时自动抛出异常吗?

In brief:简单来说:

  • Code behavior modification is outside the scope of the Checker Framework.代码行为修改超出了 Checker 框架的范围。
  • The change you proposed is usually not desirable.您提议的更改通常是不可取的。
  • A run-time check is sometimes impossible.运行时检查有时是不可能的。

Here are more details.这里有更多细节。

  1. The Checker Framework is a verification tool that works at compile time. Checker 框架是一个在编译时工作的验证工具。 It makes no changes to your code's behavior -- it just tells you whether that behavior might be wrong.它不会更改您代码的行为——它只是告诉您该行为是否可能是错误的。 You could use a tool that changes your code's behavior in the way you described, but such a tool is different than the Checker Framework.您可以使用以您描述的方式更改代码行为的工具,但这种工具与 Checker Framework 不同。

  2. Changing the code to throw an exception is desirable behavior for a public API method, such as main : it gives a user-friendly error message.更改代码以抛出异常是公共 API 方法的理想行为,例如main :它提供用户友好的错误消息。 However, for most methods in your code, the change does no good: it merely changes one exception that crashes your program into a different exception that crashes your program.但是,对于代码中的大多数方法,这种更改没有任何好处:它只是将一个使程序崩溃的异常更改为另一个使程序崩溃的异常。 (It can be a debugging benefit for the exception to be thrown earlier, but that is no consolation to users.) The goal of the Checker Framework is to prevent errors, not to change the way that they manifest. (提前抛出异常可能对调试有好处,但这对用户来说并不是安慰。)Checker Framework 的目标是防止错误,而不是改变它们的表现方式。

  3. For many type systems, there is no possible run-time test.对于许多类型系统,没有可能的运行时测试。 Section Run-time tests and type refinement in the Checker Framework Manual gives a dozen examples. Checker 框架手册中的运行时测试和类型优化部分给出了十几个例子。

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

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