java-声纳问题-参数必须为非空但被标记为可为空

我已经写了这个谓词,而声纳对此有所抱怨.我不确定如何解决此违规问题.请帮忙:

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参数.

06001

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

解决方法:

您正在做foo.getKey().如果foo为null,则将抛出NullPointerException,但您并不是在声明foo不能为null.

在使用前请先检查foo.

if (foo != null) {
   return foo.getKey().equals(o);
} else {
   return null;
}
上一篇:代码检测docker-sonarqube


下一篇:java-SonarLint V3:“ Serializable”类中的字段对于List接口应该是瞬态的或可序列化的