gpt4 book ai didi

java - Sonar 更改此条件,使其不总是计算为 "true"。假阳性

转载 作者:行者123 更新时间:2023-12-02 03:04:59 25 4
gpt4 key购买 nike

我们的 Sonar 给出了更改此条件,以便它并不总是评估为“true”问题,我认为这是误报

    for (Path file: stream) {
FileDetails fileDetails = getFileDetails(path.toString() + '/' + file.getFileName().toString(), file);
if (fileDetails != null) {
fileList.add(fileDetails);
}
}
// Process the list of non null files
...

获取文件详细信息方法(稍后在类中定义)

private FileDetails getFileDetails(String absolutePathName, Path path) {
FileDetails fileDetails = new FileDetails();
fileDetails.setName(path.getFileName().toString());
fileDetails.setAbsoluteName(absolutePathName);
fileDetails.setDirectory(path.toFile().isDirectory());
fileDetails.setFileStoreUri(fileStoreDetails.getUri());
try {
fileDetails.setSize(Files.size(path));
fileDetails.setModifiedDate(new Date(Files.getLastModifiedTime(path).toMillis()));
} catch (java.nio.file.AccessDeniedException e) {
logger.info("Cannot get file details for " + path, e);
fileDetails = null;
} catch (IOException e) {
// No need to throw a checked exception as we can't do anything with it.
logger.error("Cannot get file details for " + path, e);
throw new UncheckedIOException(e);
}
return fileDetails;
}

根据我的阅读,fileDetails 很可能为非空,但在某些情况下它将为空,因此需要进行检查。这里有我遗漏的技巧吗?

我们使用 SonarQube 6.2,以及 v4.4.0.8066 的 java 插件

最佳答案

因此,java.nio.file.AccessDeniedExceptionIOException的子类,并且不会直接抛出。在审查该函数后,我们认为因访问被拒绝而返回 null 是错误的做法。因此我们删除了这个 catch(保留 IOException catch)。这意味着该方法现在不能返回 null。正如 Sonar 告诉我们的那样,这使得 if 变得多余

关于java - Sonar 更改此条件,使其不总是计算为 "true"。假阳性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41878415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com