如何告诉 IDEA/Studio 已完成空检查? [英] How to tell IDEA/Studio that the null check has been done?

查看:28
本文介绍了如何告诉 IDEA/Studio 已完成空检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm developing with Android Studio/IntelliJ IDEA.

我已启用名为Constant conditions &"的检查检查例外"如果我冒着 NPE 的风险,它会显示警告,例如:

I have enabled the inspection check called "Constant conditions & exceptions" that shows a warning if I am risking a NPE, such as:

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

我的代码中有以下内容:

I have the following in my code:

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;
}

我不会冒任何 NPE 的风险,因为 TextUtils#isEmpty(String) 会返回 true 给 null 指针.

I'm not risking any NPE because TextUtils#isEmpty(String) would return true to a null pointer.

但是,我仍然收到小 方法调用 'encoding.equalsIgnoreCase('gzip')' 可能会产生 'java.lang.NullPointerException' 警告,这可能很烦人.

However I'm still getting the little Method invocation 'encoding.equalsIgnoreCase("gzip")' may produce 'java.lang.NullPointerException' warning, which can be annoying.

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

Is it possible to make this check smarter and ignore the NPE warning if there's already a null-check done?

推荐答案

您可以查看 Peter Gromov 在他的 answer 中提到的链接.

You can look into the link that Peter Gromov mention in his answer.

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

Created some simple classes that resemble your setup:

一个带有用@Nullable注解的方法的类:

A class with a method annotated with @Nullable:

带有 isEmpty 方法的 TextUtil 类:

The TextUtil class with it's isEmpty method:

最后是调用 TextUtil#isEmpty 的主类:

And finally the main class calling the TextUtil#isEmpty:

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

Now if you enter the File -> Settings... and go to Inspections ->Constant conditions & exceptions part you can change the Configure Assert/Check Methods to cater for your isEmpty method:

添加一个新的IsNull检查方法:

Add a new IsNull check method:

进入TextUtil类,isEmpty方法和CharSequence参数:

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

This gives this Assert/Check Method Configuration window:

Ok 然后再按 Ok 回到编辑器视图,你会看到检查消失了:

Press Ok and then Ok again to go back to the editor view and you'll see that the inspection disappeared:

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

You are actually telling IntelliJ that the isEmpty method is doing a null check on the str parameter.

这篇关于如何告诉 IDEA/Studio 已完成空检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆