[英]Sonar Change this condition so that it does not always evaluate to “true”. False positive
Our sonar is giving a Change this condition so that it does not always evaluate to "true"
issue and I think it is a false positive 我们的声纳正在给“ 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
...
The get file details method as defined later in class 稍后在类中定义的获取文件详细信息方法
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;
}
From my reading, fileDetails will most likely be non-null, but in a certain scenario it will be null, hence the check. 根据我的阅读,fileDetails很可能为非null,但在某些情况下它将为null,因此进行检查。 Is there a trick here that I'm missing? 这里有我想念的把戏吗?
We're using SonarQube 6.2, with the v4.4.0.8066 of the java plugin 我们正在使用SonarQube 6.2,以及Java插件的v4.4.0.8066
So, java.nio.file.AccessDeniedException
is a sub class of IOException
and isn't directly thrown. 因此, java.nio.file.AccessDeniedException
是IOException
的子类,不会直接抛出。 After review of the function, we decided that returning null for access denied is the wrong thing to do. 在检查了功能之后,我们认为为拒绝访问返回null是错误的做法。 So we removed this catch (leaving the IOException catch in place). 因此,我们删除了此捕获(将IOException捕获留在原处)。 This meant the method now can't return null. 这意味着该方法现在不能返回null。 Which made the if redundant as Sonar tells us 如Sonar所告诉我们的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.