【发布时间】:2013-05-17 06:31:30
【问题描述】:
我遇到了一个我目前还不清楚的奇怪情况:
当在 Eclipse 中启用可能的空指针访问警告时,我会收到如下警告(警告位于相应注释之前的行):
protected Item findItemByName(String itemName, Items items) {
boolean isItemNameMissing = null == itemName || itemName.isEmpty();
boolean isItemsMissing = null == items || null == items.getItems() || items.getItems().isEmpty();
if (isItemNameMissing || isItemsMissing) {
return null;
}
// potential null pointer access: the variable items might be null at this location
for (Item item : items.getItems()) {
// potential null pointer access: the variable itemName might be null at this location
if (itemName.equals(item.getName())) {
return item;
}
}
return null;
}
如果我使用 Guava 的先决条件检查 null,也会发生同样的情况
Preconditions.checkArgument(argument != null, "argument must not be null");
我可以理解,在后一种情况下,用于检查 IllegalArgumentException 何时发生的流分析可能太困难/昂贵甚至不可能,我反过来也不明白编译器为什么会发出警告(如果我删除它们消失的检查)。
能否解释一下潜在的空指针访问是如何实现的,以及为什么在这两种情况下都会出现这种情况?或者至少给我指明方向。
同时我也看看是不是我自己发现了...
附录
我已经将其分解为案例的核心。给定以下类,警告仅显示在方法 sample2 中(正如评论再次指出的那样)。请注意,sample3 方法也不会触发警告。
public class PotentialNullPointerAccess {
public void sample1(final String aString) {
if (aString == null) {
return;
}
System.out.println(aString.length());
}
public void sample2(final String aString) {
boolean stringIsNull = null == aString;
if (stringIsNull) {
return;
}
// Potential null pointer access: The variable aString might be null at this location
System.out.println(aString.length());
}
public void sample3(final String aString) {
System.out.println(aString.length());
}
}
【问题讨论】:
-
代码的前 2 行似乎对读者不友好,IMO 你应该使用大括号对表达式进行分组
-
您收到的确切警告是什么?为什么你认为它来自编译器,而不是来自Eclipse?
-
@T.J.Crowder:我不知何故将 compiler 也包含在了 Eclipse Java Compiler (ejc) 中。此外,正如我所写的那样,我收到的确切错误消息已在评论中说明(第一个字符是大写字母除外)。
-
@sanbhat:是的。 Fwiw,这是我发现的代码。如果涉及到设计缺陷/代码异味等问题,在这个小方法中会有几件事值得一提……
-
你应该把它带到一个 Eclipse 论坛,甚至可以直接提交一个错误。
标签: java null guava compiler-warnings eclipse-jdt