[英]Why does Eclipse JDT Null-Checking respect Apache Commons Validate
[英]Why Eclipse null analysis ignore null-checking statement: if (someObject != null) {…}, when someObject is a field-scope variable?
我有這樣的課:
import javax.annotation.Nullable;
public class Nullness {
private @Nullable Object someObject;
public void foo() {
if (someObject != null) {
someObject.toString(); //error "Potential null pointer access: The field someObject is specified as @Nullable"
}
}
}
當啟用eclipse null分析時,在someObject.toString();
語句中標記了一個錯誤someObject.toString();
讀取Potential null pointer access: The field someObject is specified as @Nullable
。
Eclipse有一個快速修復程序,可以將我的代碼更改如下:
public void foo() {
final Object someObject2 = someObject;
if (someObject2 != null) {
someObject2.toString();
}
}
注意,實際上,不需要使用final
修飾符即可使錯誤消失。
我不明白為什么Eclipse不允許我在null檢查語句if (someObject != null)
直接使用字段變量,而是強迫我創建其他局部變量someObject2
。 這種行為僅僅是錯誤還是故意的?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.