如何分辨IDEA / Studio中的空检查已经做了什么? [英] How to tell IDEA/Studio that the null check has been done?

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

问题描述

我与Android工作室/ IntelliJ IDEA的发展。

I'm developing with Android Studio/IntelliJ IDEA.

我已经启用名为检验检查恒条件和放大器;例外情况,显示一个警告,如果我冒着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
    ...
}

我在我的code以下内容:

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

下面的文本实用程序#的isEmpty(字符串)的源$ C ​​$ C

/**
 * 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 文本实用程序#的isEmpty(字符串)将返回true到指针

不过,我仍然得到一点方法调用'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?

推荐答案

您可以看看彼得·格罗莫夫提及在他的答案的链接

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

创建一些简单的类,像您的设置:

Created some simple classes that resemble your setup:

@Nullable 注释的方法的类:

它的 TextUtil 类的<​​code>的isEmpty 方法:

和最后的主类调用 TextUtil#的isEmpty

现在,如果你输入文件 - &GT;设置... 并转到检查 - &GT;恒定的条件和放大器;例外部分,你可以修改配置报警/检查方法来满足您的的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 参数:

Enter the TextUtil class, isEmpty method and CharSequence parameter:

这给出了这样的断言/检查方法配置窗口:

preSS 确定然后确定回去给编辑视图,你会看到该检查消失:

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天全站免登陆