10

我正在使用 Android Studio/IntelliJ IDEA 进行开发。

我启用了名为“恒定条件和异常”的检查检查,如果我冒着 NPE 的风险,它会显示警告,例如:

String foo = foo.bar(); // Foo#bar() is @nullable
if (foo.contains("bar")) { // I'm living dangerously
    ...
}

我的代码中有以下内容:

String encoding = contentEncoding == null ? null : contentEncoding.getValue();
if (!TextUtils.isEmpty(encoding) && encoding.equalsIgnoreCase("gzip")) {
    inputStream = new GZIPInputStream(entity.getContent());
} else {
    inputStream = entity.getContent();
}

下面是源代码TextUtils#isEmpty(String)

/**
 * Returns true if the string is null or 0-length.
 * @param str the string to be examined
 * @return true if str is null or zero length
 */
public static boolean isEmpty(CharSequence str) {
    if (str == null || str.length() == 0)
        return true;
    else
        return false;
}

我不会冒任何 NPETextUtils#isEmpty(String)的风险,因为会返回 truenull指针。

但是我仍然得到一点点Method invocation 'encoding.equalsIgnoreCase("gzip")' may produce 'java.lang.NullPointerException'警告,这可能很烦人。

如果已经完成了空检查,是否可以使此检查更智能并忽略 NPE 警告?

4

6 回答 6

19

您可以查看 Peter Gromov 在他的回答中提到的链接。

创建了一些类似于您的设置的简单类:

具有用 注释的方法的类@Nullable

在此处输入图像描述

TextUtil具有它的isEmpty方法的类:

在此处输入图像描述

最后是主类调用TextUtil#isEmpty

在此处输入图像描述

现在,如果您输入File -> Settings...并转到Inspections ->Constant conditions & exceptions部分,您可以更改Configure Assert/Check Methods以适应您的isEmpty方法:

在此处输入图像描述

添加新的IsNull检查方法:

在此处输入图像描述

输入TextUtil类、isEmpty方法和CharSequence参数:

在此处输入图像描述

这给出了这个Assert/Check Method Configuration窗口:

在此处输入图像描述

再次按OkOk以返回编辑器视图,您会看到检查消失了:

在此处输入图像描述

您实际上是在告诉 IntelliJ,该isEmpty方法正在对str参数进行空值检查。

于 2013-10-11T13:24:44.097 回答
10

您可以使用//noinspection ConstantConditions它来删除以下行的 NPE 警告,如下所示:

String encoding = contentEncoding == null ? null : contentEncoding.getValue();

//noinspection ConstantConditions
if (!TextUtils.isEmpty(encoding) && encoding.equalsIgnoreCase("gzip")) {
    inputStream = new GZIPInputStream(entity.getContent());
} else {
    inputStream = entity.getContent();
}
于 2016-08-25T10:02:33.230 回答
5

您可以使用@SuppressWarnings("ConstantConditions")注释。

@SuppressWarnings("ConstantConditions")
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int indexViewType) {
    if (inflater == null) {
        inflater = LayoutInflater.from(parent.getContext());
    }
    ItemViewProvider provider = getProviderByIndex(indexViewType);
    provider.adapter = MultiTypeAdapter.this;
    return provider.onCreateViewHolder(inflater, parent);
}
于 2017-02-19T09:49:42.890 回答
3
  1. 选择“TextUtils.isEmpty”。
  2. 右键单击-> 显示上下文操作-> 添加方法协定。
  3. 输入“null -> true”。
  4. 保存配置 xml。

请在此处查看详细信息

于 2020-10-13T10:01:53.297 回答
1

有关 IDEA 12,请参见http://www.jetbrains.com/idea/webhelp/configuring-check-assert-methods.html。在 IDEA 13 EAP 中,您可以添加方法合同:http: //youtrack.jetbrains.com/issue /IDEA-93372

于 2013-10-11T10:02:50.133 回答
1

不幸的是,标记为“正确答案”的解决方案是过时的。但我找到了对我来说等效的解决方案。

新版本的 IDE 可以正确使用静态方法。因此,问题中的示例将不再发出警告。

TextUtils#isEmpty(String);

public static boolean isEmpty(CharSequence str) {
    // your checks
}
于 2018-12-06T13:33:38.343 回答