- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
com.google.common.base.Function
接口(interface)(来自 Google Guava)将 apply
定义为:
@Nullable T 应用(@Nullable F 输入);
该方法有以下javadoc注释:
@throws NullPointerException 如果 {@code input} 为 null 并且此函数不接受 null 参数
。
FindBugs 提示我的 Function 实现:
private static final class Example implements Function<MyBean, String> {
@Override
@Nullable
public String apply(@Nullable MyBean input) {
if (null == input) {
throw new NullPointerException();
}
return input.field;
}
}
带有高优先级警告:
NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE, Priority: High
input must be nonnull but is marked as nullable
This parameter is always used in a way that requires it to be nonnull, but the parameter is explicitly annotated as being Nullable. Either the use of the parameter or the annotation is wrong.
我的函数不支持 null
输入,如果是这种情况,则会引发异常。如果我理解正确,FindBugs 会将此视为非 null 的要求。
对我来说,这看起来很矛盾:输入是@Nullable,但是当它为空时方法@throws NullPointerException。我错过了什么吗?
我可以看到摆脱警告的唯一方法是手动抑制。 (显然, Guava 代码不在我的控制范围内)。
@Nullable 注解、FindBugs、Guava 还是我自己的用法谁错了?
最佳答案
你的实现是错误的;)
基本上文档说(我会解释和强调):
@throws NullPointerException
ifinput
is null and the concrete function implementation does not accept null arguments
通过实现你的函数,你必须决定它是否接受空值。第一种情况:
private static final class Example implements Function<MyBean, String> {
@Override
@Nullable
public String apply(@Nullable MyBean input) {
return input == null ? null : input.field;
}
}
第二种情况:
private static final class Example implements Function<MyBean, String> {
@Override
@Nullable
public String apply(MyBean input) {
if (null == input) {
throw new NullPointerException();
}
return input.field;
}
}
在这两个示例中都允许返回 null。
编辑:
请注意,Guava 在所有包上都使用 @javax.annotation.ParametersAreNonnullByDefault
,因此如果存在 @Nullable
则意味着“暂停全局 @Nonnull
并在此处允许空值”,如果不是,则表示“此处禁止空值”。
也就是说,您可能希望在参数上使用 @Nonnull
注释或在包中使用 @ParametersAreNonnullByDefault
来告诉 FindBugs 函数的参数不能为空。
编辑 2:
结果是 this case is known issue ,请参阅评论 #3(来自 Guava 的首席开发人员 Kevin Bourrillion,关于他与 Findbugs 的负责人 Bill Pugh 的对话):
My reference was a series of in-person conversations with Bill Pugh. He asserted unambiguously that
@Nullable
means only that some subtypes might accept null. And this seems to be borne out by findbugs for us -- our code passes the nullability checks pretty cleanly (though we should check again since this particular Function change was made).
关于java - Google Guava Function 界面中的@Nullable 输入触发 FindBugs 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12422473/
因为对这个问题的第一条评论可能是为什么以及为什么可能使这不是一个重复的问题:我有一个具有三个有效状态的值类型变量(如果重要的话是小数类型):has value | null | unspecified
Nullable 的定义是: [SerializableAttribute] public struct Nullable where T : struct, new() 约束where T : st
以下代码拒绝编译: Nullable!(Nullable!int) nni = Nullable!(Nullable!int)(10); 出现此错误消息: Error: inout method nu
Not Nullable 类型转换为 Nullable 类型的基础知识是什么? CLR 内部发生了什么? 值类型是否在内部转换为引用类型? int i = 100; and int ? i = 7?
我在寻找答案时得到的结果是指从 short 到 int 以及从可空到不可空的转换。但是,我无法理解如何将“较大”类型 int? 转换为“较小”类型 short?。 我能想到的唯一方法就是写一个这样的方
我们在契约(Contract)中使用 Swagger。考虑这个简单的响应 DTO public class Result { public int SomeInt { get;set; } }
有一个Nullable结构,还有另一个静态 Nullable具有三个静态方法的类。 我的问题是,为什么static Nullable中的这些静态方法不能?类进入Nullable结构?将它们定义为两种不
我错误地发现了一些让我吃惊的东西。 我有这个方法 public static string PrintDecimal(decimal? input, string NumberFormat = nul
此代码编译: private static void Main(string[] args) { bool? fred = true; if (fred == true)
希望标题已经很清楚了。 我想看一个更改表语句的示例,该语句可以将 Sybase 表中现有的不可为空的列更改为可以为空。 最佳答案 Modifying the NULL default value of
我使用 C# 8 可空引用类型。 我有一个泛型类,它可能接受可为空引用类型作为类型参数。 有没有办法根据泛型类型参数声明不可为空的类型,这些参数可能是可为空的引用类型(甚至是 Nullable 结构)
考虑以下代码: Nullable dt; dt. dt?. . 如何以及为什么? 最佳答案 因为如果 ?. 左侧的对象为 null,则 null 传播的工作方式永远不会执行右侧的对象。因为您知道右
我一直使用 Nullable<>.HasValue因为我喜欢语义。然而,最近我正在研究其他人现有的代码库,他们在其中使用了 Nullable<> != null。专门代替。 是否有理由使用一个而不是另
我对转换方法“.ToString()”有一个普遍的疑问。起初我使用这个语句进行转换: Nullable SomeProperty; string test = SomeProperty.ToStrin
有没有一种方法可以表达两种相关类型(一种具有可空属性,一种不具有),以便您可以在运行时进行检查后将一种强制转换为另一种? 例如 - type Stat = { count: ?number, }
对于 EF 5.0.0、VS 2012 和 .NET 4.5,当我从现有 SQL Server 2012 数据库添加新的 ADO.NET 实体数据模型时,生成的代码不会区分可空和不可空 varchar
我是 PySpark 的新手,正面临一个奇怪的问题。我试图在加载 CSV 数据集时将某些列设置为不可空。我可以使用非常小的数据集 (test.csv) 重现我的案例: col1,col2,col3 1
假设我有以下类(class): public class GenericClass { public T Find() { //return T if found ot
这个问题在这里已经有了答案: How is the boxing/unboxing behavior of Nullable possible? (3 个答案) 关闭 7 年前。 为什么 null可
Firebase 3.6.0 中的警告。 Xcode 8 - Swift 3。 这些是 Firebase 类:- @class FIROptions @class FIRAuthCredential
我是一名优秀的程序员,十分优秀!