java Sonar issue参数必须为非Null,但标记为可为Null
我写了这个谓词,sonar对此表示不满。我不知道如何纠正这一违规行为。请帮助:
import com.google.common.base.Predicate;
import java.util.Map;
public final class FooPredicate {
private FooPredicate(){}
public static Predicate<Map.Entry<Long,Long>> isFirstElement(final Long o) {
return new Predicate<Map.Entry<Long,Long>>() {
@Override
public boolean apply(Map.Entry<Long,Long> foo) {
return foo.getKey().equals(o);
}
};
}
}
它向apply方法抱怨Foo参数
Dodgy - Parameter must be nonnull but is marked as nullable foo must be nonnull but is marked as nullable
apply方法被定义为接受一个null值,对此我无能为力。声纳和番石榴不是在这里战斗吗
> boolean apply(@Nullable
> T input)
>
> Returns the result of applying this predicate to input. This method is
> generally expected, but not absolutely required, to have the following
> properties:
>
> Its execution does not cause any observable side effects.
> The computation is consistent with equals; that is, Objects.equal(a, b) implies that predicate.apply(a) ==
> predicate.apply(b)).
>
> Throws:
> NullPointerException - if input is null and this predicate does not accept null arguments
# 1 楼答案
你正在做
foo.getKey()
。如果foo
为null,则将引发NullPointerException,但您没有声明foo
不能为null在使用前检查空值
foo