为什么checkNotNull()没有用@Nonnull注释 [英] Why checkNotNull() is not annotated with @Nonnull
问题描述
我发现 checkNotNull()
guava中的前提条件不方便 @Nonull
注释。请考虑以下示例:
I found inconvenient that checkNotNull()
precondition in guava is not marked with @Nonull
annotation. 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();
}
因此IDE无法找到 variants == null
始终为false。是否有任何特殊原因导致此前置条件未标记为 @Nonull
(即使它的参数是使用 @Nullable
定义的)。
So IDE could not found that variations == null
is always false. Is there any particular reasons why this precondition is not marked with @Nonull
(even if it's arguments are defined using @Nullable
).
推荐答案
我们还没有在任何地方使用 @Nonnull
,抱歉。为什么?我们尝试添加更多的空检查注释,我们发现:
We haven't used @Nonnull
anywhere, sorry. Why? We tried adding more null-checking annotations, and we found that:
- 添加所有其他注释非常详细。
-
@Nullable
是NullPointerTester
所需的全部内容。不可否认,对Guava开发者而言,这比Guava用户更重要。 -
@Nullable
似乎已经解决了大多数问题。我承认很难说在用户找到之前会有多少未经检入的错误其他注释。
- Adding all the other annotations is highly verbose.
@Nullable
is all we need forNullPointerTester
. Admittedly that's more important to Guava developers than Guava users.@Nullable
appeared to have caught most problems. 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.
(如果我们做了某件事,我怀疑我们会尝试制作 @Nonnull
默认情况下,使用 @CheckForNull
代替例外。但是我还没有调查它甚至足以确保我已经意思是正确的。)
(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.)
这篇关于为什么checkNotNull()没有用@Nonnull注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!