【发布时间】:2017-06-12 05:12:04
【问题描述】:
我们的声纳给出了一个Change this condition so that it does not always evaluate to "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 插件
【问题讨论】:
-
很难回答,除非没有看到getFileDetails(String)方法的实现