英文:
Is this FindBugs nullpointer dereference error valid for spring-data Specification class?
问题
我在以下代码行上遇到了“可能的空指针解引用,由于调用方法的返回值” FindBugs 错误。
Specification spec = Specification.where(idSpec).and(nameSpec)
.and(typeSpec).and(statusSpec);
Specification 是 Spring 数据 JPA 类。以下是其中一些代码片段:
@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));
}
这是否是有效的 FindBugs 错误?如何修复它?
我如何避免在每次调用 where
和 and
时都进行空检查?因为这样的空检查会降低代码的可读性,而当前的代码看起来就像是使用方法链进行查询。
英文:
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.
答案1
得分: 1
这是有效的FindBugs错误吗?
是的,是有效的。
如何修复它?
添加对null
的测试或告诉FindBugs保持安静。
如何避免在每次调用where
和and
时进行null
检查?这样的空值检查会降低代码的可读性,而当前的代码就像使用方法链的查询一样。
没有解决这个问题的灵丹妙药。您需要执行以下操作之一:
- 添加繁琐的空值检查,或者
- 为
Specification
编写自己的替代品,其中参数和结果不是Nullable
,或者 - 逐个抑制FindBugs发现的任何可能不是真正错误的潜在错误,或者
- 完全关闭该检查。
请注意,如果您抑制了一个真正的错误的Findbugs检查,您有可能在运行时遇到NPEs。因此,您需要使用其他技术来查找应用程序中的任何(真正的)错误,这些错误可能导致NPEs。例如,更全面的单元测试和系统测试。
1 - 我不确定这在技术上是否可行,但您可能能够编写Specification
及其相关类的子类,然后修改代码以使用它们而不是原始类。这样做会有一些不利因素...
英文:
> Is this valid FindBugs error?
Yes it is.
> How to fix it?
Add the tests for null
or tell FindBugs to be quiet.
> 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.
There is no magic bullet for this. You need to do one of the following:
- add the ugly null checks, or
- code your own replacement<sup>1</sup> for
Specification
where the arguments and results are notNullable
, or - individually suppress any potential bugs found by FindBugs that you "know" are not real bugs, or
- turn that check off completely.
Note that if you suppress a Findbugs check that is an actual bug, you are liable to get NPEs at runtime. So the onus would be on you to use other techniques to find any (real) bugs in your application could lead to NPEs. For example, more comprehensive unit and system tests.
<sup>1 - I am not sure if this is technically feasible, but you may be able to write subclasses of Specification
and friends, and then change your code to use them instead of the originals. There will be downsides to doing this ...</sup>
答案2
得分: 1
Spring已经在DATAJPA-1766的一部分中从Specification
类中移除了这些不正确的@Nullable
注解。
在使用了上述缺陷修复的Spring版本后,现在将正常工作。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论