简体   繁体   English

为什么checkNotNull()没有用@Nonnull注释

[英]Why checkNotNull() is not annotated with @Nonnull

I found inconvenient that checkNotNull() precondition in guava is not marked with @Nonull annotation. 我发现不方便之处在于番石榴中的checkNotNull()前提条件未使用@Nonull批注标记。 Consider following example: 考虑以下示例:

State(Set<Model> models, Set<Variation> variations) {
  this.models = checkNotNull(models);
  this.variations = checkNotNull(variations);

  if (this.variations == null) {
     throw new IllegalArgumentException();
  }
  this.engine = createEngine();
}

So IDE could not found that variations == null is always false. 因此,IDE无法发现variations == null始终为false。 Is there any particular reasons why this precondition is not marked with @Nonull (even if it's arguments are defined using @Nullable ). 是否有任何特殊原因为何此先决条件没有用@Nonull标记(即使其参数是使用@Nullable定义的)。

We haven't used @Nonnull anywhere , sorry. 很抱歉, 我们没有在任何地方使用@Nonnull Why? 为什么? We tried adding more null-checking annotations, and we found that: 我们尝试添加更多的空检查注释,发现:

  • Adding all the other annotations is highly verbose. 添加所有其他注释非常冗长。
  • @Nullable is all we need for NullPointerTester . @NullableNullPointerTester所需的全部。 Admittedly that's more important to Guava developers than Guava users. 诚然,对于Guava开发人员而言,这比Guava用户更为重要。
  • @Nullable appeared to have caught most problems. @Nullable似乎遇到了大多数问题。 I admit it's hard to say how many un-checked-in bugs other annotations would have caught before the user found them. 我承认很难说在用户找到其他注释之前会捕获多少未检查的错误。

The verbosity was the main thing. 详细是最主要的。 It gets crazy, especially with subtyping and with parameterized types. 它变得很疯狂,尤其是对于子类型化和参数化类型。 We tried to pick a sweet spot for annotations. 我们试图为注释选择一个最佳位置。 Maybe we'll change it one day. 也许有一天我们会更改它。 For the moment, though, that's why things are the way they are. 但就目前而言,这就是事情按原样进行的原因。

(If we did do something, I suspect we'd try to make @Nonnull the default, using @CheckForNull for the exceptions instead. But I haven't looked into it even enough to make sure I've got the meanings right.) (如果我们做了某些事情,我怀疑我们会尝试将@Nonnull为默认值,而是使用@CheckForNull作为例外。但是我还没有仔细研究它以确保我理解了正确的含义。)

It would indeed be interesting to annotate its result with @Nonnull , since checkNotNull() throws a NPE if the reference is null , which means it never returns null : @Nonnull注释其结果确实很有趣,因为如果引用为null ,则checkNotNull()会引发NPE,这意味着它永远不会返回null

  @Nonnull
  public static <T> T checkNotNull(T reference) {
    if (reference == null) {
      throw new NullPointerException();
    }
    return reference;
  }

Note that you'd need to change your code to: 请注意,您需要将代码更改为:

if(this.variations == null)

since the @Nonnull would only apply to the result of checkNotNull() , but says nothing about its argument. 因为@Nonnull仅适用于checkNotNull()的结果,但对其参数一无所知。 Note that we cannot annotate the argument with @Nonnull , since we might often be checking nullable variables. 注意,我们不能用@Nonnull注释参数,因为我们可能经常检查可为空的变量。

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

相关问题 同时使用@Nonnull和Preconditions.checkNotNull(…) - Using both @Nonnull and Preconditions.checkNotNull(…) 如果使用@Nonnull 进行注释,则没有针对 null 返回值的 FindBugs 警告 - No FindBugs warning for null return value if annotated with @Nonnull 为@Nonnull 注释参数编写单元测试 - Writing unit test for @Nonnull annotated parameter Java中,用带有@NonNull注释的属性验证对象的最佳方法 - in Java, best way to validate objects with properties annotated with @NonNull 尽管使用 @NonNull 进行了注释,但在带有 @NonNull for Primay Key 的 Room 上的 Android 应用程序错误 - Android app error on Room with @NonNull for Primay Key despite being annotated with @NonNull 当方法arg注释为@Nonnull时,JUnit测试失败 - JUnit test fails when method arg is annotated @Nonnull 此方法调用为非空方法参数传递空值。 将该参数注释为应始终为非null的参数 - This method call passes a null value for a nonnull method parameter. Either the parameter is annotated as a parameter that should always be nonnull Android Studio错误的含义:未注释的参数会覆盖@NonNull参数 - Meaning of Android Studio error: Not annotated parameter overrides @NonNull parameter 为什么不允许在FQN上使用@NonNull注释? - Why Are @NonNull Annotation on FQN Not Allowed? 为什么在运行时检查@Nonnull注释? - Why @Nonnull annotation checked at runtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM