本文介绍了IntStream何时实际关闭? SonarQube S2095对IntStream是误报吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Java 8流代替许多旧式for循环来迭代一堆结果并生成摘要统计信息。例如:

I am using Java 8 streams in place of many old style for loops to iterate through a bunch of results and produce summary statistics. For example:

int messages = IntStream.rangeClosed(0, 7).map(ids::get).reduce(Integer::sum).getAsInt();

注意:我知道还有其他方法可以进行上面显示的计数。我这样做是为了说明我的问题。

我正在使用SonarQube 5.3和Java 3.9插件。在该配置中,上面的代码行违反了squid规则S2095:资源应该关闭。这是我期望看到AutoCloseable(例如,FileInputStream)被打开但从未关闭的结果。

I am using SonarQube 5.3 with the Java 3.9 plugin. In that configuration, the above line of code gives me a violation of squid rule S2095: "Resources should be closed." That's the result I would expect to see if an AutoCloseable (e.g., a FileInputStream) was opened but never closed.

所以这是我的问题:终端操作 reduce 关闭流吗?应该是?或者这是鱿鱼规则中的误报?

So here's my question: does the terminal operation reduce close the stream? Should it? Or is this a false positive in the squid rule?

推荐答案

它没有关闭,因为 AutoCloseable 界面仅适用于 try-with-resources 。但是对于 IntStream ,这个关闭操作是完全没必要的,因为它在 AutoCloseable 接口 javadoc

It isn't closed, because AutoCloseable interface works only inside try-with-resources. But this close operation is totally unnecessary for IntStream as it said in AutoCloseable interface javadoc:

所以:S2095是IntStream的误报。希望通过确定这一点

So yes S2095 is a false positive for IntStream. That will be hopefully fixed by SONARJAVA-1478

这篇关于IntStream何时实际关闭? SonarQube S2095对IntStream是误报吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 23:45