声纳管:"Invoke method(s) only conditionally"

SonarQube: "Invoke method(s) only conditionally"

让我们考虑有一个来自第三方库的 class Status,它已经实现了自己的 toString() 方法,如下所示:-

public class Status<T> extends AttemptStatus<T> {

    public String toString() {
        StringBuilder sb = new StringBuilder("Status{");
        sb.append("id=").append(this.id);
        sb.append(", startTime=").append(this.startTime);
        sb.append(", endTime=").append(this.endTime);
        .....
    }
}

我在我的代码中使用这个 class,如下所示:-

public class ABC {

private <T> T executeWithRetries(Callable<T> callable, RetryConfig conf) {
    Status<T> status = new CallExecutorBuilder()
            .config(conf)
            .afterFailedTryListener(s -> {
                    LOGGER.warn("Connection failed. Retrying.");
                    MetricsUtil.REDIS_RETRIES_METER.mark();
                    this.redis = createConnection();
            })
            .build()
            .execute(callable);
    LOGGER.info(status.toString());
    return status.getResult();
}

现在 SonarQube 在下面这句话中抱怨 Invoke method(s) only conditionally:-

LOGGER.info(status.toString());

因此,如果我从上面的句子中删除 toString(),那么编译器会按照以下方式抱怨:-

 LOGGER.info(status);

Cannot resolve method 'info(com.evanlennick.retry4j.Status<T>)'

我应该将 status 变量包装在 String.valueOf() 中吗?但是它不会给我在 Status class 中指定的所需的 toString() 方法输出。有什么解决办法吗?

怎么样:

LOGGER.info("{}",status);

status 是一条 java 记录。声纳警告与我在这里所做的无关。