这个FindBugs的空指针解引用错误对于spring-data的Specification类是否有效?

huangapple go评论68阅读模式
英文:

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 错误?如何修复它?

我如何避免在每次调用 whereand 时都进行空检查?因为这样的空检查会降低代码的可读性,而当前的代码看起来就像是使用方法链进行查询。

英文:

I am getting &quot;Possible null pointer dereference due to return value of called method&quot; 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 &lt;T&gt; Specification&lt;T&gt; where(@Nullable Specification&lt;T&gt; spec) {
		return spec == null ? (root, query, builder) -&gt; null : spec;
	}

	@Nullable
	default Specification&lt;T&gt; and(@Nullable Specification&lt;T&gt; other) {
		return composed(this, other, (builder, left, rhs) -&gt; 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保持安静。

如何避免在每次调用whereand时进行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 not Nullable, 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.

huangapple
  • 本文由 发表于 2020年8月6日 18:43:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63281886.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定