如何注释我的辅助方法,以便 Eclipse 在返回 true 时知道它的参数是非空的? [英] How can I annotate my helper method so Eclipse knows its argument is non null if it returns true?

查看:14
本文介绍了如何注释我的辅助方法,以便 Eclipse 在返回 true 时知道它的参数是非空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个辅助方法,hasContent(String),如果它的参数既非空又包含至少一个非空白字符,则该方法返回 true.我刚刚在 Eclipse 中启用了空分析,我发现当我使用此方法执行一段代码时,该代码块以我的辅助函数的结果为条件,表明该字符串具有内容(因此 不能 为空),但 Eclipse 会抱怨我的 String 可能仍然为空.

I have a helper method, hasContent(String), which returns true iff its argument is both non-null and contains at least one non-whitespace character. I have just enabled null analysis in Eclipse and I have found that when I use this method to execute a block of code which is conditional on the result of my helper function indicating that the string has content (and therefore cannot be null), that nonetheless Eclipse complains that my String might still be null.

辅助函数

public static boolean hasContent(String text) {
    if (text == null)
        return false;
    if (text.trim().length() == 0)
        return false;
    return true;
}

使用示例

...
String dataString;

try {
    dataString = readStringFromFile("somefile.txt");
} catch (IOException e) {
    System.err.println("Failed to read file due to error: " + e);
    dataString = null;
}

// At this point dataString may be null

if (hasContent(dataString)) {

    // At this point dataString must be non-null, but Eclipse warns:
    // "Potential null pointer access: The variable dataString may be null at this location"
    // at the following reference to dataString

    System.out.println("Read string length " + dataString.length());
}
...

这种情况的最佳做法是什么?如果可以避免,我不想压制警告.我更愿意告诉 Eclipse,如果 hasContent() 返回 true 那么它的参数肯定是非空的.这可能吗?如果是这样,如何?

What is the best practice for this situation? I don't want to suppress the warning if I can avoid it. I'd prefer to tell Eclipse that if hasContent() returns true then it's argument is definately non-null. Is this possible? If so, how?

推荐答案

你的方法的约定是,如果 hasContent 返回 true,那么它的参数保证为非空.

The contract of your method is that if hasContent returns true, then its argument is guaranteed to be non-null.

Eclipse 无法在编译时表达或检查此契约,至少在不更改您的代码并降低其风格的情况下是这样.

Eclipse cannot express or check this contract at compile time, at least without changing your code and degrading its style.

Nullness Checker 是一个不同的工具,可以在编译时表达和检查这个合约时间.它不需要您更改代码.您只需添加 @EnsuresNonNullIf 注释您的代码:

The Nullness Checker is a different tool that can express and check this contract at compile time. It does so without requiring you to change your code. You simply add the @EnsuresNonNullIf annotation your code:

@EnsuresNonNullIf(expression="#1", result=true)
public static boolean hasContent(String text) { ...

空值检查器随检查器框架一起分发.有一个 Eclipse 插件,可让您在 Eclipse 中运行 Nullness Checker.

The Nullness Checker is distributed with the Checker Framework. There is an Eclipse plugin that enables you to run the Nullness Checker within Eclipse.

这篇关于如何注释我的辅助方法,以便 Eclipse 在返回 true 时知道它的参数是非空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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