0

这是我的错误

  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 开心?

这是完整的源代码检查器框架,因为如果我这样做了,它就不会编译。

4

1 回答 1

2

Checker 框架抱怨,因为在您的main方法中您没有初始化实例字段argargs并且dir. 如果您没有显式注释字段,则从检查器框架的角度来看,该字段被认为是@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框架只知道在main方法调用之后这些字段没有被初始化,没有这样的初始化代码,picocli会提供你指定的默认值但是checker框架不知道,这就是它的原因正在抱怨它。

话虽如此,如果您更喜欢抑制警告,请注意检查器框架指示必须抑制的警告类型initialization.fields.uninitialized,并且您的代码中似乎有错字:

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

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

于 2020-12-17T13:42:10.027 回答