'SonarQube: "Invoke method(s) only conditionally"

Let's consider there is a class Status from a third party library which has implemented toString() method of their own as per below: -

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);
        .....
    }
}

I use this class in my code as per below: -

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();
}

Now SonarQube complains about Invoke method(s) only conditionally on this sentence below:-

LOGGER.info(status.toString());

So if I remove toString() from above sentence then compiler complains as per below: -

 LOGGER.info(status);

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

Should I wrap the status variable in String.valueOf()? but then it would not give me a desired toString() method output specified in the Status class. Any solution?



Solution 1:[1]

enter image description here

status is a java record. Sonar warning is not related to what I did here.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Alexandru Ciprian Mos