可以对getString的此调用实际上返回null吗? [英] Could this call to getString actually return null?

查看:61
本文介绍了可以对getString的此调用实际上返回null吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将Android Studio更新到了最新版本1.2.我最近还更新了其他组件,例如Android Gradle Extensions.

I've updated Android Studio to the latest version, 1.2. I've also recently updated other components like the Android Gradle Extensions.

现在,我被警告使用以前标注为 NotNull 的方法.警告是:表达式的计算结果可能为null,但由声明为@NotNull

Now I'm getting warned on methods that I had previously annotated as NotNull. The warning is: Expression might evaluate to null but is returned by the method declared as @NotNull

这是一个例子:

public class MyApplication extends Application {
    private static SharedPreferences sPrefs = null;

    /**
     * Corresponds to a string value.
     *
     * @see #setDeviceInfo(String, String, String)
     */
    private static final String PREF_DEVICE_INFO_DEVICE_ADDRESS = "deviceaddress";

    @Override
    public void onCreate() {
        super.onCreate();
        sPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    }

    @NonNull
    public static String getDeviceAddress() {
        return sPrefs.getString(PREF_DEVICE_INFO_DEVICE_ADDRESS, "");
    }
}

在这种情况下,我认为 getString 无法返回null,因为我已经设置了默认值"

I would think that there is no way for getString to return null in this case because I have have set a default value, ""

即使我看着 getString 的实现(无论如何,在API 22中),似乎在这种情况下android studio仍然忽略了我的默认值不为null的事实,并且错误地假设默认值可以为空.
API 22实施:

And even when I look at the implementation of getString (in API 22 anyway) it would seem that this is a case where android studio has simply ignored the fact that my default value is not null and made a false assumption that the default value could be null.
API 22 implementation:

public String getString(String key, String defValue) {
    synchronized (this) {
        awaitLoadedLocked();
        String v = (String)mMap.get(key);
        return v != null ? v : defValue;
    }
}

在继续前进之前,我仍然需要社区让我放心或指出我所缺少的东西.

Still I need the community to put me at ease or point out what I'm missing before I move on.

推荐答案

您很安全

您确实很安全.

You are Safe

You are indeed safe.

您正在指定默认值,并且该方法无法返回 null 值(除非您将其指定为 defValue ).

You're specifying a default value, and the method cannot return a null value (except if you specify it as your defValue).

由于多种原因,IntelliJ/AndroidStudio只是无法确定逃生路线,并且无法确定您的方法使用是否安全.而且,方法contract不会正式拒绝 defValue 的空值.

For a number of reasons, IntelliJ/AndroidStudio was just unable to determine the escape route and to figure out your method usage was safe. And the method contracts doesn't formally reject null values for the defValue.

此外,您将方法注释为 @NotNull ,但未对 getString 本身进行注释(并且不必要运行时检查).

Also, you annotate your method as @NotNull but getString itself isn't annotated (and these are not necessarily runtime checks).

最后,我想知道您是否错误地在@NotNull和@NonNull之间切换注释,并因此切换了实现提供程序以进行注释检查.您在错误和说明中提到了@NotNull,但是代码使用了@NonNull.他们的行为不同.例如,IntelliJ的NonNull是仅静态检查.

Finally, I wonder if you didn't mistakenly switch your annotations between @NotNull and @NonNull, and consequently switched the implementation provider for your annotation checking. You mention @NotNull in your error and your description, but the code uses @NonNull. They don't behave identically. IntelliJ's NonNull, for instance, is a static-only check.

请参见我应使用哪个@NotNull Java注释?一个不错的总结.

See Which @NotNull Java annotation should I use? for a nice summary.

提出来以防万一...

Bringing these up just in case...

但是,如果指定 null 键,则地图可能会抛出 NullPointerException .但这取决于所使用的地图实现,以及该地图是否不允许空键(我没有研究过),这对您来说不是问题(至少在您提供的代码段中没有).

However, if you specify a null key, you could get a NullPointerException thrown by the Map. But that depends on the map implementation used and if that map doesn't allow null keys (I haven't looked into it), and that's not an issue for you (at least not in the code snippet you provided).

由于SharedPreferences在合同中未指定任何有关实现的内容,因此应该信任JDK的

As SharedPreferences doesn't specify in its contract anything about the implementation, should just trust what the JDK's Map.get() documentation states:

NullPointerException-如果指定的键为null,并且此映射不允许使用null键(可选)

NullPointerException - if the specified key is null and this map does not permit null keys (optional)

可能 ClassCastException

还要注意 SharedPreferences.getString 文档警告,如果存储的首选项不是 String 类型,则可能会收到ClassCastException(我想在您的情况下不太可能,但是我提到以防万一):

Possible ClassCastException

Beware as well that the SharedPreferences.getString documentation warns you could get a ClassCastException if the stored preference isn't of type String (I suppose unlikely in your case, but I'm mentioning it just in case):

如果存在一个名称不是String的首选项,则抛出ClassCastException.

Throws ClassCastException if there is a preference with this name that is not a String.

这篇关于可以对getString的此调用实际上返回null吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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