遍历目录时出现 Sonar Lint 问题
Sonar Lint issue when walking a directory
我正在使用以下代码遍历目录并获取第一个文件。我无法修复这两个声纳 lint 问题。请帮忙
List<String> result = walk.filter(Files::isRegularFile).map(x -> x.toString()).collect(Collectors.toList());
请进行此更改:
List<String> result = walk.filter(p -> p.toFile().isFile()).map(Path::toString).collect(Collectors.toList());
SonarLint
还要说明建议的原因。
Lambdas should be replaced with method references
Java 8's "Files.exists" should not be used
看看 the 3725 Sonar rule :
The Files.exists method has noticeably poor performance in JDK 8, and
can slow an application significantly when used to check files that
don't actually exist.
The same goes for Files.notExists, Files.isDirectory and
Files.isRegularFile.
Note that this rule is automatically disabled when the project's
sonar.java.source is not 8.
您的项目很可能依赖于 JDK/JRE 8.
如果您深入研究 the OpenJDK issues,您会发现在 Linux 上问题已部分解决,但在 Windows.
上未解决
关于第二期:
map(x -> x.toString())
只需将其替换为方法参考即可:
map(Path::toString)
所以最终要与 Sonar 兼容,它给出了:
//FIXME use Files::isRegularFile when update with Java>8
List<String> result = walk.filter(p -> p.toFile().exists())
.map(Path::toString)
.collect(Collectors.toList());
我正在使用以下代码遍历目录并获取第一个文件。我无法修复这两个声纳 lint 问题。请帮忙
List<String> result = walk.filter(Files::isRegularFile).map(x -> x.toString()).collect(Collectors.toList());
请进行此更改:
List<String> result = walk.filter(p -> p.toFile().isFile()).map(Path::toString).collect(Collectors.toList());
SonarLint
还要说明建议的原因。
Lambdas should be replaced with method references
Java 8's "Files.exists" should not be used
看看 the 3725 Sonar rule :
The Files.exists method has noticeably poor performance in JDK 8, and can slow an application significantly when used to check files that don't actually exist.
The same goes for Files.notExists, Files.isDirectory and Files.isRegularFile.
Note that this rule is automatically disabled when the project's sonar.java.source is not 8.
您的项目很可能依赖于 JDK/JRE 8.
如果您深入研究 the OpenJDK issues,您会发现在 Linux 上问题已部分解决,但在 Windows.
关于第二期:
map(x -> x.toString())
只需将其替换为方法参考即可:
map(Path::toString)
所以最终要与 Sonar 兼容,它给出了:
//FIXME use Files::isRegularFile when update with Java>8
List<String> result = walk.filter(p -> p.toFile().exists())
.map(Path::toString)
.collect(Collectors.toList());