java 谷歌番石榴函数界面中的@Nullable 输入触发 FindBugs 警告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12422473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:49:55  来源:igfitidea点击:

@Nullable input in Google Guava Function interface triggers FindBugs warning

javaguavafindbugs

提问by vitaly

The com.google.common.base.Functioninterface (from Google Guava) defines applyas:

com.google.common.base.Function(从接口谷歌番石榴)定义apply为:

@Nullable T apply(@Nullable F input);

@Nullable T apply(@Nullable F input);

The method has the following javadoc note:

该方法具有以下 javadoc 注释:

@throws NullPointerException if {@code input} is null and this function does not accept null arguments.

@throws NullPointerException if {@code input} is null and this function does not accept null arguments.

FindBugs complains about my implementation of Function:

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;
    }
}

with a high-prioritywarning:

带有高优先级警告:

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.

NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE,优先级:高

输入必须为非空但被标记为可为空

此参数始终以要求其为非空的方式使用,但该参数被显式注释为 Nullable。要么是参数的使用,要么是注解不对。

My function does not support nullinputs and an exception is thrown if that is the case. If I understand correctly, FindBugs treats this as a requirement for non-null.

我的函数不支持null输入,如果是这种情况会抛出异常。如果我理解正确,FindBugs 将此视为非空的要求。

To me it looks like a contradiction: input is @Nullable but method @throws NullPointerException when it is null. Am I missing something?

对我来说,它看起来像一个矛盾:输入是@Nullable 但方法 @throws NullPointerException 当它为空时。我错过了什么吗?

The only way to get rid of the warning that I can see is manual suppression. (Guava code is out of my control, obviously).

摆脱我能看到的警告的唯一方法是手动抑制。(显然,番石榴代码超出了我的控制范围)。

Who is wrong about the usage of @Nullable annotation, FindBugs, Guava or myself?

@Nullable 注释、FindBugs、Guava 或我自己的用法谁错了?

采纳答案by Xaerxess

Your implementation is wrong ;)

你的实现是错误的;)

Basically docs says (I'll paraphrase and emphasise):

基本上文档说(我会解释和强调):

@throws NullPointerExceptionif inputis null and the concrete function implementationdoes not accept null arguments

@throws NullPointerExceptionifinput为空且具体的函数实现不接受空参数

By implementing your function you must decide if it accepts nulls or not. In first case:

通过实现您的函数,您必须决定它是否接受空值。在第一种情况下:

private static final class Example implements Function<MyBean, String> {
    @Override
    @Nullable
    public String apply(@Nullable MyBean input) {
        return input == null ? null : input.field;
    }
}

In second case:

第二种情况:

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;
    }
}

In both examples returning null is allowed.

在这两个示例中,都允许返回 null。

EDIT:

编辑:

Note that Guava uses @javax.annotation.ParametersAreNonnullByDefaulton all packages, hence if @Nullableis present it means "suspend global @Nonnulland allow nulls here" and if not it means "nulls forbidden here".

请注意,番石榴@javax.annotation.ParametersAreNonnullByDefault在所有包上都使用,因此如果@Nullable存在,则表示“暂停全局@Nonnull并在此处允许空值”,如果不存在,则表示“此处禁止空值”。

That said, you may want use @Nonnullannotation on your argument or @ParametersAreNonnullByDefaultin package to tell FindBugs Function's argument can't be null.

也就是说,您可能希望@Nonnull在参数或@ParametersAreNonnullByDefault包中使用注释来告诉 FindBugs 函数的参数不能为空。

EDIT 2:

编辑2:

Turns out this case is known issue, see comment #3 (from Guava's lead dev Kevin Bourrillion, about his conversation with Bill Pugh, Findbugs' lead):

事实证明,此案例是已知问题,请参阅评论 #3(来自 Guava 的首席开发人员 Kevin Bourrillion,关于他与 Findbugs 的负责人 Bill Pugh 的对话):

My reference was a series of in-person conversations with Bill Pugh. He asserted unambiguously that @Nullablemeans only that some subtypes mightaccept 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).

我的参考是与 Bill Pugh 的一系列面对面对话。他毫不含糊地断言,这@Nullable意味着只有某些子类型 可能接受 null。这似乎为我们的 findbugs 所证实——我们的代码非常干净地通过了可空性检查(尽管我们应该再次检查,因为进行了这个特定的函数更改)。

回答by Balamurugan Muthiah

Marking the parameter @Nonnullresolves the issue from findbugs.

标记参数@Nonnull可以解决 findbugs 的问题。

回答by Ben Thomas

It seems as though by default Google Guava Functions are @Nullable by default - I was getting Findbugs errors stating that "result must be nonnull but is marked as nullable" when there was no annotation. Adding @Nonnull to the function declaration in the following way helped:

似乎默认情况下,Google Guava 函数默认为 @Nullable - 当没有注释时,我收到 Findbugs 错误,指出“结果必须为非空,但被标记为可为空”。通过以下方式将 @Nonnull 添加到函数声明中有所帮助:

new Function<Object, Object>() {
            @Nonnull
            public Object apply(@Nonnull Object object) {

and now Findbugs is happy. Thanks all

现在 Findbugs 很高兴。谢谢大家