Java 我如何注释我的helper方法,以便Eclipse在返回true时知道它的参数是非null的?

Java 我如何注释我的helper方法,以便Eclipse在返回true时知道它的参数是非null的?,java,eclipse,null,annotations,Java,Eclipse,Null,Annotations,我有一个助手方法,hasContent(String),当它的参数都是非null并且至少包含一个非空白字符时,它返回true。我刚刚在Eclipse中启用了空值分析,我发现当我使用此方法执行一个代码块时,该代码块的条件是我的helper函数的结果指示字符串有内容(因此不能为空),尽管如此,Eclipse还是抱怨我的字符串可能仍然为空 助手功能 public static boolean hasContent(String text) { if (text == null)

我有一个助手方法,
hasContent(String)
,当它的参数都是非null并且至少包含一个非空白字符时,它返回true。我刚刚在Eclipse中启用了空值分析,我发现当我使用此方法执行一个代码块时,该代码块的条件是我的helper函数的结果指示字符串有内容(因此不能为空),尽管如此,Eclipse还是抱怨我的字符串可能仍然为空

助手功能

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的。这可能吗?如果是,怎么做?

这可能不是最佳做法,但是:如果抛出IOException,您可以返回false,或者简单地将变量设置为false。如果不是,可以将变量设置为true(在try块中)


我看不出有什么方法可以完全实现你的目标

您可以修改
hasContent
以返回它传递的字符串,而不是a
布尔值
,并在参数为null或空时抛出
异常。然后用
@NonNull
注释函数。但是,这会以我怀疑您不喜欢的方式损害您的调用代码,因为它必须使用
try
/
catch
逻辑,而不是
if

这将使
hasContent
功能:

@NonNull
public static String hasContent(String text) throws Exception {
    if (text == null)
        throw new Exception( "Null string" );
    if (text.trim().length() == 0)
        throw new Exception( "Empty string" );
    return text;        
}
以及呼叫代码:

...
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
try {
    dataString = validateHasContent( dataString );
    // At this point dataString must be non-null

    System.out.println("Read string length " + dataString.length());
} catch( Exception e ) {        
}
...

如果您准备做出这种妥协,那么一个专用的异常显然会更好。

您的方法的契约是,如果
hasContent
返回true,那么它的参数保证为非null

Eclipse无法在编译时表达或检查此契约,至少在不更改代码和降低其样式的情况下是如此

是一种不同的工具,可以在编译时表示和检查此契约。这样做不需要您更改代码。只需在代码中添加注释:

@EnsuresNonNullIf(expression="#1", result=true)
public static boolean hasContent(String text) { ...
空值检查器与一起分发。
有一种方法可以让您在Eclipse中运行空值检查程序。

谢谢,但这没有抓住我问题的重点。我当然可以简单地测试:
if(dataString!=null){do stuff with dataString}
,这可能不会被eclipse标记。但是我想用一种方法告诉eclipse,
if(hasContent(dataString)){do stuff with dataString}
至少和上面一样安全。好的,你想做的事情需要一个默认参数,但Java不支持。谢谢你,mernst,这正是我想要的!
@EnsuresNonNullIf(expression="#1", result=true)
public static boolean hasContent(String text) { ...