Optional.orElse(null) 标记为 org.jetbrains.annotations.NotNull

Optional.orElse(null) is marked with org.jetbrains.annotations.NotNull

Optional.orElse(null)如何标记为org.jetbrains.annotations.NotNull

orElse 的 javadoc 明确指出允许 null

     * @param other the value to be returned, if no value is present.
     *        May be {@code null}.

但是我的代码在 SonarQube 中标记了一个“错误”。

        return users.findById(userID)
            .flatMap(user -> students.findByUser(user))
            .map(GetAllStudentsSvc::mapToStudent)
            .orElse(null);

查看代码似乎没有标记。查看 IDE 它似乎也没有被标记

即使将代码注释为 @Nullable 也无济于事。

我认为很明显 null 在这里非常好,所以我建议抑制它。这对我有用:

@SuppressWarnings("squid:S2637")