0

我正在测试 CodePro Anlaytix(Eclipse 插件)以检查项目中的代码样式。CPA 告诉我设置器中变量“titleParam”和“descParam”的“变量具有空值”。

这是课程:

/**
 * fdas fsda fsda fsa
 * @version 1.0
 */
public class CodeProItem {

    /**
     * Field title.
     */
    private String title;

    /**
     * Field desc.
     */
    private String desc;

    /**
     * Method getTitle.
     * @return String
     */
    public String getTitle() {
        return title;
    }

    /**
     * Method setTitle.
     * @param titleParam String
     */
    public void setTitle(String titleParam) {        
        this.title = titleParam;
    }

    /**
     * Method getDesc.
     * @return String
     */
    public String getDesc() {
        return desc;
    }

    /**
     * Method setDesc.
     * @param descParam String
     */
    public void setDesc(String descParam) {
        this.desc = descParam;
    }

}

这是规则的摘要(来自 CPA 文档):

保证为空值并在表达式中使用的变量可能表明程序员忘记用其实际值初始化变量。

规则“变量具有空值”已激活,这是此规则将捕获的代码示例(来自 CPA 文档):

public boolean myMethod(String param)
{
    String tmp = null;
    if (tmp.equals(param)) {
        return true;
    } else {
        return false;
}   
}

我得到了这个例子,但为什么它说我在设置器中的参数为空?

4

1 回答 1

0

It seems like a bug for me. If it would say it might be null and should be checked, that is possible. But unless it is called with a known null value somewhere else (that is possible and often possible to find out statically), the error makes no sense. However, if the caller provides the issue, then simply the error marker is misplaced.

Generally, I use FindBugs - that is optimized not to give false warnings, but works quite nice in my experience.

于 2012-01-17T20:24:28.607 回答