的com.google.common.base.Function
接口(從Google Guava)定義apply
爲:@Nullable輸入觸發FindBugs的警告
@Nullable T apply(@Nullable F input);
的方法,具有以下的Javadoc註釋:
@throws NullPointerException if {@code input} is null and this function does not accept null arguments
。
FindBugs的抱怨我的實現功能:
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,優先順序:高度
輸入必須爲非空,但被標記可爲空
該參數始終以非空的方式使用,但該參數明確註釋爲Nullable。參數或註釋的使用是錯誤的。
我的功能不支持null
輸入,如果是這種情況,則會引發異常。如果我理解正確,FindBugs將此視爲非空要求。
對我來說,它看起來像一個矛盾:輸入是@Nullable,但方法@throws NullPointerException當它爲null。我錯過了什麼嗎?
擺脫我可以看到的警告的唯一方法是手動抑制。 (顯然,番石榴密碼不在我的控制之下)。
誰對@Nullable註釋,FindBugs,Guava或我自己的用法有錯?
你說你會在簽名中接受null,然後你在* body中拒絕* null值。這聽起來像你誤解了「@ Nullable」的用途。 –
我爲輸入參數添加'@ Nullable'的唯一原因是它是'Function'接口定義的。 無論如何,我從@Xaerxess建議中刪除了參數中的'@ Nullable'註釋,但FindBugs不斷抱怨。 – vitaly