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

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

问题描述

我有一个辅助方法hasContent(String),如果其参数均为非null且包含至少一个非空白字符,则该方法返回true.我刚刚在Eclipse中启用了空分析,并且发现当我使用此方法执行代码块时,该代码块取决于我的辅助函数的结果,该结果表明字符串具有内容(因此不能 >为null),但是Eclipse抱怨我的String可能仍然为null.

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,那么它的参数肯定是非null的.这可能吗?如果可以,怎么办?

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) { ...

Nullness Checker随 Checker Framework 一起分发. 有一个 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,则知道其参数为非null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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