[FindBugs分析记录]Redundant nullcheck of o,which is known to be non-null

官网解释:

  This method contains a redundant check of a known non-null value against the constant null.

  这种方法包含了一个称为非空对空值的不断重复检查。

什么代码会引起这个问题呢?先看下面:

public static boolean isNull(Object o) {

        if (null == o)

            return null == o;

        if (o instanceof String) {

            return StringUtils.isBlank((String) o);

        }

        return o == null;

    }

倒数第二行代码:return o == null;执行到这一步的时候已经表明o不为空,现在就是拿一个已知非空的值和null进行比较,findbus认为这是种运行期存在安全隐患的代码。

这里的建议是:防止这种情况出现,直接返回false.

public static boolean isNull(Object o) {

        if (null == o)

            return null == o;

        if (o instanceof String) {

            return StringUtils.isBlank((String) o);

        }

        return false;

    }

 

你可能感兴趣的:(findbugs)