SonarQube:更改此条件,使其不总是评估为“false”(对于最终在 javax.mail 接收中)

Art*_*s M 6 java jakarta-mail sonarqube

为什么 SonarQube 抱怨这部分代码?

SonarQube 说: 更改此条件,使其不总是评估为“假”

但是我似乎无法理解为什么条件总是错误的?事实上,实际上并非如此,我只是在调试模式下再次重新运行了这部分,它运行良好,它确实进入了内部,并且大多数情况下条件都不是假的。

这是代码部分:

    } finally {
        if ((inboxFolder != null) && (inboxFolder.isOpen())) {
            try {
                inboxFolder.close(true);
            } catch (MessagingException e) {
                log.error(e.getMessage(), e);
            }
        }
        if ((store != null) && (store.isConnected())) {
            try {
                store.close();
            } catch (MessagingException e) {
                log.error(e.getMessage(), e);
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

当尝试使用 javax.email 接收电子邮件时,这是 try-catch 的最后一部分,它同时抱怨 if 条件。

这是这些变量的声明,它们在 try 部分被实例化:

Folder inboxFolder = null;
Store store = null;
Run Code Online (Sandbox Code Playgroud)

那么为什么 SonarQube 会抱怨这个呢?

Dou*_*tie 2

我们遇到了类似的误报,产生错误“更改此条件,使其不总是评估为“假””。有问题的代码如下:

  public Properties getProperties() {
  Properties properties = new Properties();
  InputStream in = getClass().getResourceAsStream("/my.properties");
  IllegalStateException streamCloseError = null;
  try {
    if (in != null) {
      try {
        properties.load(in);
      } catch (Exception e) {
        //fall through...
      }
    }
  } finally {
    try {
      if (in != null) {
        in.close();
      }
    } catch (IOException e) {
      streamCloseError = new IllegalStateException(e);
    }
  }
  if (streamCloseError != null) {
    throw streamCloseError;
  }
  return properties;
}
Run Code Online (Sandbox Code Playgroud)

抛出错误就行了if (streamCloseError != null) {。在阅读了有关使用上面的 try-with-resources 后,我们清理了这段代码。

是否可以使用此规则来检测它是否与“关闭”一起找到,如果是,则提供使用 try-with-resources 的提示?

感谢您考虑这一点。