在方法參數中使用 NotNull Annotation - Using NotNull Annotation in method argument

問題:

I just started using the @NotNull annotation with Java 8 and getting some unexpected results.我剛開始在 Java 8 中使用@NotNull註釋並得到一些意想不到的結果。

I have a method like this:我有一個這樣的方法:

public List<Found> findStuff(@NotNull List<Searching> searchingList) {
    ... code here ...
}

I wrote a JUnit test passing in the null value for the argument searchingList.我編寫了一個 JUnit 測試,傳入參數 searchList 的空值。 I was expecting some type of error to happen but it went through as though the annotation was not there.我原以爲會發生某種類型的錯誤,但結果好像註釋不存在一樣。 Is this expected behavior?這是預期的行爲嗎? From what I understood, this was to allow you to skip writing the boilerplate null check code.據我瞭解,這是爲了讓您跳過編寫樣板空檢查代碼。

An explanation of what exactly @NotNull is supposed to do would be greatly appreciated.@NotNull 究竟應該做什麼的解釋將不勝感激。


解決方案:

參考一: https://en.stackoom.com/question/2J3Pr
參考二: https://stackoom.com/question/2J3Pr
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章