Sonar problem - parameter should be non-zero, but marked as nullable

I wrote this predicate and sonar is complaining about it. I'm not sure how to fix this violation. Please, help:

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);
            }
        };
    }
}

      

It complains about the Foo parameter to the application method.

Dodgy - Parameter must be nonnull but is marked as nullable
foo must be nonnull but is marked as nullable

      

The apply method is defined to be NULL and there is nothing I can do about it. So Sonar and Guva aren't fighting here?

> 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

      

+3


source to share


1 answer


You do foo.getKey()

. If foo

is null, which will throw a NullPointerException, but you are not declaring that foo

it should not be null.

Zero check foo

before using it.



if (foo != null) {
   return foo.getKey().equals(o);
} else {
   return null;
}

      

+4


source







All Articles