Search code examples
javaspring-data-jpaspring-datafindbugsnull-check

Is this FindBugs nullpointer dereference error valid for spring-data Specification class?


I am getting "Possible null pointer dereference due to return value of called method" FindBugs error on following line.

Specification spec = Specification.where(idSpec).and(nameSpec)
    .and(typeSpec).and(statusSpec);

Specification is Spring data JPA class. Some of its snippets:

    @Nullable
    static <T> Specification<T> where(@Nullable Specification<T> spec) {
        return spec == null ? (root, query, builder) -> null : spec;
    }

    @Nullable
    default Specification<T> and(@Nullable Specification<T> other) {
        return composed(this, other, (builder, left, rhs) -> builder.and(left, rhs));
    }

Is this valid FindBugs error? How to fix it?

How can I avoid null checks on each and every call of where and and? As such null checks will reduce readbility of code which currently reads just like a query using method chaining.


Solution

  • Spring has removed these incorrect @Nullable annotations from Specification class as part of DATAJPA-1766.

    This will work fine now after using the Spring version in which above defect was fixed.