有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Checker框架初始化。领域。取消初始化假阳性

这是我的错误

  found   : @Initialized @Nullable String
  required: @Initialized @NonNull String
/Users/calebcushing/IdeaProjects/ppm/scaf/src/main/java/com/xenoterracide/scaf/Application.java:21: error: [initialization.fields.uninitialized] the constructor does not initialize fields: arg, args, dir
public final class Application implements Runnable {
             ^
3 errors

这些都是由picocli初始化的,所以我添加了SuppressWarnings,不确定为什么它仍在发生

  @SuppressWarnings({ "NullAway.Init", "initialization.fields.uninitialize" })
  @CommandLine.Parameters( index = "0", description = "first configuration directory" )
  private String arg;

  @SuppressWarnings({ "NullAway.Init", "initialization.fields.uninitialize" })
  @CommandLine.Parameters(
    index = "1..*",
    description = "path to configuration directories separated by space"
  )
  private List<String> args;

  @SuppressWarnings({ "NullAway.Init", "initialization.fields.uninitialize" })
  @CommandLine.Option(
    names = {"-d", "--dir"},
    defaultValue = ".config/scaf",
    showDefaultValue = CommandLine.Help.Visibility.ALWAYS,
    description = "Directory path from the current working directory. " +
      "Templates and configs are looked up relative to here"
  )
  private Path dir;

我试过

  @SuppressWarnings({ "NullAway.Init", "initialization.fields.uninitialize"})

在类、构造函数和字段上。如何让checkerframework快乐

here is the full source code检查器框架目前未在其中启用,因为如果启用,它将无法编译


共 (1) 个答案

  1. # 1 楼答案

    Checker framework正在抱怨,因为在main方法中,您没有初始化实例字段argargsdir。如果没有显式地注释字段,从checker框架的角度来看,该字段被认为是@NotNull

    请尝试用@Nullable注释该字段:

      @CommandLine.Parameters( index = "0", description = "first configuration directory" )
      private @Nullable String arg;
    
      @CommandLine.Parameters(
        index = "1..*",
        description = "path to configuration directories separated by space"
      )
      private @Nullable List<String> args;
    
      @CommandLine.Option(
        names = {"-d", " dir"},
        defaultValue = ".config/scaf",
        showDefaultValue = CommandLine.Help.Visibility.ALWAYS,
        description = "Directory path from the current working directory. " +
          "Templates and configs are looked up relative to here"
      )
      private @Nullable Path dir;
    

    虽然您将picocli配置为提供默认值,但checker framework只知道在main方法调用这些字段之后,这些字段没有初始化,没有这样的初始化代码,picocli将提供您指定的默认值,但checker framework不知道,这就是为什么它抱怨它

    话虽如此,如果您希望抑制警告,请注意,checker framework正在指示必须抑制的警告类型,initialization.fields.uninitialized,而且代码中似乎有一个输入错误:

    @SuppressWarnings({ "NullAway.Init", "initialization.fields.uninitialize" })
    

    请注意所需值initialization.fields.uninitialized与您提供的值initialization.fields.uninitialize之间的差异