java - Eclipse - Sonar S2629 可能误报新字符串

标签 java eclipse sonarqube sonarlint-eclipse

我正在使用最新的 Eclipse 和 Sonar 插件

answer对于日志记录,有以下行:

log.debug("Request body: {}", new String(body, "UTF-8"));

只有在 DEBUG 级别才应该创建字符串:

/**
 * Log a message at the DEBUG level according to the specified format
 * and argument.
 * <p/>
 * <p>This form avoids superfluous object creation when the logger
 * is disabled for the DEBUG level. </p>
 *
 * @param format the format string
 * @param arg    the argument
 */
public void debug(String format, Object arg);

但 Sonar 将其标记为 squid:S2629:

"Preconditions" and logging arguments should not require evaluation (squid:S2629)

并给出连接的例子

logger.log(Level.DEBUG, "Something went wrong: " + message); // Noncompliant; string concatenation performed even when log level too high to show DEBUG messages

这是误报 Sonar 警告还是我遗漏了什么?

这不是 this question 的副本这通常是在询问规则概念,它是串联的,但不是通过将对象创建为 new String

来格式化的

还有 link的答案说创建 new Date() 不会造成内置格式的问题:

public static void main(String[] args) {
    LOGGER.info("The program started at {}", new Date());
}

}

Logging this way, you avoid performance overhead for strings concatenation when nothing should be actually logged.

最佳答案

非DEBUG模式行

log.debug("Request body: {}", new String(body, "UTF-8"));

代替

log.debug(MessageFormatter.format("Request body: {}", new String(body, "UTF-8")));

避免创建通过 MessageFormatter.format(String messagePattern, Object arg) 创建的字符串,但不是由 new String(body, "UTF-8") 创建的另一个字符串的创建。

这意味着它不是误报,因为参数是在调用日志记录方法之前先计算的。

只要SLF4J does not support lambda expression to lazy evaluate arguments , 以下实用方法可用作解决方法:

private static Object lazyToString(final Supplier<String> stringSupplier) {
    return new Object() {
        @Override
        public String toString() {
            return stringSupplier.get();
        }
    };
}

这可以用于将字节数组转换为字符串仅限于 DEBUG 模式:

log.debug("Request body: {}", lazyToString(() -> new String(body, StandardCharsets.UTF_8)));

关于java - Eclipse - Sonar S2629 可能误报新字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53031847/

相关文章:

java - Android - 无法将 Spinner 值传递给 jsonSendRequest

java - JDK 9+ 不支持 Eclipse : clientBuilder. sslSocketFactory

java - eclipse 如何感知诸如break循环结束、关键字等的东西?

MySQLNonTransientConnectionException : Communications link failure during commit(). 事务解析未知

cruisecontrol.net - 使用 CCNET 从特定位置调用 .bat 文件

java - 将字符串转换为日期 WebService 时出错

java - org.json.JSONException : No value for status Android Parsing

java - 需要帮助添加使用 system.exit(0) 的 JButton

eclipse - 将eclipse与tomcat连接-版本问题

Java - 非空参数的 Sonar 空传递