将表达式转换为 @Nonnull,以避免在 Eclipse 中出现警告?

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

Cast expression to @Nonnull to avoid warning in Eclipse?

问题

我正在尝试使用javax.annotation.{Nonnull,Nullable},以查看它是否能够增强在Eclipse中的代码质量。在简单的情况下,它似乎有所帮助,但在某些情况下可能会变得尴尬,我想确定缓解问题的最佳策略。

我目前正在查看一个类似于以下代码的方法:

public @Nonnull List<Thing> produceSomeThings(Stuff stuff) {
    List<Thing> things = new ArrayList<>();
    if (stuff != null) {
        if (stuff.checkStuff())
            things.add(stuff.getThings());
        }
    }
    if (CollectionUtils.isNotEmpty(things)) {
        return things.stream() // 这一行
            .filter(Util.filter(thing -> thing.isBad())
            .collect(Collectors.toList());
    }
    return things;
}

在Eclipse中,这会在"这一行"上产生一个警告,内容如下:

Null type safety: The expression of type 'List<Thing>' needs unchecked conversion to conform to '@NonNull List<Thing>'

我注意到了以下Eclipse的错误报告:https://bugs.eclipse.org/bugs/show_bug.cgi?id=501464。

这个问题似乎已经修复了,而且修复版本已经在很多版本之前就可用了。然而,我无法找到任何方法来进行转换,以使其能够正常工作,更不用说让它看起来不那么糟糕。

我还尝试将该行更改为简单地分配给"things",但这只是将警告出现的行移动了一下。

在这里有哪些合理的选择?

更新

在检查了@howlger提供的一些选项后,我做了以下更改:

  • 修改了libraries/java/java/util/streams/Collectors.eea文件,添加了建议的一个"1"字符
  • 按建议实现了中间的"toList"函数子

从我所看到的情况来看,这些更改没有任何区别。错误消息在技术上是不同的,但我认为问题是相同的。添加"SuppressWarnings"注解没有任何效果。

新的代码(更接近实际的真实代码)如下:

Collector<Thing, ?, @NonNull List<Thing>> toList = Collectors.toList();
return orderThing.stream()
        .filter(CartUtil.distinctByKey(individualThing -> individualThing.get_ThingId()))
        .collect(toList);

"toList"表达式有以下警告:

Null type safety (type annotations): The expression of type 'Collector<@NonNull Thing,capture#of ?,@NonNull List<@NonNull Thing>>' needs unchecked conversion to conform to '@NonNull Collector<? super @NonNull Thing,capture#of ?,@NonNull List<@NonNull Thing>>'
英文:

I am experimenting with using javax.annotation.{Nonnull,Nullable} to see if it can enhance code quality in Eclipse. It seems to help in simple cases, but there are cases where it gets awkward, and I want to determine the best strategies for mitigating the problems.

I'm currently looking at a method that looks somewhat like this:

public @Nonnull List&lt;Thing&gt; produceSomeThings(Stuff stuff) {
    List&lt;Thing&gt; things = new ArrayList&lt;&gt;();
    if (stuff != null) {
        if (stuff.checkStuff())
            things.add(stuff.getThings());
        }
    }
    if (CollectionUtils.isNotEmpty(things)) {
        return things.stream() // this line
            .filter(Util.filter(thing -&gt; thing.isBad())
            .collect(Collectors.toList());
    }
    return things;
}

In Eclipse, this presents a warning on "this line", saying this:

Null type safety: The expression of type &#39;List&lt;Thing&gt;&#39; needs unchecked conversion to conform to &#39;@NonNull List&lt;Thing&gt;&#39;

I noticed the following Eclipse bug report: https://bugs.eclipse.org/bugs/show_bug.cgi?id=501464 .

This appears to have been fixed, and the fix was available many versions ago. However, I'm unable to figure out any way to cast this so that it even works, much less doesn't look nasty.

I also tried changing that line to simply assign to "things", but that just moves what line the warning appears at.

What are reasonable options here?

Update:

Examining some of the options that @howlger provides, I did the following:

  • Modified the libraries/java/java/util/streams/Collectors.eea file, adding the one "1" character suggested
  • Implemented the intermediate "toList" functor as suggested

From what I can see, these changes didn't make any difference. The error message is technically different, but I think it's the same problem. Adding the "SuppressWarnings" annotation had no effect at all.

The new code (a little closer to the actual real code) is this:

Collector&lt;Thing, ?, @NonNull List&lt;Thing&gt;&gt; toList = Collectors.toList();
return orderThing.stream()
		.filter(CartUtil.distinctByKey(individualThing -&gt; individualThing.get_ThingId()))
		.collect(toList);

The "toList" expression has this warning:

> Null type safety (type annotations): The expression of type 'Collector<@NonNull Thing,capture#of ?,@NonNull List<@NonNull Thing>>' needs unchecked conversion to conform to '@NonNull Collector<? super @NonNull Thing,capture#of ?,@NonNull List<@NonNull Thing>>'

答案1

得分: 1

这里,不是修改后的 things 列表,而是基于 things 计算得出的一个新的 List&lt;Thing&gt; 类型的对象实例被返回。这个计算得出的列表是否保证为非“null”取决于 collect(...) 返回什么,而这又取决于 Collector 的实现。由于 API 并不保证它,并且从代码中推断出来并不是微不足道的,因此会显示警告。

请按以下方式重写您的代码:

@SuppressWarnings(&quot;null&quot;)
Collector&lt;Thing, ?, @NonNull List&lt;Thing&gt;&gt; toList = Collectors.toList();
return things.stream()
             .filter(Util.filter(thing -&gt; thing.isBad()))
             .collect(toList);

或者,外部空值注释 也可以在涵盖所有使用的情况下起作用。

英文:

Here, not the modified things list, but a new object intance of type List&lt;Thing&gt;, that was computed based on things is returned. Whether this computed list is guaranteed to be non-null, depends on what collect(...) returns, which depends on the implementation of Collector. Since the API does not guarantee it and it is not trivial to deduce from the code, the warning is displayed.

Rewrite your code as follows:

@SuppressWarnings(&quot;null&quot;)
Collector&lt;Thing, ?, @NonNull List&lt;Thing&gt;&gt; toList = Collectors.toList();
return things.stream()
             .filter(Util.filter(thing -&gt; thing.isBad()))
             .collect(toList);

Alternatively, external null annotations should also work if they cover everything used.

huangapple
  • 本文由 发表于 2020年10月9日 04:14:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64269997.html
匿名

发表评论

匿名网友

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

确定